Question

Multiple Docker containers on the same host listening to the same port?

I have several web sites. I would like to have each in a separate Docker container, to keep them isolated from one another. If I had multiple IP addresses I could bind each IP to a container. like this:

docker run -p 10.0.0.10:80:80 -name container1 <someimage> <somecommand> docker run -p 10.0.0.11:80:80 -name container2 <someimage> <somecommand>

But, since on here I only have a single IP that is out of the question. Does anyone have any idea how I could make it work? Only thing I can think of would be to run a load balancer/reverse proxy or NAT or something similar. I know I could put them on a different port but then I could loose traffic by it being blocked because it is not common like 80.

If anyone can provide some insight it will be greatly appreciated.

Thanks,

Nick

Subscribe
Share

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there,

I just came across this question. In addition to all of the comments above, I would suggest taking a look at this answer here as well:

https://www.digitalocean.com/community/questions/how-to-host-multiple-docker-containers-on-a-single-droplet-with-nginx-reverse-proxy

This includes a short video demo on how to achieve this as well.

Regards, Bobby

You can’t run two instances of application (in your case Docker) on same machine. This is how Linux & Docker is built and nothing you can change. But there is workaround. Run containers on different port and set up Apache or Nginx. As Apache/Nginx works on port 80, you’ll not lose any traffic, as 80 is common port. I would recommend Nginx - I find it much easier to set up reverse proxy with Nginx and it’s lighter on resource compared to Apache. For nginx, you need to set up it and learn more about Server Blocks: How To Install Nginx on Ubuntu 16.04. How To Set Up Nginx Server Blocks (Virtual Hosts) on Ubuntu 16.04. In Server Blocks, you need to use proxy_pass, for which you can learn more about on nginx site.

If you host only two containers, you can go with Droplet IP + Floating IP, so you have 2 IP addresses. It would work normally, but if you need to add third container, it’s not working anymore. As an option, there is IPv6. You can get up to 16 IPv6 IP addresses and you can use Docker with IPv6. But please be careful, this is VERY RISKY!!! Not all providers around the world supports IPv6 protocol and some users could not access your site.

Conclusion: It’s best to go with reverse proxy with Nginx/Apache. Looking at resource standpoint, it’s most lightweight. Site accessibility - all sites work on port 80 so you don’t have problem with this.

You can’t run two instances of application (in your case Docker) on same machine. This is how Linux & Docker is built and nothing you can change. But there is workaround. Run containers on different port and set up Apache or Nginx. As Apache/Nginx works on port 80, you’ll not lose any traffic, as 80 is common port. I would recommend Nginx - I find it much easier to set up reverse proxy with Nginx and it’s lighter on resource compared to Apache. For nginx, you need to set up it and learn more about Server Blocks: How To Install Nginx on Ubuntu 16.04. How To Set Up Nginx Server Blocks (Virtual Hosts) on Ubuntu 16.04. In Server Blocks, you need to use proxy_pass, for which you can learn more about on nginx site.

If you host only two containers, you can go with Droplet IP + Floating IP, so you have 2 IP addresses. It would work normally, but if you need to add third container, it’s not working anymore. As an option, there is IPv6. You can get up to 16 IPv6 IP addresses and you can use Docker with IPv6. But please be careful, this is VERY RISKY!!! Not all providers around the world supports IPv6 protocol and some users could not access your site.

Conclusion: It’s best to go with reverse proxy with Nginx/Apache. Looking at resource standpoint, it’s most lightweight. Site accessibility - all sites work on port 80 so you don’t have problem with this.

You can’t run two instances of application (in your case Docker) on same machine. This is how Linux & Docker is built and nothing you can change. But there is workaround. Run containers on different port and set up Apache or Nginx. As Apache/Nginx works on port 80, you’ll not lose any traffic, as 80 is common port. I would recommend Nginx - I find it much easier to set up reverse proxy with Nginx and it’s lighter on resource compared to Apache. For nginx, you need to set up it and learn more about Server Blocks: How To Install Nginx on Ubuntu 16.04. How To Set Up Nginx Server Blocks (Virtual Hosts) on Ubuntu 16.04. In Server Blocks, you need to use proxy_pass, for which you can learn more about on nginx site.

If you host only two containers, you can go with Droplet IP + Floating IP, so you have 2 IP addresses. It would work normally, but if you need to add third container, it’s not working anymore. As an option, there is IPv6. You can get up to 16 IPv6 IP addresses and you can use Docker with IPv6. But please be careful, this is VERY RISKY!!! Not all providers around the world supports IPv6 protocol and some users could not access your site.

Conclusion: It’s best to go with reverse proxy with Nginx/Apache. Looking at resource standpoint, it’s most lightweight. Site accessibility - all sites work on port 80 so you don’t have problem with this.

I was able to configure Nginx as a reverse proxy to sit in front of my containers which allows me to have several containers, listening on HTTP (80), from the external perspective.

Thanks again for the great idea!