Search code examples
dockerdocker-containerdocker-network

How to expose docker containers to the internet if they share the host port?


I am using docker for windows. There are 2 containers running Windows IIS on ports listed below:

Container 1: 0.0.0.0:50095->80/tcp
Container 2: 0.0.0.0:50093->80/tcp

My host machine IP is 192.168.25.110

As shown above, both containers map to port 80. So when external users browse to 192.168.25.110:80, which of the 2 containers will be accessed? Is there a deterministic behavior?


Solution

  • As David mentioned in the comments, you are backwards on your port mappings. Based on what you've given, for example, you would navigate to http://192.168.25.110:50095/ in order to reach Container1.

    That's probably not what you want as you likely want to use the default HTTP port (80).

    You should look into a reverse proxy solution for your situation. The reverse proxy will listen on port 80 for ALL traffic and then decide (based on configuration) which container to send the request to.

    Traefik is a popular solution for this. Traefik listens on port 80 and then you can configure it to route traffic to different containers based on rules.

    You could do something like:

    • http://192.168.25.110/container1 => Container1
    • http://192.168.25.110/container2 => Container2

    OR

    • http://container1.docker.local => Container1
    • http://container2.docker.local => Container2

    Take a look at Traefik's quick start guide: https://docs.traefik.io/getting-started/quick-start/