Skip to content

Instantly share code, notes, and snippets.

@dasgoll
Last active November 16, 2023 23:05
Show Gist options
  • Save dasgoll/476ecc7a057ac885f0be to your computer and use it in GitHub Desktop.
Save dasgoll/476ecc7a057ac885f0be to your computer and use it in GitHub Desktop.
That’s one of the real strengths of Docker: the ability to go back to a previous commit. The secret is simply to docker tag the image you want.
Here’s an example. In this example, I first installed ping, then committed, then installed curl, and committed that. Then I rolled back the image to contain only ping:
$ docker history imagename
IMAGE CREATED CREATED BY SIZE
f770fc671f11 12 seconds ago apt-get install -y curl 21.3 MB
28445c70c2b3 39 seconds ago apt-get install ping 11.57 MB
8dbd9e392a96 7 months ago 131.5 MB
$ docker tag 2844 imagename # <-- that's the secret right there
$ docker history imagename
IMAGE CREATED CREATED BY SIZE
28445c70c2b3 56 seconds ago apt-get install ping 11.57 MB
8dbd9e392a96 7 months ago 131.5 MB
==========================
But maybe the best things are rollbacks: since every image contains all of its building steps, we can easily go back to a previous step if we want so. This can be done tagging a certain layer. Let’s take a look at image b again:
$ docker history test/b
IMAGE CREATED CREATED BY SIZE
c0daf4be2ed4 3 hours ago /bin/sh -c touch /opt/b.txt 8 B
9977b78fbad7 3 hours ago /bin/sh -c apt-get install -y apache2 54.17 MB
e83b3bf07b42 3 hours ago /bin/sh -c apt-get update 20.67 MB
9cd978db300e 3 months ago /bin/sh -c #(nop) ADD precise.tar.xz in / 204.4 MB
6170bb7b0ad1 3 months ago /bin/sh -c #(nop) MAINTAINER Tianon Gravi <ad 0 B
511136ea3c5a 10 months ago 0 B
If we want to make a rollback and remove the last layer (maybe the file should be called c.txt instead of b.txt) we can do so by tagging the layer 9977b78fbad7:
1
$ docker tag 9977b test/b
Let’s take a look at the new history:
$ docker history test/b
IMAGE CREATED CREATED BY SIZE
9977b78fbad7 3 hours ago /bin/sh -c apt-get install -y apache2 54.17 MB
e83b3bf07b42 3 hours ago /bin/sh -c apt-get update 20.67 MB
9cd978db300e 3 months ago /bin/sh -c #(nop) ADD precise.tar.xz in / 204.4 MB
6170bb7b0ad1 3 months ago /bin/sh -c #(nop) MAINTAINER Tianon Gravi <ad 0 B
511136ea3c5a 10 months ago 0 B
Our last layer is gone and with the layer the text file b.txt!
@fanjieqi
Copy link

fanjieqi commented Feb 6, 2018

Thanks~

@tsamis-vasilis
Copy link

tsamis-vasilis commented Jun 14, 2018

Is there a way to tag layers whose Id is <missing> when executing "docker history imagename" ? e.g.: stackoverflow issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment