How to backup and restore a Docker Container?
In this article, we will get to know that how to backup and restore a Docker Container. While Docker – and every container is typically somewhat newer to the production IT environment than most other technologies. There’s still a necessity to backup these containers, their applications, and their data. it’s going to even be of critical importance.
Therefore, safeguarding that data just in case disaster happens. For example as a result of a knowledge breach or human error is probably going necessary. This topic covers Docker‘s ability to create a backup and restore docker container.
Backing up a Docker Container
First of all, if you need to back up the docker container, we need a container ID of that particular container. We will use the docker ps command to get the container ID of all the running containers. After that copy the required container ID which we need to backup.
Follow the command below to do so.
After that, copy the container ID of the docker container for Which you want to create a backup. To create a snapshot of the docker container. We need to use the docker commit command.
The Docker commit command is −
# sudo docker commit −p <CONTAINER_ID> <BACKUP_NAME>
sudo docker commit −p 5c2f45fg535 ubuntu-os-backup
To save the image as a tar format in your local machine, you need to use this command.
# sudo docker save −o ∽/ubuntu-os-backup.tar ubuntu-os-backup
Use the below command to check for the saved tar file.
# sudo ls −l ∽/ubuntu-os-backup.tar
You can also push that image backup directly on the Docker registry. Follow this command to do this. Replace the username with your docker registry username.
# sudo docker login # sudo docker push ubuntu-os-backup:tag
Restoring a Docker Container
After creating a backup of the docker container, if you would like to revive the container for using it. here’s how you’ll be able to do so.
In case you have got saved the tar file and move it into your host machine. You’ll simply restore it back using the docker load command. Follow the command below to try to do so.
sudo docker load −i ∽/ubuntu-os-backup.tar
If you have pushed the backed-up file image to the Docker registry. You can pull it back using the below command
sudo docker pull ubuntu-os-backup:tag
After you have restored the image on your local machine. You can use the docker run command to run a new container of the restored docker image. You can use the following command.
# sudo docker run −ti ubuntu-os-backup:tag
backup and restore a Docker Container
In the above article, we’ve seen the way to backup and restore a docker container. If you would like to migrate your docker container which is running in your host machine, to a different machine. You’ll be able to use the mixture of both the processes (backup and restore) to try to do so.
If you have created the backup of the image and pushed it on the docker registry. You’ll simply log in to the target machine and pull the backed-up image. In case, you have got created the tar files to backup your container. You’ll copy and paste the tar file on the target machine and use the docker load command to load the image. Then run the docker run command to execute the container.
Click here to know that how to install docker on centos 7.