Been using docker to host HA and other containers related to HA without problems for years up until now. I have had HA, Mosquitto, Deconz and more.
Recently, I moved from Deconz to Z2M which was simple just to replace the container. I also use watchtower to update my containers every week, that means the container will reboot if there is an update.
Today, I woke up and nothing worked. It seemed my mosquitto container could not start since the default port of 1883 was already in use. Tried rebooting the entire machine, it did not help. I had to kill the process using the port, which was Mosquitto by the way, then it could start just fine. The very same thing happend a couple of weeks ago.
docker compose up
[+] Running 2/2
✔ Network mosquitto_default Created 0.1s
✔ Container mosquitto Created 0.0s
Attaching to mosquitto
Error response from daemon: failed to set up container networking: driver failed programming external connectivity on endpoint mosquitto (id): failed to bind host port for 0.0.0.0:1883:172.20.0.2:1883/tcp: address already in use
x@home-server:~/docker/mosquitto$ sudo docker compose down
x@home-server:~/docker/mosquitto$ sudo lsof -i -P -n | grep 1883
mosquitto 650 mosquitto 5u IPv4 20518 0t0 TCP 127.0.0.1:1883 (LISTEN)
mosquitto 650 mosquitto 6u IPv6 20519 0t0 TCP [::1]:1883 (LISTEN)
sudo kill 650
sudo lsof -i -P -n | grep 1883
docker compose up
Why has this started happening now? How can I avoid it? There seems to be some sort of conflict around this port? The old Mosquitto container do not release it properly? As I have said, it seems to started happening since adding the Z2M container but it could be a coincidence of course.
Confirm who’s using port 1883 (lsof or netstat). Properly stop and remove the Mosquitto container before updates. Check for lingering processes.
Review Watchtower logs for update-related issues. Consider assigning a temporary port during updates as a fallback.
Try docker network ls to get a list of networks docker is aware of. The “NAME” column will probably indicate that one line exists in relation to your app.
Remove that using a docker network rm <NAME>.
Do a docker ps, there you can see every running container (add -a to see all). The interfaces and ports the containers are configured to use are listed there.
Yeah, I did that and the Mosquitto container was not there and no other container occupied that port either. Still, could not start it because port was in use.
You can force port release using docker compose down --remove-orphans, or locate the occupying process via netstat -ano | findstr :port_number and terminate it.
Strange, this morning there was new containers, HA and Mosquitto was updated. It looked like my smart power meter, sending MQTT messages, was working but not any of my zigbee klights. First I restarted the Mosquitto container, it did not do anthing. Then I restarted my Zigbee2Mqtt container and it worked, now my all zigbee devices work again. Checking the logs shows nothing strange. Checking zigbee2mqtt logs looks like it lost connection to MQTT 03:04, when the container was updated. But just after a minute or or it was back sending messages again.
Maybe I should schedule a reboot on the entire linux machine after containers are updated?
Hehe, yeah, been working fine for years though I might just start updating it manually every 6 month or so instead, at least the critical containers. Still thinking big bang releases is more dangerous than just having small iterative releases each week.
Now i am having issuse again. A short power outage caused my machine to reboot. Now, my mosquitto container is having issues again. This what I did.
sudo docker ps
Tells me that all containers are running except the mosquitto one, its not present at all.
Navigating to the docker-compose file, running sudo docker compose up
Attaching to mosquitto
Error response from daemon: failed to set up container networking: driver failed programming external connectivity on endpoint mosquitto (bd0af941709eff662cf44b6fd01b9cc6625427df894e79f025491636d6b9a4b8): failed to bind host port for 0.0.0.0:1883:172.20.0.2:1883/tcp: address already in use
I know this was already mentioned, but I think it’s a good idea to reiterate. Whenever I see a Docker message with “already in use” I ensure to run sudo docker ps --all since you might have containers that exist with some resource bound to them, but not running. sudo docker ps will only show the running containers. Remove any stopped containers that you don’t need or want to start over with fresh.
Also as mentioned above, including the --remove-orphans flag when taking down a Docker compose service, will help to remove containers no longer connected to a service (like if you changed your YAML while the service was running). Worst case, if that wasn’t used, you can use the sudo docker ps --all command, find the lingering containers, and use sudo docker rm {{CONTAINER}} to remove it. You can also run sudo docker system prune (reference) to remove all stopped containers, unused images, dangling networks, and unused cache. Good to run on occasion to reclaim some disk space too, however it’s good to be aware of what’s getting removed first.
This reads like you have mosquitto installed on the host directly. That the process is only listening on localhost is a second indicator.
Uninstall mosquitto locally (or at least disable it) and the issue should be gone.
Now it happend again after a power outage. This time, mosquitto container did not start so Z2M container could not start either. Annoying. The only way to fix is stop the mosquitto process like so: