Docker Community Forums

Share and learn in the Docker community.

Docker Mosquitto container not working

I can’t get the mosquitto container working properly, but don’t know why.
when starting the container, the following information is logged:

0: mosquitto version 2.0.14 starting
0: Config loaded from /mosquitto/config/mosquitto.conf.
0: Starting in local only mode. Connections will only be possible from clients running on this machine.
0: Create a configuration file which defines a listener to allow remote access.
0: For more details see Authentication methods | Eclipse Mosquitto
0: Opening ipv4 listen socket on port 1883.
0: Opening ipv6 listen socket on port 1883.
0: Error: Address not available
0: mosquitto version 2.0.14 running

My mosquitto.conf file is locally on my pi on this location:
/mosquitto/config/mosquitto.conf

This is the content:
persistence true
persistence_location /mosquitto/data/
log_dest file /mosquitto/log/mosquitto.log
per_listener_settings true
allow_anonymous true
connection_messages true
log_type all
listener 1883

Is the path mentioned for the mosquitto config file inside the container of outside on the host machine ? I have created it on my host.

I suppose that the message in the log file metioning:
Error: Address not available
is causing the issue, but how do I resolve it ?

Good evening :slight_smile:

The path mentioned within the container’s log is the path within the container.
If you want to use a config file from outside the container (i.e. a file on your local computer) you have to “mount” it into the container.

If you are using docker-compose.yml you can do so similar to the following example:

version: '3'

services:
    mosquitto:
        image: eclipse-mosquitto:latest
        restart: unless-stopped
        ports:
            - 1883:1883
        volumes:
            - ./mosquitto.conf:/mosquitto/config/mosquitto.conf:ro

which mounts the mosquitto.conf from the same directory as the docker-compose.yml into the container’s /mosquitto/config/mosquitto.conf.
If you start your container using the docker run ... - there is also an option for this:
docker run ... --volume /absolute/path/to/file.on.host:/path/in/container/file ...

Regarding the address not available-message: please check if another service is listening to this port preventing your container from using this port. You can use the following command for this purpose: sudo netstat -tulpn | grep :1883

Thank you very much for this usefull information!
This really helps me understand how it works.

When running the docker-compose up command with the provided yml file, I get the following error. I am running docker on a Pi, can it be related to that ?

Creating network “mosquitto_default” with the default driver
Creating mosquitto_mosquitto_1 … error

ERROR: for mosquitto_mosquitto_1  Cannot start service mosquitto: OCI runtime create failed: container_linux.go:380: starting container process caused: process_linux.go:545: container init caused: rootfs_linux.go:76: mounting "/mosquitto/mosquitto.conf" to rootfs at "/mosquitto/config/mosquitto.conf" caused: mount through procfd: not a directory: unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type

ERROR: for mosquitto  Cannot start service mosquitto: OCI runtime create failed: container_linux.go:380: starting container process caused: process_linux.go:545: container init caused: rootfs_linux.go:76: mounting "/mosquitto/mosquitto.conf" to rootfs at "/mosquitto/config/mosquitto.conf" caused: mount through procfd: not a directory: unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type
ERROR: Encountered errors while bringing up the project.

I get the following message when running docker-compose up:
Can it be related to the fact that I use a Pi?
Creating network “mosquitto_default” with the default driver
Creating mosquitto_mosquitto_1 … error

ERROR: for mosquitto_mosquitto_1 Cannot start service mosquitto: OCI runtime create failed: container_linux.go:380: starting container process caused: process_linux.go:545: container init caused: rootfs_linux.go:76: mounting “/mosquitto/mosquitto.conf” to rootfs at “/mosquitto/config/mosquitto.conf” caused: mount through procfd: not a directory: unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type

ERROR: for mosquitto Cannot start service mosquitto: OCI runtime create failed: container_linux.go:380: starting container process caused: process_linux.go:545: container init caused: rootfs_linux.go:76: mounting “/mosquitto/mosquitto.conf” to rootfs at “/mosquitto/config/mosquitto.conf” caused: mount through procfd: not a directory: unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type
ERROR: Encountered errors while bringing up the project.

