为什么“docker compose up”会删除卷?

为什么“docker compose up”会删除卷?,docker,docker-compose,Docker,Docker Compose,我正在测试很多作为容器的服务,通常是通过docker compose 因为我是docker的新手,不管怎样,这个问题可能会显得毫无意义 在某个时候,我运行了一个docker compose堆栈(只有几个容器有卷)。我不得不停止集装箱重复使用相同的港口;我是用docker stop命令完成的 当我准备再次启动容器时,我做到了: $ docker-compose start Starting jenkins ... done Starting gitlab-ce ... done ERROR:

我正在测试很多作为容器的服务,通常是通过docker compose

因为我是docker的新手,不管怎样,这个问题可能会显得毫无意义

在某个时候,我运行了一个docker compose堆栈(只有几个容器有卷)。我不得不停止集装箱重复使用相同的港口;我是用
docker stop
命令完成的

当我准备再次启动容器时,我做到了:

$ docker-compose start
Starting jenkins   ... done
Starting gitlab-ce ... done
ERROR: No containers to start
我检查了集装箱,惊讶地发现:

$ docker-compose ps
Name   Command   State   Ports
------------------------------
所以我跑了:

$ docker-compose up
Creating volume "dockerpipelinejenkins_jenkins_home" with default driver
Creating volume "dockerpipelinejenkins_gitlab_logs" with default driver
Creating volume "dockerpipelinejenkins_gitlab_data" with default driver
Creating volume "dockerpipelinejenkins_gitlab_config" with default driver
Creating dockerpipelinejenkins_jenkins_1   ... done
Creating dockerpipelinejenkins_gitlab-ce_1 ... done
Attaching to dockerpipelinejenkins_jenkins_1, dockerpipelinejenkins_gitlab-ce_1
。。。我震惊地发现我的卷被重新创建,有效地删除了我的数据


为什么docker compose会删除卷?每次我使用
docker stop
停止docker容器时都会发生这种情况吗?

A
stop
不会删除卷。事实上,它不能,因为容器仍然存在,因此在使用中保持体积:

$ docker run --name test-vol -v test-vol:/data busybox sleep 10

$ docker volume rm test-vol
Error response from daemon: unable to remove volume: remove test-vol: volume is in use - [1de0add7a8dd6e083326888bc02d9954bfc7b889310ac238c34ac0a5b2f16fbf]
docker compose down
将删除容器,但除非您明确传递该选项,否则不会删除卷:

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

By default, the only things removed are:

- Containers for services defined in the Compose file
- Networks defined in the `networks` section of the Compose file
- The default network, if one is used

Networks and volumes defined as `external` are never removed.

Usage: down [options]

Options:
    --rmi type          Remove images. Type must be one of:
                        'all': Remove all images used by any service.
                        'local': Remove only images that don't have a custom tag
                        set by the `image` field.
    -v, --volumes       Remove named volumes declared in the `volumes` section
                        of the Compose file and anonymous volumes
                        attached to containers.
    --remove-orphans    Remove containers for services not defined in the
                        Compose file
如果已停止所有正在运行的容器,并且还运行了修剪,并将选项传递给该修剪以同时删除卷,则将删除卷:

$ docker system prune --help

Usage:  docker system prune [OPTIONS]

Remove unused data

Options:
  -a, --all             Remove all unused images not just dangling ones
      --filter filter   Provide filter values (e.g. 'label=<key>=<value>')
  -f, --force           Do not prompt for confirmation
      --volumes         Prune volumes
$docker系统修剪--帮助
用法:docker系统修剪[选项]
删除未使用的数据
选项:
-a、 --全部删除所有未使用的图像,而不仅仅是悬挂的图像
--筛选器筛选器提供筛选器值(例如“label==”)
-f、 --强制不提示确认
--卷修剪卷

简而言之,您所描述的不是docker的默认行为,您已经运行了一些命令来删除问题中未包含的卷。

docker stop
不应删除卷。运行
docker stop
后,运行
docker ps-a
。您的容器还在显示吗?是的,尽管当我浏览卷
ls/var/lib/docker/volumes/dockerpipeline\u gitlab\u config/\u data
时,数据已经确定在您的回答之后,我更仔细地检查了卷,发现新卷和缺少的卷的名称几乎相同。我以为我的数据都不见了。