I want to have multiple Docker containers on a single server, and have them both host web applications. If I only have one Docker container set up, I can expose port 80 on the Docker container and connect to the Docker container via the public IP address of the physical server. However, I want two Docker containers to both be running on port 80 on the physical server, and be accessed by separate IP addresses from servers on the same network as the physical Docker server. Is this possible? If so, how can I implement this? I've been doing some research and haven't found a way to do it, so any advice would be appreciated.
-
Are these containers exposing an http service? – GregL Jul 06 '19 at 00:10
-
Yes, each one exposes an http service. – AndreasKralj Jul 08 '19 at 16:04
-
In that case, it seems to me that you could just use a load balancer, like HAProxy or Traefik to listen on port 80, and send the traffic to the right container based on whatever rule you want (path, host header, cookie, etc... – GregL Jul 08 '19 at 17:04
1 Answers
You can't have two devices share the same IP address without resulting in connection and traffic problems. At a lower layer, the network hardware uses the MAC address (supposed to be unique to every device) to decide where to send the traffic related to a given connection. Putting another device on the LAN, with an IP address already in use, causes packets to randomly be delivered to each device. One host may be waiting for an ACK packet on an established connection, but it never receives it because it went to a different host.
If you're trying to have a "standby" docker image which would automatically start serving content when the "hot" image goes down, look into HAProxy. If you want to distribute requests across both containers, look into RRDNS. These are some of the cheapest/easiest ways to have multiple hosts online at the same time.
Both options refer to technologies having to do with "load balancing" or "fault tolerance". There are other options but depends how much time, money and effort you want to put into it.

- 3,714
- 12
- 54
- 89
-
1I figured they couldn't share the same IP address. The two applications are different, so I can't use a load balancing system to switch between them since they need to be distinct. Could I give my host multiple IP addresses and then assign those IP addresses to each Docker container? – AndreasKralj Jul 05 '19 at 16:39
-
1Ah, I think you might be looking to NAT (or bridge) your docker containers so the applications are available to other computers, not just local to your server/workstation? This might help: https://blog.oddbit.com/post/2014-08-11-four-ways-to-connect-a-docker/ – Server Fault Jul 05 '19 at 16:59
-
1Yep, that's right. I'll try that method out and see how it goes. Thanks – AndreasKralj Jul 08 '19 at 18:20
-
1Using the NAT method worked. I added another NIC, assigned another IP address to it, and then just specified one IP address to each container I built out. I was then able to host two separate applications on the same physical server via Docker. – AndreasKralj Jul 09 '19 at 17:34
-
1