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

How to use bar charts in Vue and modify the configuration yourself

1. Import echart in HTML file <!-- Import echa...

Docker installation and deployment of Net Core implementation process analysis

1. Docker installation and settings #Install Cent...

Detailed explanation of creating and calling MySQL stored procedures

Table of contents Preface Stored Procedure: 1. Cr...

How to set process.env.NODE_ENV production environment mode

Before I start, let me emphasize that process.env...

How to obtain a permanent free SSL certificate from Let's Encrypt in Docker

1. Cause The official cerbot is too annoying. It ...

CentOS6.8 Chinese/English environment switching tutorial diagram

1. Introduction People who are not used to Englis...

Incredible CSS navigation bar underline following effect

The first cutter in China github.com/chokcoco Fir...

Linux/Mac MySQL forgotten password command line method to change the password

All prerequisites require root permissions 1. End...

Linux uses lsof command to check file opening status

Preface We all know that in Linux, "everythi...

uniapp implements date and time picker

This article example shares the specific code of ...

Solution to the problem that VC6.0 cannot be used when installed on WIN10

VC6.0 is indeed too old VC6.0 is a development to...

Introduction to Enterprise Production MySQL Optimization

Compared with other large databases such as Oracl...

Solve the MySQL login 1045 problem under centos

Since the entire application needs to be deployed...