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

js implements shopping cart addition and subtraction and price calculation

This article example shares the specific code of ...

Summary of Ubuntu backup methods (four types)

Method 1: To use respin, follow these steps: sudo...

Analysis of rel attribute in HTML

.y { background: url(//img.jbzj.com/images/o_y.pn...

In-depth understanding of Vue's data responsiveness

Table of contents 1. ES syntax getter and setter ...

Zookeeper unauthorized access test problem

Table of contents Preface Detect Zookeeper servic...

12 Useful Array Tricks in JavaScript

Table of contents Array deduplication 1. from() s...

The role and opening of MySQL slow query log

Preface The MySQL slow query log is a type of log...

Vue uses Baidu Maps to realize city positioning

This article shares the specific code of Vue usin...

CentOS6.8 uses cmake to install MySQL5.7.18

Referring to the online information, I used cmake...

Design Theory: Textual Expression and Usability

<br />In text design, we usually focus on th...

MySql implements page query function

First of all, we need to make it clear why we use...

A brief analysis of the difference between FIND_IN_SET() and IN in MySQL

I used the Mysql FIND_IN_SET function in a projec...

Perfect Solution for No rc.local File in Linux

Newer Linux distributions no longer have the rc.l...

A complete example of implementing a timed crawler with Nodejs

Table of contents Cause of the incident Use Node ...