How to backup and restore a Docker Container?

Docker allows us to automate the process of building and deploying an application. It also allows us to create a packaged environment to run the application which makes it easily portable and lightweight while also allowing us to keep track of the versions. All of these are possible through Docker Containers. It helps in making the applications platform independent.

Let’s say we have a docker container running in our machine and we want to take a snapshot or keep a backup of that container so that in case of any emergency, if we want to roll back changes or execute a container with a previous timestamp, we can easily do it with the help of stored backups. Thus, backing up a docker container and restoring can become an essential component of the project.

In this article, we will see how to backup and restore Docker containers with the help of certain commands. We also need to note that the processes used in this article to backup a docker container does not work if the containers use separate data volumes. To backup those docker containers that use separate data volumes, we need to create a separate backup for each of the data volumes.

  • Backing up a Docker Container

First of all, in order to backup the docker container, we need the container ID of that particular container. We will use the ps command to get the container IDs of all the running containers and copy the one which we need to backup.

Check out the command below to do so.

sudo docker ps −a

After that, copy the container ID of the docker container that you want to create a backup for. To create a snapshot of the docker container, we use the docker commit command. The format of the Docker commit command is −

sudo docker commit −p <CONTAINER_ID> <BACKUP_NAME>


sudo docker commit −p 5c2f44fbb535 backup-ubuntu

To save the image as a tar file in the local machine, you can use this command.

sudo docker save −o ∽/backup−ubuntu.tar backup−ubuntu

Use this command to check for the save tar file.

sudo ls −l ∽/backup−ubuntu.tar

You can also choose to push the image backup directly to the docker registry. Use this command to do so. Replace the username with your docker hub username.

sudo docker login
sudo docker push backup−ubuntu:tag
  • Restoring a Docker Container

After creating a backup of the docker container, if you want to restore the container for using it, here’s how you can do so.

In case you have saved the tar file in your host machine, you can simply restore it back using the docker load command. Use the command below to do so.

sudo docker load −i ∽/backup-ubuntu.tar

To confirm if the image was successfully restored or not, you can list all the images using the following command.

sudo docker images

If you have pushed the backed up image to docker registry, you can pull it back using the following command

sudo docker pull backup-ubuntu:tag

After you have your restored image on your local machine, you can use the docker run command to run a new instance of the restored docker image. You can use the command below to do so.

sudo docker run −ti backup−ubuntu:tag

In the above article, we have seen how to backup and restore a docker container. If you want to migrate your docker container which is running in your host machine, to another machine, you can use the combination of both the processes (backup and restore) to do so. If you have created the backup of the image and pushed it in docker registry, you can simply login to the target machine and pull the backed up image. In case, you have created the tar files to backup your container, you can copy and paste the tar file on the target machine and use the docker load command to load the image and then run the docker run command to execute the container.

Updated on: 27-Oct-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started