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 jokoe/waf-lb:latest bash
After you fire up the bash, use the below command to create a file.
echo "mas ekoe" > mas-ekoe.txt ls
Step 2: Get the Container ID
You will need the Container ID in order to create the backup.
root@waf:~# docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e9f656f6f9b7 jokoe/waf-lb:latest "/init" 1 months ago Up 11 days 0.0.0.0:80-81->80-81/tcp, :::80-81->80-81/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp root_app_1 7eda00f27413 influxdb:2.0.8 "/entrypoint.sh infl…" 21 months ago Up 11 days 0.0.0.0:8086->8086/tcp, :::8086->8086/tcp root_influxdb_1 e21048121e86 jc21/mariadb-aria:latest "/scripts/run.sh" 22 months ago Up 11 days 3306/tcp root_db_1 root@waf:~#
Note that if the container is not running, you can start the Container using the below command.
sudo docker start <container-id>
Step 3: Commit the Docker Container
To create a snapshot, you need to commit the Container.
root@waf:~# docker commit e9f656f6f9b7 msteam/waf-lb:bck-230706 sha256:e3d4f71a4a347e6ffee36de81169cbb32f678d2786ecf451c8d00d85934cfd41 root@waf:~# root@waf:~# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE msteam/waf-lb bck-230706 e3d4f71a4a34 About a minute ago 1.08GB jokoe/waf-lb latest 1875e6988765 21 months ago 978MB influxdb 2.0.8 08f78720df14 22 months ago 345MB jc21/nginx-proxy-manager latest 426180db242b 22 months ago 892MB jc21/mariadb-aria latest d3aa3f6e075e 22 months ago 217MB root@waf:~#
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.
root@waf:~# docker save -o waf-lb-bck230706.tar msteam/waf-lb root@waf:~# ls -l total 1140896 -rw-r--r-- 1 root root 137 Jul 21 2021 75-cloud-ifupdown.rules drwxrwxr-x 2 root root 4096 Aug 31 2021 GeoLite2-City_20210831 -rw-r--r-- 1 root root 35279257 Sep 21 2021 GeoLite2-City_20210831.tar.gz -rw-r--r-- 1 root root 26664 Nov 16 2020 check-mk-agent_1.6.0p19-1_all.deb drwxr-xr-x 11 root root 4096 Dec 19 2022 data -rw-r--r-- 1 root root 786 Sep 24 2021 docker-compose.yml drwxr-xr-x 10 root root 4096 Jul 6 00:00 letsencrypt drwxr-xr-x 9 1001 1001 4096 Sep 21 2021 nginx-1.19.3 -rw-r--r-- 1 root root 1052581 Sep 29 2020 nginx-1.19.3.tar.gz drwxr-xr-x 2 root root 4096 Sep 21 2021 ngx_http_geoip2_module -rw-r--r-- 1 root root 138970 Sep 18 2021 systemd.xml -rw-r--r-- 1 root root 332325 Sep 21 2021 telegraf.conf -rw------- 1 root root 1131402752 Jul 6 00:37 waf-lb-bck230706.tar root@waf:~#
You will find the backup Tar file in your current 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 msteam/waf-lb:latest
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.