Docker Tutorial 4: Exporting Container and Saving Image

Sik-Ho Tsang
3 min readJul 6, 2018

--

It is important to save the change inside the container. To do this, we need to know the difference of export and save
Export: Export a container
Save: Save an image

So, let’s see the difference. (Sik-Ho Tsang @ Medium)

Docker flow
  1. If we list the image, we can see that we have the ubuntu:18.04 image already.
sudo docker images

Let me run and go into the container.

sudo docker run -itd --name ubuntu ubuntu:1804 /bin/bash
sudo docker exec -it ubuntu bash

2. Let us mkdir to create a new directory aaa. We have some changes in the container. Let’s exit.

mkdir aaa
ls -al
exit

3. Then we can save the image and export the container.

sudo docker save ubuntu > ubuntu_save.tar
sudo docker export ubuntu > ubuntu_export.tar

4. Let us remove the image and container before seeing the differences.

sudo docker stop ubuntu 
sudo docker rm ubuntu
sudo docker rmi ubuntu:18.04

5. Load the image ubuntu_save.tar that we’ve just saved first.

sudo docker load < ubuntu_save.tar

6. If we go into the container, the directory aaa is not here.

7. Exit first.

8. This time, we import the container ubuntu_export.tar that we’ve just export as well.

cat ubuntu_export.tar | sudo docker import - ubuntu:18.04
sudo docker run -itd --name ubuntu ubuntu:18.04 /bin/bash
sudo docker exec -it ubuntu bash

We can see, the directory aaa has been back!!!

--

--

Sik-Ho Tsang
Sik-Ho Tsang

Written by Sik-Ho Tsang

PhD, Researcher. I share what I learn. :) Linktree: https://linktr.ee/shtsang for Twitter, LinkedIn, etc.

Responses (6)