Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker volume not persisting data #1411

Open
philipobrien opened this issue Sep 13, 2019 · 7 comments
Open

docker volume not persisting data #1411

philipobrien opened this issue Sep 13, 2019 · 7 comments

Comments

@philipobrien
Copy link

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
Copy link

wsw70 commented Oct 8, 2019

Please see #1452, that may be the same issue as the one I am having (with a workaround)

@philipobrien
Copy link
Author

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
Copy link

wsw70 commented Oct 8, 2019

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
Copy link

wollet42 commented Nov 5, 2019

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

@johnslemmer
Copy link

@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
Copy link

koenvervloesem commented May 7, 2020

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
Copy link

melyux commented Jul 8, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants