关闭Docker时分离卷(以重新启动)

关闭Docker时分离卷(以重新启动),docker,docker-compose,docker-volume,Docker,Docker Compose,Docker Volume,我有一个Docker Compose(文件名为my main db.yml),它创建了一个MySQL 8.0容器: 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

我有一个Docker Compose(文件名为
my main db.yml
),它创建了一个MySQL 8.0容器:

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
要启动此功能,我运行:

docker-compose -f my-main-db.yml up
它运行得很好,我可以连接到它,也可以通过
docker ps
看到它在运行

如果我想停止此容器并保留其中的所有数据,我将使用以下方法将其关闭:

docker-compose -f my-main-db.yml down
但是,如果我想停止此容器并擦除其中的所有数据,以便它启动全新的(干净的),我的理解是我需要分离或删除卷。所以我一直在试图关闭它,就像这样:

docker-compose -f my-main-db.yml down -v
然而,当我这样做,然后启动它备份,我的数据仍然在那里,而我希望有一个全新的(空)数据库


有人能发现我哪里出了问题吗?
docker compose down-v
将删除命名卷,但不会删除作为绑定挂载(如在您的配置中)挂载的目录。为此,您需要自己使用
rm-rf

如果要
docker compose
管理卷,请改用命名卷:

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:
这将在启动堆栈时自动为您分配docker卷,并在您运行
docker compose down-v
时将其删除


例如,如果我在
.env
中有以下内容:

MY_SERVICE_DB_ROOT_PASSWORD=secret
MY_SERVICE_DB_APP_USER=myservice
MY_SERVICE_DB_APP_PASSWORD=secret
我可以打开堆栈:

docker-compose up -d
$ 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
然后连接到mysql并创建一个表:

$ 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
现在,如果我用
-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
…您可以看到Docker已经删除了
Docker\u my-service-db-data
音量。如果我们重新启动堆栈:

docker-compose up -d
$ 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
我们可以连接到mysql,看到这个表已经不存在了, 因为数据库是从头重新创建的:

$ 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]>

谢谢@larsks(+1),但这似乎不起作用。我确保没有运行任何东西(通过
docker ps
),手动删除我的数据文件夹(
my service db data/
),应用您的更改,运行docker compose(与我的问题中发布的命令相同),向其中添加一些数据,关闭它(没有
-v
选项),启动它备份,确认数据仍然存在,关闭它(这次使用
-v
选项),然后重新启动它。数据仍然存在。我遗漏了什么吗?@Hotmeatballshop我已经为这个问题添加了一个完整的例子。如果您看到的是不同的行为,请确保您使用的正是此答案中的
Dockerfile
,并且在使用
-v
放下堆栈时,您看到的是来自Docker的相同消息(特别是
删除卷Docker\u my-service-db-data
),当我没有在
docker compose up
命令中指定
-d
时,这一切都不起作用(当我
docker compose down
使用
-v
时,数据目录永远不会被删除)。但是如果我将
-d
添加到
up
命令中,一切都会正常工作。谢谢