Backup and Restore Docker Containers: A Quick Guide

Hanin Mestrah
Tech Blog
Published in
5 min readJan 14, 2023

A backup is created when data is saved on a suitable storage medium. Once a backup has been completed successfully, a backup copy will be accessible for data restoration. If your system fails or becomes infected with a virus, you must be able to restore business and customer data promptly; otherwise, you run the danger of suffering significant financial losses. Furthermore, if dissatisfied clients are abruptly compelled to wait longer for their commercial operations, it could potentially harm a good reputation developed over time.

Image Reference: filecloud.com

This article illustrates the different Docker commands that can be used to perform backup and restore operations.

Docker Image vs Container: a remainder

First, the difference between Docker containers and images is worth mentioning to remove any confusion.

A Docker image contains the basic package, the application configuration, and the dependencies. A Docker container is a deployed and running Docker image. In other words, it is an instance of a docker image.

A Docker container consists of the initial Docker image, an internal file system, environment variables, and a network adapter.

Basic Docker commands (Reference: Stackoverflow.com)

Docker Data

Docker is a software platform that makes it simple to develop, test, and deploy applications. Docker packages software into standardized units called containers containing all the needed things, such as system tools and libraries. For an application to run swiftly and consistently across different computer environments, code and its dependencies are packaged into the container.

However, operating system images are not present within containers, unlike server or machine virtualization methods. As a result, they become substantially more portable and lightweight.

Docker architecture (Reference: the basics a beginners guide to docker)

Docker data is dispersed among many Docker files and objects. Unfortunately, there isn’t a single method for backing up specific Docker data. The use of multiple instructions is possible, though.

Saving Docker Data

There is no need for a backup if you utilize volumes or bind mounts to store your container data, but keep in mind the configurations and options available to you when constructing the container.

Docker Commit

To commit your container (current file system, environment variables, and images) to a new tagged image, you can use the docker commit command:

docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

A container’s filesystem modifications and some of its configurations are stored when a container is committed. Take into consideration the sensitive information.

Backing up Volumes

When you build a volume, it is kept on the Docker host in a directory. This directory is what is mounted inside the container when the volume is mounted. Similar to how bind mounts behave, but with volumes controlled by Docker and separated from the host machine’s essential operations.

To create a volume you can refer to docker volume create command:

docker volume create

For storing container’s data within a volume you should pass the directory and volume information as an argument to the docker run command:

docker run -v /dbdata — name dbstore ubuntu /bin/bash  for backup,

Then, we can use the command below to backup the data stored within the volume.

docker run — rm — volumes-from dbstore -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata

Refer to the docker volume prune command to remove unused volumes.

docker volume prune

Refer to the backup, restore, or migrate data volumes page in the storage section to restore volume data.

Backing up and Restoring Docker Images

Mainly, backing up and restoring Docker images using external files is done using two commands: docker save and docker load.

Docker Save

It is used to save one or more images to a tar archive.

docker image save [OPTIONS] IMAGE [IMAGE...]

The output tar archive should be passed after the -o argument.

docker image save -o images.tar image1 [image2 …]

Docker load

This command is used to load an image from a tar archive.

docker image load [OPTIONS]

To restore saved images previously, we should specify the input tar file after the -i argument.

docker image load -i images.tar

Pushing and Pulling images

Alternatively, we can backup images to an online repository using the docker pull (By default, images are uploaded to the Docker Hub). We can use the docker push command to upload to the Docker Hub any image you have locally created and wish to maintain.

docker push

Make sure to configure the repository’s visibility as “private” for images that should not be publicly accessible.

To download images from a docker repository, we can use the docker pull command:

docker pull

Backing up and Restoring Docker Containers

Backing up and restoring containers is mainly done using two commands: docker import and docker export.

Image Reference: PSPDFKit.com

Docker Export

By exporting this means that Docker containers exports a copy of the file system inside the container.

docker export [OPTIONS] CONTAINER

Docker Import

By importing the content from an archive or tarball that is produced by exporting a container, Docker import creates a Docker image. When importing data or information from the archive, we might specify a URL or a dash (-).

docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]

Conclusion

To sum up, in this article, we explained different methods used to backup and restore docker containers and images. Commands can be summarized as follows:

  • docker pull, docker push: used to backup and restore images to a online repository.
  • docker save, docker load: used to backup and restore images to a local file.
  • docker import, docker export: used to backup and restore containers to a local file.

References

--

--

Hanin Mestrah
Tech Blog

A computer science student with strong foundation in programming logic, eager to learn from senior engineers that encourages personal development.