Docker container: Backup and Restore

The purpose of this guide is to go over the step by step instructions of how to back up a Docker container on the Linux command line. We’ll also show how to restore a Docker container from backup. This can be done on any Linux system where Docker is installed, and will work on any Linux distribution.

To understand the Docker container backup and recovery process we first need to understand the difference between a Docker image and a Docker container. A Docker image contains an operating system with possibly one or more preconfigured applications, whereas a Docker container is a running instance created from an image.

In this tutorial you will learn:

  • How to back up a Docker container on Linux
  • How to restore a Docker container on Linux

Backing up a Docker container on Linux

Backing up a Docker container on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Docker
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Docker container backup



    1. When we need to make a backup of a docker container, we need to use the docker commit command to capture its current state and save it as a Docker image. Let’s start by finding the name of our container and the container ID.
      $ docker ps
      

Viewing our running Docker container

Viewing our running Docker container
    1. From the above output we see a running Docker container named container1 with an ID of 78727078a04b. We can now use docker commit command to take a snapshot of its current running state:
      $ docker commit -p db012f940cd7 container1
      
    2. When executing the above command we have first paused a running container with the -p option, made a commit to save the entire snapshot as a docker image with a name container1:
      $ docker images
      


This output shows that our Docker container running state has been saved as an image

This output shows that our Docker container running state has been saved as an image
  1. Now we have a container backup saved as an image waiting to be redeployed again. If we wish to redeploy our container1 image on another docker host system we may push the image to some private docker repository:
    $ docker login
    $ docker push container1
    
  2. Alternatively, we can save it as a tar file and move it freely to any desired docker host system for a deployment:
    # docker save -o ~/container1.tar container1
    # ls -l ~/container1.tar
    -rw-r--r--. 1 root root 131017216 Jun 14 20:31 /root/container1.tar
    

Docker container recovery



The previous section explained how to backup a docker container. In this section we will discuss how to recover from a docker backup.

In the case that we have pushed our backed up docker container image to a private repository we can simply use the docker run command to start a new instance from the container1 image. If we have transferred our container1.tar backup file to another docker host system we first need to load backed up tar file into a docker’s local image repository:

# docker load -i /root/container1.tar

Then, confirm that the image was loaded with the following command:

# docker images

Now we can use the docker run command to start a new instance from the above loaded container1 image.

Closing Thoughts

In this guide, we learned how to back up a Docker container on Linux. We also saw how to restore the Docker container we previously backed up. The process involved converting our Docker container to a Docker image, which then allows us to make a backup.



Comments and Discussions
Linux Forum