Create Docker Image From Running Container

While working with containers, we keep doing installations and other changes to make our application work. And once we are done, we want to take an image. So in this guide, we will see how to take an image from a running container.

docker container commit

This command is used to take an image of a running container. When the image is taken, all the processes are paused so that the image does not encounter any problems once it is used to create one more container.




--author, -a: Author
--change, -c: Apply Dockerfile instruction to the created image
--message, -m: Commit message
--pause, -p: Pause container during commit


Let's create a container first-

learning-ocean:~ gaurav$ docker container run -it ubuntu:14.04 /bin/bash
root@1b2f7e840cf3:/# cd /tmp

Create few files in the the containers filesystem-

root@1b2f7e840cf3:/# touch 1 2 3 4 5 6 7 8 9
root@1b2f7e840cf3:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@1b2f7e840cf3:/# cd /tmp
root@1b2f7e840cf3:/tmp# ls
1  2  3  4  5  6  7  8  9

come out from container using CTRL+pq

Now let's list the containers-

learning-ocean:~ gaurav$ docker container ls
CONTAINER ID   IMAGE               COMMAND                  CREATED          STATUS          PORTS          NAMES
1b2f7e840cf3   ubuntu:14.04        "/bin/bash"              2 minutes ago    Up 2 minutes                  hungry_hermann
learning-ocean:~ gaurav$

Finally creating the image using the commit command-

learning-ocean:~ gaurav$ docker container commit 1b2f7e840cf3 newubuntu

Now listing the image -

learning-ocean:~ gaurav$ docker image ls
REPOSITORY             TAG                 IMAGE ID       CREATED          SIZE
newubuntu              latest              f74a9dbd167e   4 seconds ago    197MB
ubuntu                 14.04               13b66b487594   3 months ago     197MB

Now let's try creating the container from this newly created image again and check if the file we created exists or not.

learning-ocean:~ gaurav$ docker container run -it newubuntu bash
root@91d9dc9e566d:/# cd /tmp
root@91d9dc9e566d:/tmp# cd ..
root@91d9dc9e566d:/tmp# ls
1  2  3  4  5  6  7  8  9