in Coding

Difference between save and export in Docker

I recently played around with Docker, an application container and virtualization‎ technology for Linux. It was pretty cool and I was able to create Docker images and containers within a couple of minutes. Everything was working right out of the box!

At the end of my day I wanted to persist my work. I stumbled over the Docker commands save and export and wondered what their difference is. So I went to StackOverflow and asked a question which was nicely answered by mbarthelemy. Here is what I found out.

How Docker works (in a nutshell)

Docker is based on so called images. These images are comparable to virtual machine images and contain files, configurations and installed programs. And just like virtual machine images you can start instances of them. A running instance of an image is called container. You can make changes to a container (e.g. delete a file), but these changes will not affect the image. However, you can create a new image from a running container (and all it changes) using docker commit <container-id> <image-name>.

Let’s make an example:

Now we have two different images (busybox and busybox-1) and we have a container made from busybox which also contains the change (the new folder /home/test). Let’s see how we can persist our changes.

Export

Export is used to persist a container (not an image). So we need the container id which we can see like this:

To export a container we simply do:

The result is a TAR-file which should be around 2.7 MB big (slightly smaller than the one from save).

Save

Save is used to persist an image (not a container). So we need the image name which we can see like this:

To save an image we simply do:

The result is a TAR-file which should be around 2.8 MB big (slightly bigger than the one from export).

The difference

Now after we created our TAR-files, let’s see what we have. First of all we clean up a little bit – we remove all containers and images we have right now:

We start with our export we did from the container. We can import it like this:

We can do the same for the saved image:

So what’s the difference between both? Well, as we saw the exported version is slightly smaller. That is because it is flattened, which means it lost its history and meta-data. We can see this by the following command:

If we run the command we will see an output like the following. As you can see there, the exported-imported image has lost all of its history whereas the saved-loaded image still have its history and layers. This means that you cannot do any rollback to a previous layer if you export-import it while you can still do this if you save-load the whole (complete) image (you can go back to a previous layer by using docker tag <LAYER ID> <IMAGE NAME>).

Best regards,
Thomas

  • Rajesh Rao

    Thanks for clearing this question, I did see history on exported image is zero, where as on saved image is there

  • Pingback: Flatten a Docker container or image | Thomas Uhrig()

  • Дмитрий

    Do I need to stop container defore export it to tar file?

  • @Дмитрий No, you should also be able to export a running container.

  • Aris Setyawan

    Can I “pause” a computation, and then “resume” it using EXPORT container command? Or is It possible to do, using Docker?

  • Pingback: Introduction to Docker()

  • Alex Glover

    Thanks for posting this Thomas – this isn’t a topic that is well covered in the documentation. I appreciate how you walked through the ‘experiment’ end to end, clearing the original images before loading them and showing the image tree.

    Great post.

  • Pingback: 在 Container 運行 httpd « Jamyy's Weblog()

  • Pingback: A thorough Introduction Guide To Docker Containers | Never Ending Security()

  • Virendhar Sivaraman

    interesting post ! neatly done.

  • azizkhani

    thanks for nice post

  • Greg Tomei

    Why use sudo for docker commands? Its not necessary and will probably lead to permissions issues later.

    • stigkj

      You need sudo for using the “Docker socket” when running on Linux if your user is not in the Docker group. If you are working on OS X you probably are using HTTP over TCP for talking to the daemon.

  • rajan suchak

    Thanks for the Explanation Thomas, I have noticed the CREATED time difference.

    busybox-1-export latest b3260e0c0c4a About a minute ago 1.113 MB
    busybox-1 latest 1e40056b884e 48 minutes ago 1.113 MB

    Export-Import creates new Container where as the Save-Load shows time of the original file created.

  • Pingback: Difference between save and export in Docker | blacksailer()

  • Leandro Kersting de Freitas

    Hi,

    Try the helicopterizer for Backup and Restore for Docker Container in the Cloud Providers.

    https://github.com/frekele/helicopterizer

    help us with your code, make a PR. 🙂

  • david

    Thomas,
    thank you for your post.
    To be honest, after reading it and the referenced StackOverflow discussion, I’m no less confused than before.
    Your tests show that both methods produce identical results, where it matters (search the post for “it does!” )

    I understand that there will be a difference in size,layers,history, etc, however – what-the-heck-does-it-mean for people whose use case is to move a container with data from one host to another, which I believe was your use case as well, reading your SO post.

    I think we should focus on what the person actually wants to achieve, rather than examine the verbiage of the question.

    There should be only one “correct” answer as to which command to use for which use case, and I still don’t know it.
    Am I the only one?

  • Pingback: Docker误区+技巧+转换关系-鲸鱼云()