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

MMT*_*MMT 6 docker docker-compose

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

因为我是 docker 的新手,所以这个问题可能显得很菜鸟。

在某个时候,我运行了一个 docker-compose 堆栈(几个带有卷的容器)。我必须停止容器才能重新使用相同的端口;我是用命令做到的docker stop

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

$ docker-compose start
Starting jenkins   ... done
Starting gitlab-ce ... done
ERROR: No containers to start
Run Code Online (Sandbox Code Playgroud)

我检查了容器,惊讶地发现:

$ docker-compose ps
Name   Command   State   Ports
------------------------------
Run Code Online (Sandbox Code Playgroud)

所以我跑了:

$ 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
Run Code Online (Sandbox Code Playgroud)

...我很震惊地发现我的卷已被重新创建,有效地删除了我的数据。

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

BMi*_*tch 4

Astop不会删除卷。事实上它不能,因为容器仍然存在并因此保持正在使用的卷:

$ 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]
Run Code Online (Sandbox Code Playgroud)

Adocker-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
Run Code Online (Sandbox Code Playgroud)

如果您已停止所有正在运行的容器,并且还运行了修剪,并将选项传递给该修剪以同时删除卷,那么这将删除卷:

$ 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
Run Code Online (Sandbox Code Playgroud)

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