Mqtt client docker connection error with mqtt broker in host connection

Hi, I’m trying to simulate IOT sensors with help of docker. I’m trying to connect mqtt client running on container to a mqtt broker which is running on the host system. i have subscribed to a topic which stores the logs of mqtt so when the connection is success/failure it prints the messge and logs it in infuxdb. So the error I am facing is instead of mqtt client ip(docker ip address) to be printed it is printing the broker(host) Ip address. Can someone please help me resolving this issue.

What Docker are you using? Docker Engine (Docker CE) or Docker Desktop? And on what platform? Linux, Windows, macOS?

How do you configure the connection between the container and the host? Can you share commands or compose files?

Hi,
I’m currently using docker desktop installed in linux on the host system.
I was trying to use this command docker run --network host -e MQTT_BROKER=“broker ip address” <image_name> after building the image

Then I moved to topic to the Docker Desktop category and tagged with “linux”.

Docker Desktop runs the docker daemon in a virtual machine so the connection between your container and your host will look like a remote connection. You will not see the container IP address if that is what you meant by “Docker ip”. And if you use the host network you will use the host network of the virtual machine not the physical host. There is a host networking mode in Docker Desktop which I always call “fake host network” because that is just forwarded ports from and to the host.

Depending on how Docker Desktop is configured, I guess you could see the host IP instead of the virtual machine IP (if that is what you meant by docker ip"). This is something I have never tested.

UPDATE:

Now I tested it on macOS and yes, you will see the host IP regardless of whether the host networking feature is enabled. If the IP address is important to you, you will need to use Docker CE which is available on Linux and which is running also inside the VM of Docker Deskop…