Route traffic to a docker container based on subdomain
Asked Answered
P

5

35

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.

Polypus answered 2/7, 2014 at 9:28 Comment(0)
P
8

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.

Polypus answered 24/6, 2015 at 10:5 Comment(0)
C
17

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.

Crackerbarrel answered 5/4, 2016 at 19:4 Comment(3)
Good to know, that works in a similar way to interlock. Does it facilitate SSH?Polypus
Yes it seems to. I don't know in which way it could facilitate SSH as Nginx-proxy does handle HTTP requests.Crackerbarrel
If you want to SSH into your Docker containers using subdomains, I think you can add a CNAME DNS rule pointing to your host IP. Exposing SSH container's port to host (hence to public), let's say 1020, would allow you to SSH into your container using your subdomain: ssh -p 1020 sub.domain.com.Crackerbarrel
P
8

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.

Polypus answered 24/6, 2015 at 10:5 Comment(0)
F
1

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.

Fascinate answered 15/7, 2014 at 7:39 Comment(4)
Here is a link to the tsuru.io mention I referenced: tsuru.io.Fascinate
Also, here is some information on doing dynamic reverse proxying with nginx and dnsmasq, though I find it a bit awkward: nginx + dnsmasqFascinate
Also take a look at the following StackOverflow question: #18498064Fascinate
Do any of these solutions support SSH traffic to the containers?Polypus
M
1

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.

Maudiemaudlin answered 1/12, 2016 at 12:46 Comment(0)
D
0

I used apache proxypresevehost

ProxyPreserveHost On ProxyPass "/" "http://localhost:4533/" ProxyPassReverse "/" "http://localhost:4533/"

Declinometer answered 5/2, 2023 at 16:11 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Anallise

© 2022 - 2024 — McMap. All rights reserved.