Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker revert changes to container

I'm trying to snapshot my docker container so that I can revert back to a single point in time.

I've looked at docker save and docker export but neither of these seems to do what I'm looking for. Am I missing something?

like image 513
pguardiario Avatar asked Oct 12 '14 05:10

pguardiario


1 Answers

You might want to use docker commit. This command will create a new docker image from one of your docker containers. This way you can easily create a new container later on based on that new image.

Be aware that the docker commit command won't save any data stored in Docker data volumes. For those you need to make backups.


For instance if you are working with the following Dockerfile which declares a volume and will write the date every 5 seconds to two files (one being in the volume, the other not):

FROM base
VOLUME /data
CMD while true; do date >> /data/foo.txt; date >> /tmp/bar.txt; sleep 5; done

Build a image from it:

$ docker build --force-rm -t so-26323286 .

and run a new container from it:

$ docker run -d so-26323286

Wait a bit so that the running docker container have a chance to write the date to the two files a couple of times.

$ docker ps
CONTAINER ID        IMAGE                COMMAND                CREATED             STATUS              PORTS               NAMES
07b094be1bb2        so-26323286:latest   "/bin/sh -c 'while t   5 seconds ago       Up 5 seconds                            agitated_lovelace

Then commit your container into a new image so-26323286:snapshot1:

$ docker commit agitated_lovelace so-26323286:snapshot1

You can now see that you have two images availables:

$ docker images | grep so-26323286
so-26323286                    snapshot1           03180a816db8        19 seconds ago      175.3 MB
so-26323286                    latest              4ffd141d7d6f        9 minutes ago       175.3 MB

Now let's verify that a new container run from so-26323286:snapshot1 would have the /tmp/bar.txt file:

$ docker run --rm so-26323286:snapshot1 cat /tmp/bar.txt
Sun Oct 12 09:00:21 UTC 2014
Sun Oct 12 09:00:26 UTC 2014
Sun Oct 12 09:00:31 UTC 2014
Sun Oct 12 09:00:36 UTC 2014
Sun Oct 12 09:00:41 UTC 2014
Sun Oct 12 09:00:46 UTC 2014
Sun Oct 12 09:00:51 UTC 2014

And witness that such a container does not have any /data/foo.txt file (as /data is a data volume):

$ docker run --rm so-26323286:snapshot1 cat /data/foo.txt
cat: /data/foo.txt: No such file or directory

Finally if you want to access to the /data/foo.txt file which is in the first (still running) container, you can use the docker run --volumes-from option:

$ docker run --rm --volumes-from agitated_lovelace base cat /data/foo.txt
Sun Oct 12 09:00:21 UTC 2014
Sun Oct 12 09:00:26 UTC 2014
Sun Oct 12 09:00:31 UTC 2014
Sun Oct 12 09:00:36 UTC 2014
Sun Oct 12 09:00:41 UTC 2014
Sun Oct 12 09:00:46 UTC 2014
Sun Oct 12 09:00:51 UTC 2014
Sun Oct 12 09:00:56 UTC 2014
Sun Oct 12 09:01:01 UTC 2014
Sun Oct 12 09:01:06 UTC 2014
Sun Oct 12 09:01:11 UTC 2014
Sun Oct 12 09:01:16 UTC 2014
like image 70
Thomasleveil Avatar answered Sep 25 '22 02:09

Thomasleveil