How to save a Docker container state

2019-03-09 11:08发布

I'm trying to learn the ins and outs of Docker, and I'm confused by the prospect of saving an image.

I ran the basic Ubuntu image, installed Anaconda Python and a few other things...so now what's the best way to save my progress? Save, commit, export?

None of these seem to work the same way as VirtualBox, which presents an obvious save-state file for your virtual machine.

标签: docker
2条回答
Ridiculous、
2楼-- · 2019-03-09 11:44

The usual way is at least through a docker commit: that will freeze the state of your container into a new image.

Note: As commented by anchovylegend, this is not the best practice, and using a Dockerfile allows you to formally modeling the image content and ensure you can rebuild/reproduce its initial state.

You can then list that image locally with docker images, and run it again.

Example:

$ 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

After that, if you have deployed a registry server, you can push your image to said server.

查看更多
祖国的老花朵
3楼-- · 2019-03-09 11:52

Use a Docker file for these kind of scenarios.

An example case for an Ubuntu image with MongoDB:

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"]

Also see Best practices for writing Dockerfiles.

查看更多
登录 后发表回答