There are situations when you have setup a container with necessary softwares or other artifacts and would like to use the same container with the installed stuff intact and not run those same commands everytime you create the container, in such cases you got to commit the steps ( or technically bind all the images ) into a new image. This is where the docker commit is useful.

Demo

Start a container

root@ubuntu14-04:~#
root@ubuntu14-04:~# docker run -it ubuntu /bin/bash
root@4691913a9f0f:/#
root@4691913a9f0f:/#

Pull ubuntu latest updates and also download the ping command

root@4691913a9f0f:/# apt-get update

Get:1 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]
Get:2 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [95.7 kB]
Get:3 http://archive.ubuntu.com/ubuntu xenial-security InRelease [94.5 kB]
...
...	


root@4691913a9f0f:/# apt-get install iputils-ping

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libffi6 libgmp10 libgnutls-openssl27 libgnutls30 libhogweed4 libidn11 libnettle6 libp11-kit0 libtasn1-6
Suggested packages:
  gnutls-bin
The following NEW packages will be installed:
  iputils-ping libffi6 libgmp10 libgnutls-openssl27 libgnutls30 libhogweed4 libidn11 libnettle6 libp11-kit0 libtasn1-6
0 upgraded, 10 newly installed, 0 to remove and 15 not upgraded.
Need to get 1303 kB of archives.
After this operation, 3778 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://archive.ubuntu.com/ubuntu xenial/main amd64 libgmp10 amd64 2:6.1.0+dfsg-2 [240 kB]
Get:2 http://archive.ubuntu.com/ubuntu xenial/main amd64 libnettle6 amd64 3.2-1 [93.2 kB]
...
...

Try the command is working or not

root@4691913a9f0f:/# ping google.com
PING google.com (216.58.197.78) 56(84) bytes of data.
64 bytes from maa03s21-in-f78.1e100.net (216.58.197.78): icmp_seq=1 ttl=53 time=35.7 ms
64 bytes from maa03s21-in-f78.1e100.net (216.58.197.78): icmp_seq=2 ttl=53 time=27.4 ms

Now this container has the latest update when compared to the older image. To persist these changes, need to create a new image by using commit command.

root@ubuntu14-04:/var/lib/docker/containers# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
4691913a9f0f        ubuntu:latest       "/bin/bash"         5 minutes ago       Up 5 minutes                            evil_leakey



root@ubuntu14-04:/var/lib/docker/containers# docker commit 4691913a9f0f
bc98503949b1f0729196dc895e8caa7fef7fef8d7163ecacddd09543bfdf4ac2

bc98503949b1f0729 is a new image created which could be checked by issuing below command.

root@ubuntu14-04:/home/kmaringanti# docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
<none>              <none>              bc98503949b1        53 seconds ago      170.2 MB
ubuntu              latest              56465e1e45d2        4 weeks ago         127.2 MB
hello-world         latest              f0cb9bdcaa69        4 months ago        1.848 kB

In case you seek for versioning the newly created image, below command is issued to tag it.

root@ubuntu14-04:/var/lib/docker/containers# docker commit 4691913a9f0f ubuntu:v1
d6dafb32f36a54667d11723102fe4f6db98d9a36b75ae940e679d2a71e309b48

check the below output

root@ubuntu14-04:/home/kmaringanti# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              VIRTUAL SIZE
ubuntu              v1                  d6dafb32f36a        6 seconds ago        170.2 MB
<none>              <none>              bc98503949b1        About a minute ago   170.2 MB
ubuntu              latest              56465e1e45d2        4 weeks ago          127.2 MB
hello-world         latest              f0cb9bdcaa69        4 months ago         1.848 kB

Now run the container with the latest image then you will see the difference with all those changes intact.

root@ubuntu14-04:~# docker run -it ubuntu:v1 /bin/bash
root@c028903876a1:/#
root@c028903876a1:/#
root@c028903876a1:/# ping google.com
PING google.com (216.58.197.78) 56(84) bytes of data.
64 bytes from maa03s21-in-f78.1e100.net (216.58.197.78): icmp_seq=1 ttl=53 time=38.7 ms
^C
--- google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 38.763/38.763/38.763/0.000 ms