Home > database >  docker-compose volume not persisting database
docker-compose volume not persisting database

Time:01-12

I use docker-compose up -d to start my services then I use docker-compose down to stop it

problem; it seems that my data is not persisted; the "${SQL_INIT}:/docker-entrypoint-initdb.d" is executed ever time

I have setup volume db_data for persistence; docker volume ls returns

local     backend_mariadb-data
local     docker_db_data
local     docker_db_logs

here is my docker-compose

version: "3"
services:
  backend:
    container_name: backend
    image: backend
    restart: always
    build: images/backend/
    env_file: .env
    ports:
      - "8000:8000"
    depends_on:
      - mariadb
    networks:
      - app_network

  mariadb:
    container_name: mariadb
    image: "mariadb:${MARIADB_VERSION}"
    restart: 'always'
    env_file: .env
    volumes:
      - "${SQL_INIT}:/docker-entrypoint-initdb.d"
      - "db_data:${MARIADB_DATA_DIR}"
      - "db_logs:${MARIADB_LOG_DIR}"
    environment:
      MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}"
      MYSQL_DATABASE: "${MYSQL_DATABASE}"
      MYSQL_USER: "${MYSQL_USER}"
      MYSQL_PASSWORD: "${MYSQL_PASSWORD}"
    ports:
      - "3306:3306"
    networks:
      - app_network


volumes:
  db_data:
  db_logs:

networks:
  app_network:

with .env file

MARIADB_VERSION="latest"
MARIADB_DATA_DIR="/var/database/mariadb"
MARIADB_LOG_DIR="/var/logs/mariadb"
MYSQL_DATABASE="app"
MYSQL_USER="app"
MYSQL_PASSWORD="password"
MYSQL_ROOT_PASSWORD="password"
SQL_INIT="./database/dev"

CodePudding user response:

Remove quotes in env file. Docker-Compose treats them as part of the value.

See last Bulletpoint

Besides that the default path for the data is /var/lib/mysql and for logging is /var/log/mysql (but disabled on default)

  •  Tags:  
  • Related