Home » Development and Build » Development Environment Setup » How to save / commit changes to docker image ?

How to save / commit changes to docker image ?

This post is in continuation with our previous post, “How to run Ubuntu 18.04 in docker container ?” , as mentioned in previous post, suppose you get to console and does some work like creating files, installing some packages etc, and “exit” from that shell and after some time you again started the same Ubuntu image, you will see all the work you done previously has been lost, and you have to start all over again.

We will first show if you didn’t saved, how you end up loosing the data.. and then will show how you can save the data in container..

Check which are the docker images installed on your system,

$ sudo docker images
[sudo] password for devlab: 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              9140108b62dc        3 weeks ago         72.9MB
ubuntu              18.04               56def654ec22        3 weeks ago         63.2MB
hello-world         latest              bf756fb1ae65        9 months ago        13.3kB

Here, we will use only Ubuntu 18.04 image, hence login to the same container as,

$ sudo docker run -it 56def654ec22 /bin/bash

Now, lets go to home directory and create some temporary files as,

root@022d00248953:/# cd home/
root@022d00248953:/home# ls
root@022d00248953:/home# echo "this is test file" > helloworld.txt
root@022d00248953:/home# ls
helloworld.txt
root@022d00248953:/home# cat helloworld.txt 
this is test file

Now, lets check on another console,

$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
022d00248953        56def654ec22        "/bin/bash"         3 minutes ago       Up 3 minutes                            loving_colden

We can see that the Ubuntu image is running.. now lets exit from the Ubuntu container console using exit command,

root@022d00248953:/home# exit
exit

Now, we will be back on our host machines console.. now lets login again and see if our previous work is still there or not..

$ sudo docker run -it 56def654ec22 /bin/bash
root@375e83b53588:/# cd /home/
root@375e83b53588:/home# ls

as you can now see, “ls” command returned empty.. but actually we had created “helloworld.txt” file in our container as seen above.. so we can see that all our data is lost .. so what to do ???

How to save / commit the data in container ?

The answer to our problem above is that, we need to manually save or commit the data you worked on in container so as once you login again, your all things like packages installed, code or files gets preserved in your django container..

Now, lets say we have created “helloworld.txt” in our container, and then before exiting from the container

$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
375e83b53588        56def654ec22        "/bin/bash"         9 minutes ago       Up 9 minutes                            hopeful_euler

here, our working docker image container ID is “375e83b53588” , so we need to save the data using “CONTAINER ID” as,

$ sudo docker commit "CONTAINER_ID" "new image name"
sha256:a4a181159e32bed11a441299c656d5624591d5e82b224a1c592de37c5989d227
$ sudo docker commit 375e83b53588 ubuntu
sha256:a4a181159e32bed11a441299c656d5624591d5e82b224a1c592de37c5989d227

So, now the data will be saved and we will see a new image got created as,

$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              a4a181159e32        7 seconds ago       63.2MB
ubuntu              <none>              9140108b62dc        3 weeks ago         72.9MB
ubuntu              18.04               56def654ec22        3 weeks ago         63.2MB
hello-world         latest              bf756fb1ae65        9 months ago        13.3kB

As you can see above, new docker image with “IMAGE ID” “a4a181159e32” got created.. so if you login to this new image as,

$ sudo docker run -it a4a181159e32 /bin/bash

you will see your data is commited..

root@23578a563bbb:/# ls /home/
helloworld.txt

Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

Leave a Comment