简体   繁体   English

容器停止时由容器创建的Docker卷会发生什么

[英]What happens to docker volumes created by container when the container is stopped

This is my Dockerfile for an spring boot app 这是我的Spring Boot应用程序的Dockerfile

FROM openjdk:8u151-jdk-alpine
VOLUME /tmp
ADD target/classes/application.properties /workdir/application.properties
ADD target/foo-app-2.0.0.jar /workdir/app.jar
WORKDIR /workdir
ENV JAVA_OPTS=""
EXPOSE 8080
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar app.jar" ]

It creates a volume /tmp when the container is started which is stored in /var/lib/docker folder of the host. 启动容器时,它将创建一个卷/tmp ,该卷存储在主机的/var/lib/docker文件夹中。

Is that volume deleted when the container is stopped ie everything from the host machine for that volume is cleaned up? 当容器停止时是否删除了该卷,即从主机清除了该卷的所有内容? If not how do I configure that? 如果没有,我该如何配置?

No, volumes are not deleted automatically (even if the container gets deleted) and you can list them using the docker volume ls command. 不,不会自动删除卷(即使删除了容器),也可以使用docker volume ls命令列出它们。

This could cause the presence of a possibly high number of dangling volumes (volumes not associated to any container) in the host. 这可能会导致主机中可能存在大量的悬空卷(与任何容器无关的卷)。 To remove them you can use this command for example: 要删除它们,您可以使用以下命令,例如:

docker volume rm `docker volume ls -q -f dangling=true`

(Took from here https://coderwall.com/p/hdsfpq/docker-remove-all-dangling-volumes ) (从这里https://coderwall.com/p/hdsfpq/docker-remove-all-dangling-volumes

Adding to @Whites11 answer - 添加到@ Whites11答案-

No, volumes are not deleted automatically/by_default (even if the container gets deleted) and you can list them using docker volume ls command. 不会,不会自动/默认删除卷(即使删除了容器),也可以使用docker volume ls命令列出它们。

However, once your containers are stopped you can remove the volumes associated with them by using rm -v : 但是,一旦容器停止,您可以使用rm -v删除与它们关联的卷:

$ docker stop $CONTAINER_ID && docker rm -v $CONTAINER_ID

Manual - 手册-

Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...] 用法:docker rm [OPTIONS] CONTAINER [CONTAINER ...]

Remove one or more containers 取出一个或多个容器

Options: 选项:

-f, --force Force the removal of a running container (uses SIGKILL) -f,--force强制删除正在运行的容器(使用SIGKILL)

-l, --link Remove the specified link -l,--link删除指定的链接
-v, --volumes Remove the volumes associated with the container -v,--volumes删除与容器关联的卷

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM