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

The use and difference between vue3 watch and watchEffect

1.watch listener Introducing watch import { ref, ...

Basic knowledge: What does http mean before a website address?

What is HTTP? When we want to browse a website, w...

Detailed tutorial on installing CentOS, JDK and Hadoop on VirtualBox

Table of contents 1. Prerequisites 1.1 Supported ...

In-depth explanation of closure in JavaScript

Introduction Closure is a very powerful feature i...

Thoughts on truncation of multi-line text with a "show more" button

I just happened to encounter this small requireme...

Vue two same-level components to achieve value transfer

Vue components are connected, so it is inevitable...

Linux operation and maintenance basics httpd static web page tutorial

Table of contents 1. Use the warehouse to create ...

Delegating Privileges in Linux Using Sudo

Introduction to sudo authority delegation su swit...

Solve the problem that ElementUI custom CSS style does not take effect

For example, there is an input box <el-input r...

js date and time formatting method example

js date time format Convert the date and time to ...

30 free high-quality English ribbon fonts

30 free high-quality English ribbon fonts for down...

Example of how to modify styles via CSS variables

question How to modify CSS pseudo-class style wit...

Detailed steps for quick installation of openshift

The fastest way to experience the latest version ...