eclipse / mosquitto

Eclipse Mosquitto - An open source MQTT broker
https://mosquitto.org
Other
8.93k stars 2.37k forks source link

docker volume not persisting data #1411

Open philipobrien opened 5 years ago

philipobrien commented 5 years ago

I'm spinning the image up with compose as follows

mqtt-broker:
    image: eclipse-mosquitto
    ports:
      - "1883:1883"
      - "9001:9001"
    networks:
      - web
      - default
    volumes:
      - /home/ubuntu/src/carelink_mqtt_config/mosquitto-staging.conf:/mosquitto/config/mosquitto.conf
      - /home/ubuntu/data/mosquitto-data:/mosquitto/data

with the following set in the config file

persistence true
persistence_location /mosquitto/data/

but although /mosquitto/data is indeed being written to in the container (i.e. mosquitto.db being created there), nothing is appearing in the synced volume on the host. Inspecting the container mounts shows

dockerinspect -f '{{ .Mounts }}' 1df9ae4ffd1dh
[{bind  /home/ubuntu/src/carelink_mqtt_config/mosquitto-staging.conf /mosquitto/config/mosquitto.conf  rw true rprivate} {volume cacf442a2aa3dba91f0f4cdbd70960e96dbe183c467fdddf710a057e9a862dd4 /var/lib/docker/volumes/cacf442a2aa3dba91f0f4cdbd70960e96dbe183c467fdddf710a057e9a862dd4/_data /mosquitto/data local rw true } {volume 3162143b9f0451ba5a60375284fa415a1d669744cc4154de66a28b744b94a523 /var/lib/docker/volumes/3162143b9f0451ba5a60375284fa415a1d669744cc4154de66a28b744b94a523/_data /mosquitto/log local rw true }]

I can't figure it out because I'm following the same process for mounting volumes for postgres, mongo, and elasticsearch and they are all working as expected

wsw70 commented 4 years ago

Please see https://github.com/eclipse/mosquitto/issues/1452, that may be the same issue as the one I am having (with a workaround)

philipobrien commented 4 years ago

I don't think it is as the same. For me I can see in the mosquitto logs Saving in-memory database to /mosquitto/data/mosquitto.db, and then within the mosquitto container mosquitto.db is being written to /mosquitto/data, but although this is mapped to a local dir (/home/ubuntu/data/mosquitto-data) nothing appears in the local dir

wsw70 commented 4 years ago

Ah this is indeed strange - I also have all kind of docker files on my server and mapping works the way you describe.

Maybe you could try to map /mosquitto to /home/ubuntu/data/mosquitto to have all the directories in one mapping and see whether you find the file there?

wollet42 commented 4 years ago

had a similar issue. In my case stopping and removing the container did the trick

johnslemmer commented 4 years ago

@philipobrien

After struggling with things mentioned in this issue. And like those mentioned in #1452 I arrived at something that works for me:

# docker-compose.yml
version: '3.7'
services:
  broker:
    image: eclipse-mosquitto
    restart: always
    ports:
      - "1883:1883"
      - "9001:9001"
    volumes:
      - type: bind
        source: ./mosquitto/config/mosquitto.conf
        target: /mosquitto/config/mosquitto.conf
      - type: bind
        source: ./mosquitto/data/
        target: /var/lib/mosquitto/
# mosquitto.conf
persistence true
persistence_location /var/lib/mosquitto/

And making sure on the host that I ran:

sudo chown 1883:1883 ./mosquitto/data

Doing all this and it seemed to work for me.

koenvervloesem commented 4 years ago

I struggled with it too. After a lot of experimenting, I found the solution was quite simple. Just create the three directories:

mkdir -p ./containers/mosquitto/{config,data,log}

Then in ./containers/mosquitto/config/mosquitto.conf comes:

port 1883
listener 9001
protocol websockets
persistence true
persistence_location /mosquitto/data/
log_dest file /mosquitto/log/mosquitto.log

The Docker Compose file then becomes:

version: '3.7'

services:
  mosquitto:
    image: eclipse-mosquitto
    container_name: mosquitto
    restart: always
    ports:
      - "1883:1883"
      - "9001:9001"
    volumes:
      - ./containers/mosquitto/config:/mosquitto/config
      - ./containers/mosquitto/data:/mosquitto/data
      - ./containers/mosquitto/log:/mosquitto/log
      - /etc/localtime:/etc/localtime:ro
    user: "1000:1000"

You need to specify the config, data and log directory separately. Also note the user: "1000:1000" at the end, which is needed so the container can write to the data and log directories. Replace this by the user ID and group ID of your current user.

After this, you have data persistence and logs on the host machine.

melyux commented 1 year ago

Worked! Why does it require them to be separate mounts?