Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Route traffic to a docker container based on subdomain

I have wildcard dns pointed to my server e.g. *.domain.com

I'd like to route each subdomain to it's own docker container. So that box1.domain.com goes to the appropriate docker container. This should work for any traffic primarily HTTP and SSH.

Or perhaps the port can be part of the subdomain e.g. 80.box1.domain.com. I will have lots of docker containers so the solution should be dynamic not hard-coded for every container.

like image 827
Adam Jimenez Avatar asked Jul 02 '14 09:07

Adam Jimenez


3 Answers

Another solution would be to use https://github.com/jwilder/nginx-proxy.

This tool automatically forwards requests to the appropriate container (based on subdomain via the VIRTUAL_HOST container environment variable).

For instance, if you want to redirect box1.domain.com to a container, simply set the VIRTUAL_HOST container environment variable to "box1.domain.com".

Here is a detailed tutorial I wrote about it: http://blog.florianlopes.io/host-multiple-websites-on-single-host-docker.

like image 153
Florian Lopes Avatar answered Nov 10 '22 11:11

Florian Lopes


I went with interlock to route http traffic using the nginx plugin. I settled on using a random port for each SSH connection as I couldn't get it work using the subdomain alone.

like image 45
Adam Jimenez Avatar answered Nov 10 '22 09:11

Adam Jimenez


The easiest solution would be to use the Apache mod_rewrite RewriteMap method. It's very performant when used against a text file, but it can call a script if desired. There is another StackOverflow answer that covers the script variant pretty well.

If you want to avoid Apache, the good folks over at dotCloud created Hipache to do the routing for their PaaS services. They even documented the different things they tried before building their own solution. I found a reference to tsuru.io using hipache exactly for routing to docker containers, so that definitely validates it for this purpose.

like image 1
Robert Wohleb Avatar answered Nov 10 '22 10:11

Robert Wohleb