How to run multiple docker containers listening to the same port

Let’s say you need to run multiple dockerized web servers on the same machine. Obviously you could just use a separate port for each one, but we want to work with hostnames instead.

There is no way around running a reverse proxy. Luckily enough that’s not a big deal with Docker. There is an image just for that. Add the following service to your docker-compose.yml:

services:

    nginx-proxy:
        image: jwilder/nginx-proxy
        ports:
          - "80:80"
        volumes:
          - /var/run/docker.sock:/tmp/docker.sock:ro

Now let’s just add two different simple web services both listening to port 80 to test the proxy:

services:

    nginx-proxy:
        # ...

    web1:
        image: tutum/hello-world
        environment:
          - VIRTUAL_HOST=web1.local
    
    web2:
        image: jwilder/whoami
        environment:
          - VIRTUAL_HOST=web2.local

Finally, map the hostnames to localhost in your /etc/hosts file. Just add the following lines:

127.0.0.1   web1.local
127.0.0.1   web2.local

That’s it! Now do a docker-compose up and and access the services with your browser. You should see the following:

web1.local web2.local
web1.local web2.local