DrVPN
By:
DrVPN

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

January 2, 2017 2k views
Docker Ubuntu 16.04

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

2 Answers

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.

Nginx is one of the most popular web servers in the world and is responsible for hosting some of the largest and highest-traffic sites on the internet. It is more resource-friendly than Apache in most cases and can be used as a web server or a reverse proxy. In this guide,...

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!

Have another answer? Share your knowledge.