Home > database >  How can I run two docker containers using the same port?
How can I run two docker containers using the same port?

Time:10-02

I am pretty new to docker containers, but I have the following task:

I have a notebook running Ubuntu 20.04 on which I have to run TTS (The Thingss Stack), with it's MQTT broker, and also Mosquitto. The problem is that both of them need to access port 1883 (the default port for MQTT communication).

I would like to know if I there's a way to configure these containers in order for both to have MQTT connection. Thanks!

CodePudding user response:

Map one to 1883 and one to 1884.

The containers may both expose port 1883, but you can't map both of them to that port on the host machine. So map one to port 1883 and the second to port 1884.

This is what the -p option on the docker run command line does.

So for one container do -p 1883:1883 and the other -p 1884:1883

You will just need to tell what ever client you want to connect to the 1884 not to use the default port.

  • Related