mysqldockerdocker-composedocker-volumeadminer

Docker mysql container doesn't persist data despite using volumes. Why?


I am running two docker containers.

I want to persist my database, therefore I use volumes in my docker-compose file. Still I lose data whenever I restart the containers (docker-compose down and up).

What I am missing? (I am on ubuntu 20.04)

services:
  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080
  db:
    image: mysql:latest
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: 'example' # TODO: Change this      
    volumes:
      - "./config/my.conf:/etc/mysql/conf.d/config-file.cnf"
      - "./data:/var/lib/mysql:rw"
      

Solution

  • Use docker-compose stop instead of docker-compose down.

    From the documentation:

    docker-compose down: Stops containers and removes containers, networks, volumes, and images created by up.