Search code examples
dockernetwork-programmingmosquitto

Networking-How to send data from a container to mosquitto container in the same network


I have a raspberry pi which is running a mosquitto docker container. This container contains file and all the configuration required to send data to IOT hub. I also have another container data-container which performs some set of steps and then it needs to send data to that IOT hub using mosquitto docker container. data-container has a python code which used paho-mqtt library to publish and subscribe to messages.

I have created my own network in docker mynetwork by using below command:

sudo docker network create mynetwork

This created the network mynetwork. I then started the mosquitto container by specifying the mynetwork:

sudo docker run -ti --net=mynetwork --restart=always -v /mosquitto/mqtt/config:/mqtt/config:ro -v /mosquitto/mqtt/log:/mqtt/log -v /mosquitto/mqtt/data/:/mqtt/data/ --name mqtt pascaldevink/rpi-mosquitto

I also started data-container using --net=mynetwork. So both the container are in the same network. Now inside the data-container, it collects few information and publish it using the below command:

publish.single("/machine/machine1/, "<data to send>", hostname=<hostname>)

I am confused as to what to use in hostname in publish.single. Should I mention the IP address of the mosquitto container in hostname.?

Thanks


Solution

  • The container name is fine when they are deployed inside the same network. To be sure you can try to exec inside your container and try to ping the other container using the container name. This should work when they are inside the same docker network.