我执行了以下操作并丢失了 Docker 容器中所有更改的数据。
-
docker build -t <name:tag> .
-
docker run *-p 8080:80* --name <container_name> <name:tag>
-
docker exec
(导入并处理一些文件,启动服务器来托管它们)
然后我想在不同的端口上运行它。 docker stop
& docker run
不起作用。相反,我做了
-
docker stop
-
docker rm <container_name>
-
docker run
(与之前相同的参数)
重启后我看到1-3的容器中发生的变化消失了,只好重新运行导入。
下次如何正确操作?
最佳答案
您需要做的是从您在更改后刚刚停止的容器中构建镜像。因为您的旧命令仍在使用没有新更改的旧图像(您在容器中进行了更改,而您刚刚停止而不是在图像中)
docker commit --help
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
docker commit -a me new_nginx myrepo/nginx:latest
然后您可以使用刚刚构建的新图像启动容器
但如果您不想使用所做的更改创建镜像(例如您不想在镜像中放置包含密码的配置),您可以使用卷挂载
docker run -d -P --name web -v/src/webapp:/webapp training/webapp python app.py
此命令将主机目录/src/webapp 挂载到位于/webapp 的容器中。如果路径/webapp 已经存在于容器的镜像中,则/src/webapp 挂载会覆盖但不会删除预先存在的内容。移除挂载后,即可再次访问内容。这与 mount 命令的预期行为一致。
关于docker - 如何在不丢失更改的情况下停止/重新启动 docker 容器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41067032/