Steps to restore code from a Docker container image

Steps to restore code from a Docker container image

Sometimes the code is lost and you need to recover the required code from the image running in the container. At this time, you only need to perform a few simple operations on the server running the container to extract the code used when packaging the image.

View all containers:

docker container ls -a

Enter the specified container according to the container ID:

docker exec -ti id /bin/bash

Copy the directory in the container to the service-specified folder:

docker container cp id:/usr/local/tomcat/webapps/province-admin /home/test/province-admin

After the above 3 steps, the code in the container has been extracted to the specified directory of the server. You can view the required code by downloading the code from the server to the local computer.

Supplement: Docker data volume container backup and recovery are super detailed! Xiaobai can do it too! ! !

In daily life, we map the directory between the container and the local physical machine and store it directly locally. Then we only need to back up the local hard disk regularly. But if there is no such mapping, how should we handle backup and recovery?

First we create a data volume that needs to be backed up.

docker run -itd -v /opt/zz --name c1 centos /bin/bash

-itd is run in interactive mode, d is run in the background

-v specifies the data volume in the container. If the specified data volume does not exist, it will be created automatically.

–name Customize the name

/bin/bash Interactive commands are executed with /bin/bash

To verify the backup function of the data volume container, create a c11 file in the mounted /opt/zz directory and write the content "woda".

[root@160e0646396d zz]# touch c11.txt
[root@160e0646396d zz]# echo "woda" > c11.txt 
[root@160e0646396d zz]# cat c11.txt
woda

Then, to back up the volume container, use --volumes-from to mark the volume to be backed up and mount the current directory from the host to the container's /v3 directory.

$(pwd) is a method supported by docker to specify the current directory. Those who understand the basic commands of linux will find that the pwd command is used to view the current directory in linux.

After the container is started, the c23.tar file generated in the current directory is the backup file of the /opt/zz container volume.

In this way, the data in the data volume container is backed up.

The whole practice process is as follows:

[root@client ~]# docker run --volumes-from c1 -v $(pwd):/v3 centos tar cvf /v3/c23.tar /opt/zz
tar: Removing leading `/' from member names
/opt/zz/
/opt/zz/c11.txt

Finally, we can see the contents of the data volume of the container that we need to back up, /opt/zz/c11.txt

–volumes-from <Container>: Connect to the container to back up data

-v $(pwd):/v3: Mount the current path to the container centos container, and the data will be backed up to this path

centos: Very small image

tar cvf /v3/c23.tar /opt/zz: pack the files in the /opt/zz path into c23.tar

Second recovery

The ultimate purpose of backup is to be able to recover, otherwise the backup is meaningless. Docker backup recovery is also very simple, only two steps are required.

First, create a container /opt/x2 with an empty data volume.

docker run -itd -v /opt/x2 --name c3 centos /bin/bash
docker run -itd -v /opt/x2 --name c3 centos /bin/bash

Then, create another container, mount the data volume in the data1 container volume, and use untar to decompress the backup file into the mounted container volume.

docker run --volumes-from c3 -v $(pwd):/v3 centos tar xvf /v3/c23.tar

At this time, the data of the previously backed up data volume container has been restored to the container /opt/x2.

In order to view and verify the recovered data, we can start another container and mount the /opt/x2 container volume to view it.

The whole practice process is as follows:

[root@client ~]# docker run --volumes-from c3 -v $(pwd):/v3 centos tar xvf /v3/c23.tar
opt/zz/
opt/zz/c11.txt

Finally, we can see that the c11.txt we backed up earlier has now been restored to the data volume in the c3 container.

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • Detailed explanation of Docker data backup and recovery process
  • Detailed explanation of psql database backup and recovery in docker
  • Detailed explanation of backup, recovery and migration of containers in Docker
  • Detailed explanation of Docker private warehouse recovery example
  • How to restore docker container data

<<:  Detailed description of mysql replace into usage

>>:  Example code for CSS to achieve horizontal lines on both sides of the text

Recommend

MySQL primary key naming strategy related

Recently, when I was sorting out the details of d...

Python3.6-MySql insert file path, the solution to lose the backslash

As shown below: As shown above, just replace it. ...

Method of Vue component document generation tool library

Table of contents Parsing .vue files Extract docu...

MySQL data type details

Table of contents 1. Numeric Type 1.1 Classificat...

React+ts realizes secondary linkage effect

This article shares the specific code of React+ts...

Specific use of Linux dirname command

01. Command Overview dirname - strip non-director...

Detailed explanation of redundant and duplicate indexes in MySQL

MySQL allows you to create multiple indexes on th...

Using iframe techniques to obtain visitor QQ implementation ideas and sample code

Today at work, a friend I added temporarily asked ...

CSS code to achieve 10 modern layouts

Preface I watched web.dev's 2020 three-day li...

Example code of vue custom component to implement v-model two-way binding data

In the project, you will encounter custom public ...

Solution to MySQL root password error number 1045

Stop MySQL Service Windows can right-click My Com...

Detailed explanation of the entry-level use of MySql stored procedure parameters

Use of stored procedure in parameters IN paramete...

Detailed explanation of MySQL sql99 syntax inner join and non-equivalent join

#Case: Query employee salary levels SELECT salary...