Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to backup and restore a Docker Container?
Docker containers provide a packaged environment for applications, making them portable and lightweight while allowing version control. Sometimes you need to create snapshots or backups of running containers for emergency rollbacks or to preserve specific states. This article covers how to backup and restore Docker containers using built-in commands.
Note: The backup methods described here work for containers with embedded data. For containers using separate data volumes, you must create separate backups for each volume.
Backing up a Docker Container
The backup process involves creating a snapshot of the container's current state and saving it as an image. This allows you to preserve the exact configuration and data at a specific point in time.
Step 1: Find the Container ID
First, identify the container you want to backup by listing all containers:
sudo docker ps -a
Copy the Container ID of the target container from the output.
Step 2: Create a Container Snapshot
Use the docker commit command to create an image from the container's current state:
sudo docker commit -p <CONTAINER_ID> <BACKUP_NAME>
The -p flag pauses the container during the commit process to ensure data consistency.
Example:
sudo docker commit -p 5c2f44fbb535 backup-ubuntu
Step 3: Save as TAR File (Local Backup)
To save the backup image as a TAR file on your local machine:
sudo docker save -o ~/backup-ubuntu.tar backup-ubuntu
Verify the TAR file was created:
sudo ls -l ~/backup-ubuntu.tar
Step 4: Push to Docker Registry (Remote Backup)
Alternatively, push the backup image to Docker Hub or another registry:
sudo docker login sudo docker tag backup-ubuntu username/backup-ubuntu:tag sudo docker push username/backup-ubuntu:tag
Replace username with your Docker Hub username and tag with a version identifier.
Restoring a Docker Container
Restoration involves loading the backup image and creating a new container instance from it.
Restore from TAR File
If you have a local TAR backup, load it using:
sudo docker load -i ~/backup-ubuntu.tar
Confirm the image was restored:
sudo docker images
Restore from Docker Registry
If the backup was pushed to a registry, pull it back:
sudo docker pull username/backup-ubuntu:tag
Run the Restored Container
Create a new container instance from the restored image:
sudo docker run -ti backup-ubuntu:tag
The -ti flags provide an interactive terminal session.
Container Migration
To migrate containers between machines, combine backup and restore processes:
Registry Method: Push the backup to a registry from the source machine, then pull it on the target machine.
TAR Method: Create a TAR backup, transfer the file to the target machine, then load and run it.
Comparison of Backup Methods
| Method | Storage Location | Transfer Required | Best For |
|---|---|---|---|
| TAR File | Local filesystem | Manual file transfer | Offline backups, local storage |
| Registry Push | Docker Hub/Registry | Network pull/push | Remote access, team sharing |
Conclusion
Docker container backup and restoration enables you to preserve application states and migrate containers between environments. Use docker commit and docker save for local backups, or combine docker commit with registry operations for remote backups. Choose the method that best fits your infrastructure and workflow requirements.
