Docker - Run Containers On The Same Host & Port

It used to take quite a bit of effort to run two containers that want to use the same public port on the same host. However, now it's quite simple, as long as your host has multiple IP addresses.

If you don't have multiple IP addresses assigned to the host you could always create a virtual interface.

If you can't assign multiple IPs to your host, and the containers you are trying to deploy are for websites, then you could set up an nginx reverse proxy through docker.

We will run each container on a different IP, but with the same port. All this takes is adding the IP to the port specification as shown in the example below:

# container 1
docker run -p -i -t ubuntu /bin/bash

# container 2
docker run -p -i -t ubuntu /bin/bash

With AWS, you will need to specify the private IP address of the NIC assigned to your EC2 instance rather than the public IP that goes to that interface.



Stuart is a software developer with a passion for Linux and open source projects.

comments powered by Disqus
We are a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to and affiliated sites. More info.