我正在测试许多服务作为容器,通常是通过docker-compose。
因为我是码头工人的新手,所以无论如何这个问题都会显得无趣。
在某些时候,我有一个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会删除卷,每次使用docker stop
停止docker容器时都会发生?
答案 0 :(得分:2)
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的默认行为,并且您已运行一些命令来删除未包含在问题中的这些卷。