Route traffic to a docker container based on subdomain

30,681

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

my answer may come to late but when you use docker you don't really need ssh to connect to your containers. with the docker exec command, you can run shell command directly in your running container.

here is my advice use the nginx proxy container listed at the beginning for configuring sub-domains. and run portainer on your host in order to have a visual overview of your Containers, images, logs and even execute command in it all of this through the portainer gui.

Share:
30,681
Adam Jimenez
Author by

Adam Jimenez

Updated on December 02, 2020

Comments

  • Adam Jimenez
    Adam Jimenez over 3 years

    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.