Help with MQTT Docker Setup

Hello All,

I am working on setting up a new server for home-automation using a Raspberry Pi 4 and docker with docker compose. The trouble that I am having is using the eclipse mosquitto image and being able to setup the location for the mosquitto.conf file, data, and logs along with assigning a username and password for the broker.

According the information on the docker hub (link). It states that:

Configuration can be changed to:

persist data to /mosquitto/data
log to /mosquitto/log/mosquitto.log
i.e. add the following to mosquitto.conf:

persistence true
persistence_location /mosquitto/data/
log_dest file /mosquitto/log/mosquitto.log

I would like to add a folder where I have all my other docker volume information on a hard drive attached to the pi, but I can’t seem to get it to work. Does anyone know what I add to presistence_location and log_dest file location when using volumes outside of the container? Also, how do I enable usernames and passwords?

I have searched the internet and seems that I find examples that don’t work or are very hard to understand.

Thank you in advance for the help.

Chris

You use bind mounts. Bind mount a location on the host with those directories

flamingm0e:

It is interesting that you responded to this topic, but I was actually using your docker-compose on your github last night as an example. Do you have an example on how to do this? I am a little new to docker.

Thanks,

Chrs

Look at my docker compose file. I have volumes configured for MQTT. This is a standard bind mount.

That is where I am having the problem I think. You have the following in your docker compose file.

 mqtt:
    container_name: MQTT
    restart: unless-stopped
    image: eclipse-mosquitto
    privileged: true
    volumes:
      - /srv/docker/mosquitto/config/mosquitto.conf:/mosquitto/config/mosquitto.conf
      - /srv/docker/mosquitto/log:/mosquitto/log
      - /srv/docker/mosquitto/data:/mosquitto/data
      - /etc/localtime:/etc/localtime:ro
    ports:
      - "1883:1883"
      - "9001:9001"

Where is/srv/docker/mosquitto/config/mosquitto.conf, etc.? Like I said in my original post I have all my files on a drive that is mounted to the pi. For example my files are located in /mnt/dietpi_userdata/home-automation. In the mount there is a docker folder. From what I read in the docker hub. I need to create the mosquitto.conf file. For something so simple and used everywhere mostly in home automation there are no good beginner tutorials to setup mosquitto in docker.

Chris

They’re in /srv/docker in my OS

Yes. You should be able to use any sample available. It doesn’t require much in the way of config.

So what exactly is the issue? Create the directory. Create the files, and run the docker image.

you need to create directory and the file on your external drive on pi 4 .Use bind_mount to mount the required config file
from
/mnt/yourdrive/*.conf on hostos’s external drive
to
/mosquitto/[conf_files_locations]
inside container.

This should work already if i’m getting you right.

Given the information you provide above your volumes will be mounted as:

volumes:
  - /mnt/dietpi_userdata/home-automation/docker/mosquitto/config/mosquitto.conf:/mosquitto/config/mosquitto.conf
  - /mnt/dietpi_userdata/home-automation/docker/mosquitto/log:/mosquitto/log
  - /mnt/dietpi_userdata/home-automation/docker/mosquitto/data:/mosquitto/data

you will need to first create all of those directories and files in the correct locations.

Okay, so I tried this out again this evening and I am able to get the container up and running, but I still have a problem. When I try to get into the containers terminal to create a username and password by the following command:

sudo docker exec -it mqtt sh

I get the following error even though portainer shows the container running:

Error response from daemon: Container 8758daf77b819eb536f22a8602dd68be7c78c9f820de1a8756990c356c684ea7 is restarting, wait until the container is running

This what I have in my docker-compose file:

  mqtt:
    container_name: mqtt
    restart: unless-stopped
    image: eclipse-mosquitto
    privileged: true
    volumes:
      - /mnt/dietpi_userdata/home-automation/mqtt/config/mosquitto.conf:/mosquitto/config/mosquitto.conf
      - /mnt/dietpi_userdata/home-automation/mqtt/log:/mosquitto/log
      - /mnt/dietpi_userdata/home-automation/mqtt/data:/mosquitto/data
      - /etc/localtime:/etc/localtime:ro
    ports:
      - "1883:1883"
      - "9001:9001"

This what I have in my mosquitto.conf:

persistence true
persistence_location /mnt/dietpi_userdata/home-automation/mqtt/data/
log_dest file /mnt/dietpi_userdata/home-automation/mqtt/log/mosquitto.log

Please help and let me know if you can.

Thanks in advance,

Chris

have you thought about running docker logs -f --tail 0 mqtt

or

docker-compose logs -f

The logs of the docker container will tell you WHY it’s restarting.

I haven’t done that but will check when I get home from work.

I guess the question is did I do the conf file correctly or should be blank?

A blank config file isn’t of much use to most applications, and this is one of those that requires options to be set in the config in order for it to work.

It seems that the container is having a hard time writing the log file. I tried changing the permissions on the directory and file and no luck.

Chris

Try running with environment variables for the uid/gid for root

Okay, so I tried that and still did not work. I also googled the internet and tried other things without luck.

Here is my docker-compose.

  mqtt:
    container_name: mqtt
    restart: unless-stopped
    image: eclipse-mosquitto
    privileged: true
    volumes:
      - /mnt/dietpi_userdata/home-automation/mqtt/config/mosquitto.conf:/mosquitto/config/mosquitto.conf
      - /mnt/dietpi_userdata/home-automation/mqtt/log:/mosquitto/log
      - /mnt/dietpi_userdata/home-automation/mqtt/data:/mosquitto/data
      - /etc/localtime:/etc/localtime:ro
    ports:
      - "1883:1883"
      - "9001:9001"
    environment:
      - PUID=1000
      - PGID=1000

I am thinking that I should just leave the logs were they are and try it.

Chris

Unless UID/GID 1000 own that directory /mnt/dietpi_userdata/home-automation/mqtt it won’t work.

Do the log and data directories actually exist?
Try “touching” the files you have told the config to use also.

touch /mnt/dietpi_userdata/home-automation/mqtt/log/mosquitto.log

I was able to get this to work finally! Thank you for all the help. I apologize I didn’t get back sooner, but have been busy.

Chris