SnapShooter Learning Center

How to Backup and Restore a Docker Container

How to Backup and Restore a Docker Container

Simon Bennett]
Simon Bennett
Last Updated: Oct 20, 2021
Table of Contents

# Introduction

Developers always find it hard to choose the best tools for each stage based on multiple languages, frameworks, architecture, or interfaces. Docker is a leading software platform that builds, runs, and manages containers on servers and the cloud. Thus, it helps accelerate the developers’ workflow and choices to select tools, deployment environment, and application stack for every project. Docker container helps create a packaged environment, including all the application codes and their dependencies in one packet. Thus, making it lightweight, easy to port, smooth to run, and reliable for all computing environments.

Some developers assume that the container will run for a temporary period, so there is no point in creating a backup. However, if we think harder, the software helps manage different versions of your projects and carries crucial information. Thus, it is necessary to keep an account or extra copy of all files and folders if in case anything goes wrong.

# Docker Container Basics

The launch of Docker revolutionized the software development industry in 2013, as a result, developers had a new software model that was flexible, portable, and easy to deploy.

To keep it short and simple, a Docker container is an executable package of software used to develop, ship, and run applications. It includes everything from code, runtime, settings, system tools, and libraries. Thus, it helps developers to minimize the extra time to write codes or run them during production. The software helps eliminate repetitive and tedious configuration tasks. Further, resulting in a fast, easy, and portable application development throughout the different stages.

Containers are lightweight, portable, and compatible with any Linux system and Windows-based applications. Also, in comparison to Virtual Machines, Containers only run for a temporary period (less than a week). It also requires orchestration to run multiple containers in a production environment. Another best feature of Docker Container is it comprises default isolation capabilities and is secure for applications. The software design was introduced for developers to separate application dependencies from infrastructure and run applications more quickly from one computing environment to the other. There are some of the leading cloud providers and open source serverless frameworks prefer dockers for their containers. If you are one of them, make sure to maintain a backup for your docker containers.

# Why Docker Needs a Backup?

As per the old model, there was an agent in the server that needed to be backed up, but with virtualization, a new model was introduced. Under the new model, the agent performs at the hypervisor level and creates the virtual machines backup as images.

However, containers do not provide any of these options. Still, it is suggested to create backups for dock containers. Let's have a look at why and when a developer must create backups for their containers.

The container serves a single application component and task, including logging or monitoring, which is why many container advocates conclude it as a less priority for backups. Also, the high availability is built in every part of the design, and people confuse it with the ability to recover from an uncertain disaster. Further, containers are often spawned or killed off as Kubernetes are always on the run, and in clusters.

In most cases, there is no data inside the container and a running container requires no backup. But, what if the disaster strikes and you lose the entire cluster, container nodes, and other storage. For precaution, it is best to keep a copy of the entire Kubernetes and Docker environment. There is no assurance! You never know if the servers fail or the large data centers suffer outages. You may face a bigger loss and it can be time taking for developers to recollect the lost data or information stored in these containers. Thus, you must always create a backup for your docker containers based on the implementation of an application and its data requirement. Docker has many layers of abstraction and if you are moving forward with the plan to create a backup, make sure to keep a backup of Container Images, databases, Deployments, Kubernetes resources.

# Back Up a Docker Containers

Before backing up a Docker container, you will need the container ID of a specific running container. You can use the docker ps -a command to get the IDs of all the running containers.

docker ps -a 

You should see all running containers with their IDs in the following output:

CONTAINER ID   IMAGE           COMMAND       CREATED          STATUS          PORTS     NAMES
905959128afc   debian:latest   "bash"        17 seconds ago   Up 15 seconds             vibrant_heisenberg
61a5f69188d9   centos:latest   "/bin/bash"   32 seconds ago   Up 30 seconds             peaceful_bhaskara
900ac11765be   ubuntu:latest   "bash"        49 seconds ago   Up 47 seconds             youthful_lewin

Now, choose any container ID that you want to backup and use the docker commit command to backup the running container.

The basic syntax to backup a running container is shown below:

docker commit -p [container id] [backup name]

For example, to backup a container with ID 900ac11765be and name of the backup container is ubuntu_backup, run the following command:

docker commit -p 900ac11765be ubuntu_backup

The above command will first stop the running container then save the entire container as a docker image.

You can also compress the ubuntu_backup with tar archive using the following command:

docker save -o ubuntu_backup.tar ubuntu_backup

You can verify the tar backup using the following command:

ls -l ubuntu_backup.tar 

Sample output:

-rw------- 1 root root 75164160 Oct 23 04:38 ubuntu_backup.tar

# Restore a Docker Container

You can use the docker load command to restore the image from the backup.

docker load -i ubuntu_backup.tar

This will load the ubuntu_backup image from the tar archive to your local machine. You can verify the restored image using the following command:

docker images

You should see your ubuntu_backup image in the following output:

ubuntu_backup   latest    2e61dcddeb36   2 minutes ago   72.8MB
ubuntu          latest    ba6acccedd29   7 days ago      72.8MB
debian          latest    f776cfb21b5e   11 days ago     124MB
centos          latest    5d0da3dc9764   5 weeks ago     231MB

Now, run the docker run command to launch a new instance from the restore docker image.

docker run -dit ubuntu_backup

# Conclusion

In the above guide, we explained how to backup and restore a docker container. I hope this will helps you to migrate a docker container from one host machine to another host.

Did you find this article helpful?