Differences between the save and export commands of Docker

Source: Internet
Author: User
Tags docker ps

I recently am playing with Docker, an application container and a virtual technology for Linux. It is so cool that it takes only a few minutes to create a Docker image and container. All work is out-of-the-box.

Before I finish my day's work, I hope to save my work. But I am messy between the save and export commands of Docker. I don't know what is the difference between them. So I asked StackOverflow a question and then got a great reply from mbarthelemy.

New virtualization options for the open-source project Docker and Red Hat

Dockerlite: lightweight Linux Virtualization

Detailed explanation of the entire process of building Gitlab CI for Docker

What is the difference between Docker and a normal Virtual Machine?

Docker will change everything

Here is what I found:

How Docker works (simple description)

Docker is image-based. An image is similar to a virtual machine image that contains files, configurations, and installed programs. Similarly, you can start multiple image instances just like starting a virtual machine. A running image is called a container. You can modify the container (such as deleting a file), but these changes do not affect the image. However, you usedocker commit <container-id> <image-name>Command to convert a running container into a new image.

For example:

# Pull an image named busybox like the official Docker hello world example
Sudo docker pull busybox

# Check which images are available locally
# We can see busybox
Sudo docker images

# Now let's modify the container of the busybox Image
# This time, we create a folder
Sudo docker run busybox mkdir/home/test

# Let's see which images we have.
# Note that the container stops after each command is executed
# A busybox container is displayed.
Sudo docker ps-

# Now, you can submit the changes.
# A new image busybox-1 will be displayed after submission
# <Container id> is the ID obtained after the CONTAINER is modified.
Sudo docker commit busybox-1

# Check which images we have.
# We now have both busybox and busybox-1 images.
Sudo docker images

# Run the following command to check whether the two images are different.
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 images (busybox and busybox-1) and a container by modifying the busybox container (one more/home/test folder ). Let's take a look at how these modifications are made persistently.

Export (Export)

The Export command is used for persistence containers (not images ). Therefore, we need to obtain the container ID using the following methods:

Sudo docker ps-

Then execute export:

Sudo docker export <container id>/home/export.tar

 

The final result is a Tar file of 2 to 7 MB (slightly smaller than the save command ).

 

Save)

 

The Save command is used to persist images (not containers ). Therefore, we need to obtain the image name using the following method:

 

Sudo docker images

 

Then run save:

 

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

 

 

The final result is a Tar file of 2 to 8 Mb size (slightly larger than the export command ).

 

Differences between them

 

Now we have created two Tar files. Let's see what they are. First, perform a small cleanup -- delete all containers and images:

 

# View all containers
Sudo docker ps-

 

# Delete them
Sudo docker rm <container id>

 

# View all images
Sudo docker images

 

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

 

 

 

You can use docker rm $ (docker ps-q-a) to delete all containers at a time. docker rmi $ (docker images-q) deletes all images at a time.

 

Import the exported container now:

 

# Import the export.tar File
Cat/home/export.tar | sudo docker import-busybox-1-export: latest

 

# Viewing images
Sudo docker images

 

# To check whether the import is successful, start a new container and check whether the/home/test directory exists (yes)
Sudo docker run busybox-1-export [-d/home/test] & echo 'Directory found' | echo 'Directory not Found'

 

Follow these steps to import an image:

 

# Import the save.tar File
Docker load

 

# Viewing images
Sudo docker images

 

# To check whether the import is successful, start a new container and check whether the/home/test directory exists (yes)
Sudo docker run busybox-1 [-d/home/test] & echo 'Directory found' | echo 'Directory not Found'

 

So what is the difference between them? We found that the exported version is slightly smaller than the original version. This is because historical data and metadata are lost after export. Run the following command:

 

# Display all layers of an image)
Sudo docker images -- tree

 

Run the command to display the following content. As you can see, the exported and imported (exported-imported) images will lose all the history, save and load (saveed-loaded) the history and layer are not lost ). This means that you will not be able to roll back to the previous layer after export. At the same time, you can use the SAVE and then load method to persist the entire image, you can perform layer rollback (you can executedocker tag <LAYER ID> <IMAGE NAME>).

 

Vagrant @ Ubuntu-13 :~ $ Sudo docker images -- tree
Capacity-f502877df6a1 Virtual Size: 2.489 MB Tags: busybox-1-export: latest
─ ── 136ea3c5a Virtual Size: 0 B
─ ── Bf747efa0e2f Virtual Size: 0 B
Capacity-48e5f45168b9 Virtual Size: 2.489 MB
2017-769b9341d937 Virtual Size: 2.489 MB
2017-227516d900002 Virtual Size: 2.489 MB Tags: busybox-1: latest

 

 

 

 

Docker details: click here
Docker: click here

This article permanently updates the link address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.