The Difference between Docker's save and export commands

Keywords: Docker sudo Linux Ubuntu

ABSTRACT: Recently, during the migration of Docker's mirror image, I met some problems. I don't know how to solve them. I found this good article and solved my problems. It was translated. http://tuhrig.de/difference-between-save-and-export-in-docker/
I've been playing with Docker recently, a virtual technology for application containers and Linux. It's so cool that it only takes a few minutes to create Docker images and containers. All the work is out of the box.

Before I finish my day's work, I hope I can save my work. But between Docker's save and export commands, I was messed up. I don't know the difference between them. So I asked a question on Stack Overflow and got a great response from mbarthelemy. The following is what I discovered:

How Docker works (a brief description)

Docker is based on mirroring. Mirroring is similar to a virtual machine image that already contains files, configurations, and installed programs. Similarly, you can boot multiple mirror instances just like booting a virtual machine. Operating mirrors are called containers. You can modify containers (such as deleting a file), but these changes do not affect the image. However, you can use the docker commit command to transform a running container into a new image.

For instance:

# Like Docker's official hello world example, pull a mirror called busybox
sudo docker pull busybox

# See what mirrors are available locally
# We can see busybox
sudo docker images

# Now let's modify the container for the busybox image
# This time, we create a folder
sudo docker run busybox mkdir /home/test

# Let's see what mirrors we have.
# Note that the container stops after each command is executed
# You can see a busybox container
sudo docker ps -a

# Now, you can submit your changes.
# After submitting, you will see a new mirror busybox-1
#  <CONTAINER ID> is the ID that you just modified the container.
sudo docker commit <CONTAINER ID> busybox-1

# Let's see what mirrors we have.
# We now have busybox and busybox-1 mirrors at the same time.
sudo docker images

# Let's execute the following command to see how the two mirrors differ
sudo docker run busybox [ -d /home/test ] && echo 'Directory found' || echo 'Directory not found'
sudo docker run busybox-1 [ -d /home/test ] && echo 'Directory found' || echo 'Directory not found'

Now, we have two different mirrors (busybox and busybox-1) and a container (with an additional / home/test folder) obtained by modifying the busybox container. Let's see how these changes are persisted.

Export

The Export command is used to persist containers (not mirrors). So we need to get the container ID by the following way:

sudo docker ps -a

Then the export is executed:

sudo docker export <CONTAINER ID> > /home/export.tar

The final result is a 2.7MB Tar file (slightly smaller than using the save command).

Save (Save)

The Save command is used to persist images (not containers). So we need to get the image name by the following way:

sudo docker images

Then the save is executed:

sudo docker save busybox-1 > /home/save.tar

The final result is a 2.8MB Tar file (slightly larger than using the export command).

The difference between them

Now that we have created two Tar files, let's see what they are. First, do a little cleaning - remove all containers and mirrors:

# View all containers
sudo docker ps -a

# Delete them
sudo docker rm <CONTAINER ID>

# Look at all the mirrors
sudo docker images

# Delete them
sudo docker rmi busybox-1
sudo docker rmi busybox

Note: You can use docker rm (dockerps q a) to delete all containers at one time, and docker images - q to delete all mirrors at one time.

Now start importing the container you just exported:

# Import export.tar file
cat /home/export.tar | sudo docker import - busybox-1-export:latest

# View mirroring
sudo docker images

# To check if the import is successful, you start a new container and check if the / home/test directory exists in it.
sudo docker run busybox-1-export [ -d /home/test ] && echo 'Directory found' || echo 'Directory not found'

Import the image using a similar step:

# Import save.tar file
docker load < /home/save.tar

# View mirroring
sudo docker images

# To check if the import is successful, you start a new container and check if the / home/test directory exists in it.
sudo docker run busybox-1 [ -d /home/test ] && echo 'Directory found' || echo 'Directory not found'

So, what's the difference between them? We found that the exported version would be slightly smaller than the original version. That's because history and metadata are lost when exported. Just execute the following commands:

# Display all layers of the mirror
sudo docker images --tree

Execute the command and display the following. As you can see, export-imported mirrors lose all history, while saved-loaded mirrors do not lose history and layers. This means that by importing after export, you will not be able to roll back to the previous layer. At the same time, by persisting the entire image in a save-and-load manner, you can roll back the layer before (you can execute docker tag to roll back the layer before).

vagrant@ubuntu-13:~$ sudo docker images --tree
├─f502877df6a1 Virtual Size: 2.489 MB Tags: busybox-1-export:latest
└─511136ea3c5a Virtual Size: 0 B
  └─bf747efa0e2f Virtual Size: 0 B
    └─48e5f45168b9 Virtual Size: 2.489 MB
      └─769b9341d937 Virtual Size: 2.489 MB
        └─227516d93162 Virtual Size: 2.489 MB Tags: busybox-1:latest

Good luck, Thomas

Posted by dw89 on Sat, 30 Mar 2019 07:54:29 -0700