Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between the save and export commands of Docker

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "what is the difference between the save and export commands of Docker". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I've been playing with Docker, 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 to keep my work. But I was messed up between Docker's save and export commands. I don't know the difference between them. So, I asked a question on StackOverflow and got a great reply from mbarthelemy. Here's what I found:

How does Docker work (brief explanation)

Docker is based on mirrors. An image is similar to a virtual machine image that already contains files, configurations, and installed programs. Similarly, you can start multiple mirror 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 will not affect the image. However, you can use the docker commit command to turn a running container into a new image.

For example:

# like the official hello world example of Docker, pull an image called busybox sudo docker pull busybox # to see which images already exist locally # We can see busybox sudo docker images # now let's modify the container of busybox image # this time Let's create a folder sudo docker run busybox mkdir / home/test # let's see which images we have. # notice that the container stops after each command is executed # you can see that there is a busybox container sudo docker ps-a # now you can commit changes. # after submission, you will see a new image busybox-1 # is the ID sudo docker commit busybox-1 # obtained after you just modified the container and see what images we have. # We now have both busybox and busybox-1 images. Sudo docker images # Let's execute the following command to see how the two images 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 images (busybox and busybox-1) and a container obtained by modifying the busybox container (with an extra / home/test folder). Let's see how these changes are persisted.

Export (Export)

The Export command is used to persist the container (not a mirror). Therefore, we need to get the container ID in the following ways:

Sudo docker ps-a

Then perform the export:

Sudo docker export > / home/export.tar

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

Save (Save)

The Save command is used to persist the image (not the container). Therefore, we need to get the image name in the following ways:

Sudo docker images

Then perform the save:

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

The end result is a 2.8MB-sized 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 of all, do a little cleanup-delete all containers and images:

# View all containers sudo docker ps-a # Delete them sudo docker rm # View all mirrored sudo docker images # Delete them sudo docker rmi busybox-1 sudo docker rmi busybox

Note: you can use docker rm $(docker ps-Q-a) to delete all containers at once, and docker rmi $(docker images-Q) to delete all images at once.

Now start importing the container you just exported:

# Import export.tar file cat / home/export.tar | sudo docker import-busybox-1-export:latest # check the image sudo docker images # check whether the import is successful, that is, start a new container and check whether the / home/test directory (does exist) sudo docker run busybox-1-export [- d / home/test] & & echo 'Directory found' | | echo' Directory not found'

Use a similar procedure to import the image:

# Import save.tar file docker load < / home/save.tar # check the image sudo docker images # check whether the import is successful, start a new container and check whether the / home/test directory (does exist) 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 will be slightly smaller than the original version. That's because history and metadata are lost after export. Just execute the following command:

# display all layers of the image (layer) sudo docker images-- tree

Execute the command to display the following. As you can see, images that are exported and then imported (exported-imported) lose all history, while images that are saved and loaded (saveed-loaded) do not lose history and layers (layer). This means that by exporting and then importing, you will not be able to roll back to the previous layer (layer). At the same time, by persisting the entire image by saving and then loading, you can roll back the layer (you can perform the layer before docker tag rolls back).

Vagrant@ubuntu-13:~$ sudo docker images-- tree ├─ f502877df6a1 Virtual Size: 2.489 MB Tags: busybox-1-export:latest └─ 511136ea3c5a Virtual Size: 0B └─ bf747efa0e2f Virtual Size: 0B └─ 48e5f45168b9 Virtual Size: 2.489 MB └─ 769b9341d937 Virtual Size: 2.489 MB └─ 227516d93162 Virtual Size: 2.489 MB Tags: so much for busybox-1:latest 's "what's the difference between the save and export commands of Docker?" Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report