Docker compose not letting go of ports?

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.

You probably have another container running, when it happens, do a docker ps and verify what is running…

Yes but none on that port.

and what does netstat -anvp | grep listen | grep say?

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.

Will check next time it happens.

Strange it has been working fine for 2 years. Gonna see if I find any logs.

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? :smiley:

This is why unattended upgrades are typically forbidden in production environments!

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 then ran:

sudo docker compose down
sudo lsof -i -P -n | grep 1883

Lists 2 processes:

mosquitto   635 mosquitto    5u  IPv4  18726      0t0  TCP 127.0.0.1:1883 (LISTEN)
mosquitto   635 mosquitto    6u  IPv6  18727      0t0  TCP [::1]:1883 (LISTEN)

Killing it:

sudo kill 635

That just spawns a new process with a new id.

So I had to do
sudo service mosquitto stop

Now I can create the container again

sudo docker compose up -d

Now it works as expected and show up again.

This is the same problem I get sometimes when I use watchtower to update the container. Very annoying.

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.

sudo docker ps --all

Only shows the mosquitto container running though.

I did prune now however, reclaimed about 2GB of space. :smiley:

Apparently, watchtower even has a cleanup feature which I have enabled but perhaps it is not working as intended. Arguments - Watchtower

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.

Hmm. Im pretty sure I havent installed it at least, and as far as I know its not included with debian. But I will check.

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:

sudo service mosquitto stop

Am I missing something in my configuration?

services:
  mosquitto:
    image: eclipse-mosquitto:latest
    container_name: mosquitto
    volumes:
      - /home/user/docker/mosquitto/conf:/etc/mosquitto
      - /home/user/docker/mosquitto/conf/mosquitto.conf:/mosquitto/config/mosquitto.conf
    ports:
      - 1883:1883
      - 9001:9001
    restart: unless-stopped

mosquitto.conf

listener 1883
persistence true
allow_anonymous false
password_file SECRET