为什么 `docker-compose up` 会擦除卷?

6

我正在测试很多服务作为容器,通常通过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容器时都会发生吗?


docker stop 不应该擦除卷。运行 docker stop 后,执行 docker ps -a。你的容器还在显示吗? - Ben Mohorc
是的,尽管当我浏览卷 ls /var/lib/docker/volumes/dockerpipeline_gitlab_config/_data 时,数据已经消失了。 - MMT
1个回答

4

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

如果您已经停止所有正在运行的容器,并且执行了一次清理操作(prune),并传递了删除卷的选项,那么这将删除这些卷:

$ 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的默认行为,您可能已运行一些命令以删除这些卷,但这些命令并未在问题中提到。

在你的回答之后,我更仔细地检查了卷,并发现新卷和缺失的卷几乎有相同的名称。哎呀,我还以为我的数据都丢了。 - MMT

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接