How to Backup a Docker Container and Restore it – Step-by-Step



Here, you will learn how to backup and restore a Docker container locally – an alternative approach to push and pull the container from Docker Hub.

If you need to learn the basics of Docker, please check this other tutorial.

1. Why Should You Care about Backuping a Docker Container?

In some cases, docker containers are large, and networking issues could be on the way if a user tries to push and pull a container from Docker Hub. Moreover, it could happen if one of the dependencies defined in the docker file is not available anymore, so backing it up is an approach to save that container.

2. How to Backup a Docker Container

Backing up a Docker container is a 4 step process:

First, the user needs to run the docker image by using the command below:

$ docker run  -it IMAGE_ID

where the IMAGE_ID should be the image you want to backup; find it using the command docker images

Now you have got it running, please open another tab in your terminal, and figure out the running container ID associated with the image above by running:

$ sudo docker container ls

Next, you know which running container to backup, run the command below using the CONTAINER_ID

$ sudo docker commit -p CONTAINER_ID my_backup

The command above commits the container, and finally, the command below will backup the container into a tar file:

$ sudo docker save -o my_backup.tar my_backup

3. How to Restore a Docker Container

Phew! Now that you know how to backup a container, restoring it should be straightforward using the command below:

$ sudo docker load -i my_backup.tar

Last but not least, the command loads the docker image and can be seen when running the command docker images and execute using docker run -it IMAGE_ID

4. Conclusion

This tutorial teaches an alternative approach to push and pull a container from Docker Hub, and using the step-by-step described here; the user can back it up locally and restore it when desired.

5. More Resources