Backing up Docker containers helps protect application files and makes it easier to restore, version, and share projects. Docker allows you to save a container's filesystem as a TAR file and push the backup image to Docker Hub.
- Save the container filesystem as a
.tarfile locally. - Use the backup to recover the container when needed.
- Maintain different versions of your Docker application.
- Push the backup image to Docker Hub for easy access and collaboration.
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 bashAfter you fire up the bash, use the below command to create a file.
echo "geeksforgeeks" > geeksforgeeks.txt
ls

Step 2: Get the Container ID
You will need the Container ID in order to create the backup.
sudo docker container lsNote that if the container is not running, you can start the Container using the below command.
sudo docker start 
Step 3: Commit the Docker Container
To create a snapshot, you need to commit the Container.
sudo docker commit -p 6cb599fe30ea my-backup
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
You will find the backup Tar file in your Home Directory.

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
