How To Back Up Docker Containers
Before we back up a docker container, we need the container ID of that specific container. Here, will use the ps command to get the IDs of all the running containers.
From here, we can copy the container ID of the container we need to back up.
So, to list all the IDs of all the running containers, use the following command:
docker ps -a
In the results, we'll look for the container ID of the docker container that we want to back up, copy it, and then use it with the Docker commit command. The format for this command is:
docker commit −p <CONTAINER_ID> <BACKUP_NAME>
So, for example, if the container ID of our container is 5c7d78fcb634, and the name of our backup is our-docker-backup, the command will be:
docker commit −p 5c7d78fcb634 our-docker-backup
With this command we've first paused a running container with the -p option, and we've made a commit to save the entire snapshot as a docker image with the name our-docker-backup.
We can also save the image as a tar file on our local machine by using the command:
docker save −o ∽/our-docker-backup.tar our-docker-backup
We can check whether the file has been saved by using the command:
ls −l ∽/our-docker-backup.tar
When it saved as a tar file we can move it do any other desired docker host system for a deployment.
We can also redeploy our our-docker-backup image on another docker host system by using the push command to push the image to a private docker repository.
To do this, will use the command:
docker logindocker push our-docker-backup
Now that we've backed up our Docker container, let's look at the procedure to restore Docker containers, either locally or on another machine.
How To Restore A Docker Backup
If we've saved the tar file on our host machine, we can restore it by using the docker load command. To do this, we’ll use the command:
docker load −i ∽/our-docker-backup.tar
To make sure that the image was restored successfully, we can then list all the images using the following command:
docker images
If we've pushed the backup image to a docker repository, we can use the pull command to pull the data from the repository.
Here, we'll use the command:
docker pull our-docker-backup:tag
Once we've restored the backed up image on our local machine, we can use the run command to run a new instance of the restore docker image. to do this we use the command:
https://simplebackups.com/blog/docker-container-backup-restore-guide/
From here, we can copy the container ID of the container we need to back up.
So, to list all the IDs of all the running containers, use the following command:
docker ps -a
In the results, we'll look for the container ID of the docker container that we want to back up, copy it, and then use it with the Docker commit command. The format for this command is:docker commit −p <CONTAINER_ID> <BACKUP_NAME>
So, for example, if the container ID of our container is 5c7d78fcb634, and the name of our backup is our-docker-backup, the command will be:docker commit −p 5c7d78fcb634 our-docker-backup
With this command we've first paused a running container with the -p option, and we've made a commit to save the entire snapshot as a docker image with the name our-docker-backup.We can also save the image as a tar file on our local machine by using the command:
docker save −o ∽/our-docker-backup.tar our-docker-backup
We can check whether the file has been saved by using the command:ls −l ∽/our-docker-backup.tar
When it saved as a tar file we can move it do any other desired docker host system for a deployment.We can also redeploy our our-docker-backup image on another docker host system by using the push command to push the image to a private docker repository.
To do this, will use the command:
docker login
docker push our-docker-backup
Now that we've backed up our Docker container, let's look at the procedure to restore Docker containers, either locally or on another machine.docker load −i ∽/our-docker-backup.tar
To make sure that the image was restored successfully, we can then list all the images using the following command:docker images
If we've pushed the backup image to a docker repository, we can use the pull command to pull the data from the repository.Here, we'll use the command:
docker pull our-docker-backup:tag
Once we've restored the backed up image on our local machine, we can use the run command to run a new instance of the restore docker image. to do this we use the command:docker run −ti our-docker-backup:tag