docker revert changes to container docker revert changes to container docker docker

docker revert changes to container


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 baseVOLUME /dataCMD 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 psCONTAINER ID        IMAGE                COMMAND                CREATED             STATUS              PORTS               NAMES07b094be1bb2        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-26323286so-26323286                    snapshot1           03180a816db8        19 seconds ago      175.3 MBso-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.txtSun Oct 12 09:00:21 UTC 2014Sun Oct 12 09:00:26 UTC 2014Sun Oct 12 09:00:31 UTC 2014Sun Oct 12 09:00:36 UTC 2014Sun Oct 12 09:00:41 UTC 2014Sun Oct 12 09:00:46 UTC 2014Sun 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.txtcat: /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.txtSun Oct 12 09:00:21 UTC 2014Sun Oct 12 09:00:26 UTC 2014Sun Oct 12 09:00:31 UTC 2014Sun Oct 12 09:00:36 UTC 2014Sun Oct 12 09:00:41 UTC 2014Sun Oct 12 09:00:46 UTC 2014Sun Oct 12 09:00:51 UTC 2014Sun Oct 12 09:00:56 UTC 2014Sun Oct 12 09:01:01 UTC 2014Sun Oct 12 09:01:06 UTC 2014Sun Oct 12 09:01:11 UTC 2014Sun Oct 12 09:01:16 UTC 2014


Here is an example of how to do it with the hello-world image from Docker Hub

First run the hello-world image, thereby downloading the image:

docker run hello-world 

Then get the hash of the image you want to get the has of

docker history hello-world

You will see something like:

IMAGE               CREATEDfce289e99eb9        15 months ago 

fce289e99eb9 is your hash-code.

To tag this image, you run:

docker tag fce289e99eb9 hello-world:SNAPSHOT-1.0

To list all the tags for a repository, use:

docker image ls hello-world

And you will get something like:

REPOSITORY          TAG                 IMAGE ID            CREATED    SIZE hello-world         SNAPSHOT-1.0        fce289e99eb9        15 months ago       1.84kB hello-world         latest              fce289e99eb9        15 months ago       1.84kB