Have you tried what the error message said?

Yes, the path on my host exists. it is exactly what is mentioned:
/mosquitto/config/mosquitto.conf

the permissions are set to 777, but I suppose it doesn’t matter.
Can it be related to a user account that requires access ?

I don’t think so. The mount happens on behalf of the root user.

Try with different mount points just for testing. Change the host path first, then change the path in the container. Try to mount to the root folder for example:

  volumes:
    - ./mosquitto.conf:/mosquitto.conf

If it doesn’t help, share your docker compose yaml. Especially the volume mount part.

Thank you. it works fine now.

I have another question. now both mosquitto and zigbee2mqtt run in a container, few other things I am wondering:

  • The mosquitto and zigbee2mqtt containers didn’t get an ip address, as the network configuration was set to ‘host’. as the other containers work fine where the network is set to ‘bridge’, I have changed these containers to ‘bridged’ as well. Now they do have an ip address and port number, is that ok, or should it be on ‘host’ ?

  • although the containers are running, I am not able to acces the mosquitto or zigbee2mqtt container by its ip address and port number, how come ? what should I do to be able to reach the web site of these containers ?

the zigbee2mqtt container needs to point to the mosquitto host and has to connect to port 1883, the configuration.yaml file wants to connect to the ‘localhost’, is that correct ? after changing it to the ip address of the container it didn’t work either.

I am really stuck now, as all containers are running, but I don’t book any progress on the zigbee configuration. tips are really appreciated.

[url=https://ibb.co/KWKbZsB][/url]

The error you are facing might occur if you previously tried running the docker run command (docker-compose … works the same way) while the file was not present at the location where it should have been in the host directory.
In this case docker daemon would have created a directory inside the container in its place, which later fails to map to the proper file when the correct files are put in the host directory and the docker command is run again.
The solution is to stop and remove the container and the associated volumes and try again.

At least for the bridge-network the container’s localhost is not the same as your computer’s localhost (at least if you don’t apply some “tweaks” - see Connecting redis to a container image - #5 by rimelek). To talk to the computer’s localhost from within a container use the 172.17.0.1 (on linux this is the host in docker’s default-network) or host.docker.internal (on Windows).
Don’t use container’s ip-addresses as they change if you start the container the next time. Instead use the container’s name as DNS-name to talk from a container to the other one.
Or if you have both containers in one docker-compose.yml you can also use the service’s name as DNS-name to connect to.

I have deleted all stuff from the mosquitto and zigbee2mqtt containers, including volumes and started over again. I used the following commands to install them again:

mosquitto:
docker run -itd --name=mqtt --restart=always --net=host -v /mosquitto/config:/mosquitto/config -v /mosquitto/data:/mosquitto/data -v /mosquitto/log:/mosquitto/log eclipse-mosquitto

zigbee2mqtt:
docker run --device=/dev/ttyACM0 -v $(pwd)/data:/app/data -v /run/udev:/run/udev:ro -e TZ=Europe/Amsterdam koenkk/zigbee2mqtt

the mosquitto container runs, and used the host network (as you can see in the command), it doesn’t publish a port, but that seems normal in host mode. I don’t know how to connect to it. I try the host ip address and port 1883 or 9001 but that doesn’t work.

The zigbee2mqtt container runs only very short and then stops again. this container uses the bridge mode. will that work properly with mosquitto, or should I run both containers in the same network configuration (host or bridge mode)?

The zigbee2mqtt container gives the following error. I changed the ip addres to the host address of the pi. by default it was set to localhost. both settings don’t work
Zigbee2MQTT:error 2021-12-02 21:35:04: MQTT failed to connect: connect ECONNREFUSED 192.168.178.17:1883

a few questions:

  • What should the network configuration be for both containers ? (host/bridge)
  • How do I get the zigbee2mqtt container working properly
  • How do I access the website for these applications ?