如何保存Docker容器状态

时间:2017-06-11 05:40:56

标签: docker

我正在尝试学习Docker的细节,我对保存图像的前景感到困惑。

我运行了基本的Ubuntu映像,安装了Anaconda Python和其他一些东西......那么现在保存进度的最佳方法是什么?保存,提交,导出?

这些似乎与VirtualBox的工作原理不同,VirtualBox为您的虚拟机提供了一个明显的保存状态文件。

3 个答案:

答案 0 :(得分:25)

通常的方法是至少通过docker commit:将容器的状态冻结为新图像。

注意:commentedanchovylegend,这不是最佳做法,使用Dockerfile可以正式建模图像内容,并确保您可以重建/重现其初始状态。

然后,您可以使用docker images在本地列出该图像,然后再次运行。

示例:

$ docker ps

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS              NAMES
c3f279d17e0a        ubuntu:12.04        /bin/bash           7 days ago          Up 25 hours                            desperate_dubinsky
197387f1b436        ubuntu:12.04        /bin/bash           7 days ago          Up 25 hours                            focused_hamilton

$ docker commit c3f279d17e0a  svendowideit/testimage:version3

f5283438590d

$ docker images

REPOSITORY                        TAG                 ID                  CREATED             SIZE
svendowideit/testimage            version3            f5283438590d        16 seconds ago      335.7 MB

之后,if you have deployed a registry server,您可以将图像推送到所述服务器。

答案 1 :(得分:3)

将Docker文件用于这些场景。

使用MongoDB的Ubuntu映像的示例:

FROM ubuntu
MAINTAINER Author name

RUN apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
RUN echo "deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist    10gen" | tee -a /etc/apt/sources.list.d/10gen.list
RUN apt-get update
RUN apt-get -y install apt-utils
RUN apt-get -y install mongodb-10gen

#RUN echo "" >> /etc/mongodb.conf

CMD ["/usr/bin/mongod", "--config", "/etc/mongodb.conf"]

另见 Best practices for writing Dockerfiles

答案 2 :(得分:0)

可以使用docker commit命令(但不推荐)。
您可以检查以下清晰的示例:
https://phoenixnap.com/kb/how-to-commit-changes-to-docker-image