How to keep the configuration data persistent?

hi,

I am completely new in the subject of EMQX but I am trying :wink:
I installed EMQX as docker compose but I am missing something - the configuration only is ok until I make command: docker compose rm emqx
But all volumes are on my persistent storage? So why?
here is my compose file:

  emqx:
    image: emqx/emqx:latest
    container_name: emqx
    restart: unless-stopped
    ports:
      - 1883:1883
      - 8083:8083
      - 8084:8084
      - 8883:8883
      - 18083:18083
    volumes:
      - /srv/docker/emqx/data:/opt/emqx/data
      - /srv/docker/emqx/log:/opt/emqx/log
      - /srv/docker/emqx/etc:/opt/emqx/etc

something I am not doing properly - I would appreciate some help :wink:
thx

Hi, @zyghom. I’m not sure whether I fully understand what you mean. I think you may be unable to start EMQX because there is no configuration file in /srv/docker/emqx/etc. This is because the container will replace its original directory with the contents of the host directory after mounting.

So you need to start the EMQX container in a non-persistent way first, and then use docker cp command to copy /opt/emqx/etc/emqx.conf to your host machine.

thank you @Maverick , this I did already by using this helper - full compose file looks like this:


  init-emqx:
    image: emqx/emqx:latest
    container_name: init-emqx
    entrypoint: sh -c
    volumes:
      # We mount this in the container as `/emqx_etc` to not conflict with built-in files
      # at `/opt/emqx/emqx_etc`
      - /srv/docker/emqx/etc:/emqx_etc
    command:
      # Seed the files from the image to our /emqx_etc if marker file not present, skip
      # if the marker file exists (and implicitly exit cleanly with status code 0)
      - |
        if [ ! -f /emqx_etc/.emqx-seed-complete ]; then
          mkdir /emqx_etc ;
          touch /emqx_etc/.emqx-seed-complete ;
          cp -dpR /opt/emqx/etc/* /emqx_etc/ ;
        fi

  emqx:
    image: emqx/emqx:latest
    container_name: emqx
    restart: unless-stopped
    depends_on:
      init-emqx:
        condition: service_completed_successfully
    
    ports:
      - 1883:1883
      - 8083:8083
      - 8084:8084
      - 8883:8883
      - 18083:18083
    volumes:
      - /srv/docker/emqx/data:/opt/emqx/data
      - /srv/docker/emqx/log:/opt/emqx/log
      - /srv/docker/emqx/etc:/opt/emqx/etc

but my problem is: when I remove container by “docker compose rm emqx” I expect my next start to be with my config: users, passwords etc.
but it is not - why?
I don’t have such problems with other apps: influxdb, grafana, even mosquitto

This is because the IP of the container has changed after restarting, so its storage path has also changed.

We list solutions in FAQ, you can look at this question: Why does restarting the EMQX Docker container cause data loss, such as configured rules and resources?

perfect - thank you
could you however translate this docker command line to the entries in docker compose file please?
I am not sure how to tackle it
if you could of course :wink:

thank you

Of Course, note the environment and networks parts below:

services:
  emqx:
    image: emqx:5.1.3
    container_name: emqx
    environment:
    - "EMQX_HOST={Your Network Alias}"
    networks:
      {Your Network Name}:
        aliases:
        - {Your Network Alias}

that will do?

environment:
    - "EMQX_HOST=emqx1.myhost.com"
    networks:
      mynetwork1:
        aliases:
        - emqx1.myhost.com

problem solved (and tested)
thank you @Maverick