Hamid Mohayeji
Hamid Mohayeji

Reputation: 4275

Docker-Compose Creates New Volume After Down & Up (Data is Lost)

I'm using docker-compose to create a redmine instance.

This is my docker-compose.yml, which is based on the official documentation:

version: '3.1'

services:
  redmine:
    image: redmine:4.0.4
    restart: always
    ports:
      - 8890:3000
    environment:
      REDMINE_DB_MYSQL: db
      REDMINE_DB_PASSWORD: password

  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: redmine

Now, whenever I run docker-compose down and then docker-compose up, all of my data (database and files) are deleted, even though I'm not using -v flag, which is necessary for deleting volumes.

Also when I run docker volumes ls, it seems a new volume is created after each docker-compose up.

Is this normal?

How can I use those previous volumes which are created by docker and have a very long hash-based name?

Upvotes: 1

Views: 2196

Answers (3)

Hassan Saeed
Hassan Saeed

Reputation: 7090

you need to assign volume settings by using this

first, you need to delete all old mysql data using

docker-compose down -v

after that add two lines in your docker-compose.yml

volumes:
  - mysql-data:/var/lib/mysql

and

volumes:
  mysql-data:

your final docker-compose.yml will looks like

version: '3.1'

services:
  php:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 80:80
    volumes:
      - ./src:/var/www/html/
  db:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: example
    volumes:
      - mysql-data:/var/lib/mysql

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080
volumes:
  mysql-data:

after that use this command

docker-compose up -d

now your data will persistent and will not be deleted even after using this command

docker-compose down

extra:- but if you want to delete all data then you will use

docker-compose down -v

Upvotes: 1

dschuldt
dschuldt

Reputation: 657

You don't persist the MySQL Container Data on your host. So yes, a new fresh instance is created. This is actual Docker behaviour. Please read the MySQL documentation "Where to Store Data" at MySQL docker Hub repo

Upvotes: 0

SimoMatavulj
SimoMatavulj

Reputation: 584

You havent added volume for database. So database data is lost every time you do docker-compose down. If you want to keep data you can add volume like this:

  version: '3.1'

    services:
      redmine:
        image: redmine:4.0.4
        restart: always
        ports:
          - 8890:3000
        environment:
          REDMINE_DB_MYSQL: db
          REDMINE_DB_PASSWORD: password

      db:
        image: mysql:5.7
        restart: always
        environment:
          MYSQL_ROOT_PASSWORD: password
          MYSQL_DATABASE: redmine
        volumes:
          - db_data:/var/lib/mysql

volumes:
    db_data: {}

Upvotes: 2

Related Questions