Detailed explanation of how to copy and backup docker container data

Detailed explanation of how to copy and backup docker container data

Here we take the Jenkins container as an example to introduce three methods.

Method 1

Package the container into an image. The data is now in the image. im:1.0 is the container name. You can use any name.

docker commit <container id> im:1.0

Re-run the image, -v maps the tmp directory in the container to the tmp directory on the host

docker run -itd -v /tmp:/tmp im:1.0 // The first tmp is the host directory, the second is the directory in the container

Copy the file /var/jenkins_home to tmp. Note that the container name here is randomly generated and can be viewed through docker ps | grep im:1.0. -it is an interactive terminal.

docker exec -it <container name> cp -r /var/jenkins_home /tmp // cp copy and paste

Enter the tmp directory and check whether there is an additional jenkins_home directory

cd /tmp
ls

Enter the jenkins_home directory and move the contents to the home, srv and other directories. Because the tmp directory is saved temporarily, it will be deleted the next time Linux is restarted, so the data can be persistent.

cd jenkins_home/
mv <home/srv>

Note that after the data is backed up, you need to delete the extra containers that were just generated.

docker stop <container name> && docker rm <container name>

Method 2

Official Documentation

Execute the following command, <container name> is the name of the running container, -v is a mapping, the /tmp/backup directory is specified arbitrarily, cvf is compressed, and /var/jenkins_home is the directory to be backed up

docker run --rm --volumes-from <container name> -v /tmp/backup:/backup ubuntu tar cvf /backup/backup.tar /var/jenkins_home

Then enter the backup directory and you will see the compressed files above.

cd tmp/backup/
ls

Unzip

tar xvf backup.tar

After decompression, there will be an extra var file. Enter it and you can see the jenkins_home directory. The content inside is the same as the data backed up in method 1. Use the mv command to move it to another directory

cd var/jenkins_home
mv

Compared with the first method, using --rm, we don't need to worry about resource recycling.

Method 3

Before trying the third method, we first go to the tmp directory and delete the jenkins_home and backup directories.

rm -rf jenkins_home/ backup/

View container id

docker ps | grep jenkins_im

Copy the container's jenkins_home directory to the current tmp directory

docker cp <container id>:var/jenkins_home /tmp/

Enter tmp to see if there is jenkins_home

cd tmp/
ls

Enter jenkins_home, you will see the same data as the previous backup, and finally do the same operation to move the content to another directory

mv

This is the end of this article on how to copy and backup docker container data. For more information about copying and backing up docker container data, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of psql database backup and recovery in docker
  • Docker uses the mysqldump command to back up and export mysql data in the project
  • Database backup in docker environment (postgresql, mysql) example code
  • Detailed explanation of docker command to backup linux system
  • How to use Docker containers to implement proxy forwarding and data backup
  • Detailed explanation of backup, recovery and migration of containers in Docker
  • Detailed explanation of Docker data backup and recovery process

<<:  WeChat applet scroll-view implements a solution to duplicate data loading when pulling up

>>:  Usage of MySQL time difference functions TIMESTAMPDIFF and DATEDIFF

Recommend

Full analysis of Vue diff algorithm

Table of contents Preface Vue update view patch s...

Solution to nginx hiding version number and WEB server information

Nginx can not only hide version information, but ...

How to modify the initial password of a user in mysql5.7

When users install MySQL database for the first t...

Common functions of MySQL basics

Table of contents 1. Common function classificati...

Network configuration of Host Only+NAT mode under VirtualBox

The network configuration of Host Only+NAT mode u...

How to use Docker to build a pypi private repository

1. Construction 1. Prepare htpasswd.txt file The ...

Nginx configuration location matching rules example explanation

The scope of nginx configuration instructions can...

Use JS to operate files (FileReader reads --node's fs)

Table of contents JS reads file FileReader docume...

JavaScript lazy loading detailed explanation

Table of contents Lazy Loading CSS styles: HTML p...

Pure CSS header fixed implementation code

There are two main reasons why it is difficult to...

11 ways to remove duplicates from js arrays

In actual work or interviews, we often encounter ...

Design Association: Why did you look in the wrong place?

I took the bus to work a few days ago. Based on m...

How to uninstall and reinstall Tomcat (with pictures and text)

Uninstall tomcat9 1. Since the installation of To...

How to build nfs service in ubuntu16.04

Introduction to NFS NFS (Network File System) is ...