Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning vhosts to Docker ports

I have a wildcard DNS set up so that all web requests to a custom domain (*.foo) map to the IP address of the Docker host. If I have multiple containers running Apache (or Nginx) instances, each container maps the Apache port (80) to some external inbound port.

What I would like to do is make a request to container-1.foo, which is already mapped to the correct IP address (of the Docker host) via my custom DNS server, but proxy the default port 80 request to the correct Docker external port such that the correct Apache instance from the specified container is able to respond based on the custom domain. Likewise, container-2.foo would proxy to a second container's apache, and so on.

Is there a pre-built solution for this, is my best bet to run an Nginx proxy on the Docker host, or should I write up a node.js proxy with the potential to manage Docker containers (start/stop/reuild via the web), or...? What options do I have that would make using the Docker containers more like a natural event and not something with extraneous ports and container juggling?

like image 392
ringmaster Avatar asked Aug 28 '13 20:08

ringmaster


People also ask

How do I assign an IP address to a Docker container?

When you connect an existing container to a different network using docker network connect , you can use the --ip or --ip6 flags on that command to specify the container's IP address on the additional network. In the same way, a container's hostname defaults to be the container's ID in Docker.

What is port forwarding in Docker?

Port forwarding or port mapping redirects a communication request from one IP address and port number combination to another. Through port forwarding, services are exposed to the applications residing outside of the host's internal network.


1 Answers

This answer might be a bit late, but what you need is an automatic reverse proxy. I have used two solutions for that:

  • jwilder/nginx-proxy
  • Traefik

With time, my preference is to use Traefik. Mostly because it is well documented and maintained, and comes with more features (load balancing with different strategies and priorities, healthchecks, circuit breakers, automatic SSL certificates with ACME/Let's Encrypt, ...).


Using jwilder/nginx-proxy

When running a Docker container Jason Wilder's nginx-proxy Docker image, you get a nginx server set up as a reverse proxy for your other containers with no config to maintain.

Just run your other containers with the VIRTUAL_HOST environment variable and nginx-proxy will discover their ip:port and update the nginx config for you.

Let say your DNS is set up so that *.test.local maps to the IP address of your Docker host, then just start the following containers to get a quick demo running:

# start the reverse proxy docker run -d -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock jwilder/nginx-proxy  # start a first container for http://tutum.test.local docker run -d -e "VIRTUAL_HOST=tutum.test.local" tutum/hello-world  # start a second container for http://deis.test.local docker run -d -e "VIRTUAL_HOST=deis.test.local" deis/helloworld 

Using Traefik

When running a Traefik container, you get a reverse proxy server set up which will reconfigure its forwarding rules given docker labels found on your containers.

Let say your DNS is set up so that *.test.local maps to the IP address of your Docker host, then just start the following containers to get a quick demo running:

# start the reverse proxy docker run --rm -it -p 80:80 -v /var/run/docker.sock:/var/run/docker.sock traefik:1.7 --docker  # start a first container for http://tutum.test.local docker run -d -l "traefik.frontend.rule=Host:tutum.test.local" tutum/hello-world  # start a second container for http://deis.test.local docker run -d -l "traefik.frontend.rule=Host:deis.test.local" deis/helloworld 
like image 200
Thomasleveil Avatar answered Oct 11 '22 15:10

Thomasleveil