Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Docker - Run Containers On The Same Host and 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 10.1.0.45:80:80 -i -t ubuntu /bin/bash

# container 2
docker run -p 10.1.0.46:80:80 -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.

Last updated: 8th August 2020
First published: 16th August 2018