Gilles Major
Gilles Major

Reputation: 499

mapping containers to docker host's /etc/hosts automatically with the same port for each container

I have a basic docker-compose setup consisting of the following:

I cannot manage to resolve these host entries without mapping a different port than 8080 to the docker host.

For this entry in my host's /etc/hosts: 192.168.50.1 fares rabbit config book checkin: the services are only accessible if I explicitely bind the services' ports 8080 to my host's port 8081, port 8082, port 8083... for each service in the .yml file.

Is there another way to make sure the services are discoverable by their dns name even from outside of the subnet?

Upvotes: 1

Views: 1135

Answers (1)

mkasberg
mkasberg

Reputation: 17262

You can't bind all 4 containers to the same port on the host. Only one container per port. But there are some workarounds:

Option 1: Use Different Ports for Each Container

  • For exmaple, bind ports 8081, 8082, 8083, and 8084.
  • In /etc/hosts, map each containers IP correctly.
  • Specify the port in addition to the hostname when connecting. Like https://fares:8081

Your /etc/hosts might look like this:

192.168.50.1 fares
192.168.50.2 rabbit
...

Option 2: Use a Reverse Proxy

You can set up an additional Docker container as a reverse proxy in your docker-compose.yml. The reverse proxy container can bind to port 8080 and forward the request to the correct container depending on the hostname. You don't need to bind ports from the other containers on the host because your reverse proxy is forwarding the requests. There's a blog post that explains how this works in detail: http://jasonwilder.com/blog/2014/03/25/automated-nginx-reverse-proxy-for-docker/

Upvotes: 1

Related Questions