简体   繁体   中英

Detaching volumes (for a fresh restart) when shutting down Docker

I have a Docker Compose (file is named my-main-db.yml ) that creates a MySQL 8.0 container:

version: "3.7"
services:
  my-main-db:
    env_file:
      - .env
    image: mysql:8
    container_name: my-main-db
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    ports:
      - 3306:3306
    environment:
      MYSQL_ROOT_PASSWORD: $MY_SERVICE_DB_ROOT_PASSWORD
      MYSQL_DATABASE: my_service_db_local
      MYSQL_USER: $MY_SERVICE_DB_APP_USER
      MYSQL_PASSWORD: $MY_SERVICE_DB_APP_PASSWORD
    volumes:
      - ./my-service-db-data:/var/lib/mysql

To fire this up I run:

docker-compose -f my-main-db.yml up

And it runs just fine, I can connect to it and also see it running via docker ps .

If I want to stop this container and keep all the data inside of it, I shut it down with:

docker-compose -f my-main-db.yml down

But if I want to stop this container and erase all the data inside of it, so that it starts up brand new (and clean), my understanding is that I need to detach or delete the volume. So I have been trying to shut it down like so:

docker-compose -f my-main-db.yml down -v

However, when I do this, and then start it back up, my data is still all there, whereas I was hoping for a brand new (empty) database.

Can anyone spot where I'm going awry?

docker-compose down -v will delete named volumes , but it won't delete directories you've mounted as bind mounts (as in your configuration). For that you would need to use rm -rf yourself.

If you want docker-compose to manage the volumes, used a named volume instead:

version: "3.7"
services:
  my-main-db:
    env_file:
      - .env
    image: mysql:8
    container_name: my-main-db
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    ports:
      - 3306:3306
    environment:
      MYSQL_ROOT_PASSWORD: $MY_SERVICE_DB_ROOT_PASSWORD
      MYSQL_DATABASE: my_service_db_local
      MYSQL_USER: $MY_SERVICE_DB_APP_USER
      MYSQL_PASSWORD: $MY_SERVICE_DB_APP_PASSWORD
    volumes:
      - my-service-db-data:/var/lib/mysql

volumes:
  my-service-db-data:

This will allocate a docker volume for you automatically when starting up the stack, and will remove it when you run docker-compose down -v .


For example, if I have the following in .env :

MY_SERVICE_DB_ROOT_PASSWORD=secret
MY_SERVICE_DB_APP_USER=myservice
MY_SERVICE_DB_APP_PASSWORD=secret

I can bring up the stack:

docker-compose up -d

And then connect to mysql and create a table:

$ mysql -h docker -u myservice -psecret my_service_db_local
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.23 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [my_service_db_local]> create table testtable (id int);
Query OK, 0 rows affected (0.065 sec)

MySQL [my_service_db_local]> show tables;
+-------------------------------+
| Tables_in_my_service_db_local |
+-------------------------------+
| testtable                     |
+-------------------------------+
1 row in set (0.004 sec)

MySQL [my_service_db_local]> ^DBye

Now if I bring down the stack with -v :

$ docker-compose down -v
Stopping my-main-db ... done
WARNING: Found orphan containers (docker_test_1) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
Removing my-main-db ... done
Removing network docker_default
Removing volume docker_my-service-db-data

...you can see that Docker has removed the docker_my-service-db-data volume. If we restart the stack:

$ docker-compose up -d
Creating network "docker_default" with the default driver
Creating volume "docker_my-service-db-data" with default driver
WARNING: Found orphan containers (docker_test_1) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
Creating my-main-db ... done

We can connect to mysql and see that the table no longer exists, because the database has been re-created from scratch:

$ mysql -h docker -u myservice -psecret my_service_db_local
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.23 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [my_service_db_local]> show tables;
Empty set (0.004 sec)

MySQL [my_service_db_local]>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM