Backing up a Docker Container

If you are a Docker Developer, you would surely know how important it is to backup your Docker Container file system. If you are working on an important Docker Application, it becomes very necessary to backup all the files and folders related to it so that in case anything goes wrong, you can get back all those files. It also helps you in managing different versions of your project and also sharing the files and folders of your project among your team members.

In this article, we are going to see how can we back up a Docker Container by saving it as a tar file in your local system. We will also see how you can push that Docker Image Backup directly into your Docker Hub accounts for ease of sharing. Follow the below steps to backup a docker container:

Step 1: Create a Docker Container

For our example, we are going to create an Ubuntu Container with a single file inside it.

sudo docker run -it ubuntu bash

After you fire up the bash, use the below command to create a file.

echo "geeksforgeeks" > geeksforgeeks.txt
ls
Creating a Container and File

Creating a Container and File

Step 2: Get the Container ID

You will need the Container ID in order to create the backup.



sudo docker container ls

Note that if the container is not running, you can start the Container using the below command.

sudo docker start <container-id>
Copying Container ID

Copying Container ID

Step 3: Commit the Docker Container

To create a snapshot, you need to commit the Container.

sudo docker commit -p 6cb599fe30ea my-backup
Committing the Container

Committing the Container

Step 4: Saving backup as a Tar file

You can use this command, to save the backup as a Tar File in your local machine.

sudo docker save -o ~/my-backup.tar my-backup
Saving Backup

Saving Backup

You will find the backup Tar file in your Home Directory.

Tar File

Tar File

Step 5: Pushing Image to Docker Hub

In order to push it back to the Docker Hub, you need to have an account on Docker Hub.

Login using your command line and push the Tar File.

sudo docker login
sudo docker push my-backup:latest
Pushing Image

Pushing Image

To conclude, in this article we saw how to create a backup of a Docker Image into a Tar File and pushing it into a Docker Hub account for ease of sharing.

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.