Hello
I want to install Home Assistant on Docker with docker-compose. When I want only expose selected ports to host, I have problem with connection HA<->MQTT. When I use network_mode: host for HA image, it works fine. I can show my problematic docker-compose:
After start, I receive error â2022-01-21 15:59:28 ERROR (MainThread) [homeassistant.components.mqtt] Failed to connect to MQTT server due to exception: [Errno 99] Address not availableâ
When I change my docker-compose to something like this:
Switch to host network. Although HA will in general run without it, features like discovery and many integrations will not work, as they open (random) ports.
I am actually wondering why it works when you set host network on the HA container, but do not open port 1883 on the mosquitto container âŚ
All ports are exposed between them so HA can connect to mosquito at 172.2.0.19:1883 without issue. If you give your container a host name in docker like âmqttâ for mosquito it will be accessible at mqtt:1883. This is best as docker IP may change if you recreate container
Additionally you may create individual docker networks to limit each containers access to other containers via network
I know, but with network_mode: host you take that container of the docker network. And since the MQTT container does not use host-network, HA cannot connect to it using âlocalhostâ
Thanks, I know that I can switch to host network, but itâs not answer for my question I donât want to use it, because I have other services on same server, so I want to fully control used ports. Why it doesnât work with opened port 8123 and MQTT access via docker network?
so the issue you are facing is, that HA cannot access the MQTT service from the same docker-compose network stack, if HA is not using the host network. I think the reason is, that you donât expose the MQTT port (1883) for the Mosquitto container. Try something like:
You also say, that it does work however, if you put only HA to host networking and that what makes me wonder. Iâd expect that you need to add a ports statement to the MQTT service, like
Reason: MQTT container is still local to the docker-compose network. When setting network_mode: host for the HA container, you take it off the compose network. To connect to the MQTT service, that service must bind a host port to the container port 1883. Then, HA (and any other client) can connect to MQTT.
Exactly. I added expose with 1883 to mosquitto in docker-compose and I tried to set broker host in HA to 172.17.0.1 or mosquitto and it doesnât work⌠Still same error. I have postgres also in docker-compose, I donât have configured ports/expose and it works from HA - in recorder I use service name as host.
OTOH, if container name is not set in the compose, IIRC, the generated container name is something like âservice-image-numberâ, and that will be the DNS name. docker ps will tell
docker ps shown automatically generated names (directory name + service name), but it doesnât matter.
I changed network_mode to host again for HA container and look:
$ docker-compose exec homeassistant ping mosquittooooo
ping: bad address âmosquittoooooâ
mosquittooooo is an invented name that doesnât exists - ping doesnt work.
$ docker-compose exec homeassistant ping mosquitto
PING mosquitto (172.18.0.3): 56 data bytes
64 bytes from 172.18.0.3: seq=0 ttl=64 time=0.268 ms
64 bytes from 172.18.0.3: seq=1 ttl=64 time=0.197 ms
64 bytes from 172.18.0.3: seq=2 ttl=64 time=0.194 ms
^C
â mosquitto ping statistics â
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.194/0.219/0.268 ms
and it works - HA container see mosquitto container by docker service name
In HA container logs still:
homeassistant_1 | 2022-01-22 12:21:03 ERROR (MainThread) [homeassistant.components.mqtt] Failed to connect to MQTT server due to exception: [Errno 99] Address not available
How about removing the mqtt section from configuration.yaml and using the MQTT integration from UI? The configuration flow of that integration may provide more information.
Yes, youâre right! I donât know how and why, but when I added MQTT integration from UI and typed host mosquitto it works without network_mode: host for container. I wouldnât think to check it. Thank you very much