What to do if the container started by docker run hangs and loses data

What to do if the container started by docker run hangs and loses data

Scenario Description

In a certain system, the functional service is started using docker stack deploy xxx , and the service of a domestic database is started separately using docker run xxx . The database service does not mount the storage location;

As a result, the customer restarted the server... When logging into the server to restart the service, he found a problem: the previous data in the database might disappear (if docker run is used to start the service again).

Solution

Attempt 1

At first I thought the data was definitely lost, so I had to recover the data again, but the workload was too huge...

But there is no way, next time you start, just mount the storage to the hard disk, Orz

However, after discussing with my colleagues, I found a simpler (but not permanent) solution. See Attempt 2.

Attempt 2

A colleague mentioned that you can use docker start container_name to start the container again, so the data is still there. I tried it later and the data was still there... although it was only a temporary solution

Later I thought about it, if the data of the image started by docker is not mapped out, it will be stored in the default volume; even if the container is restarted with docker restart xxx, the changed data will still be there; that is to say, at this point, the server is restarted and the container is hung (use docker ps to view, the container status is Exited), but in fact the previous data is still in the default volume, and the changed data will only be lost when the container is deleted.

Verification Test

Just pack an image, start the container, create a file, stop it, start it again, and check if the file exists.

# Start the container ➜ docker_start_test docker run -itd --name docker_run_test 4cbf48630b46 ping 127.0.0.1
d6278f537113122d4ccbe00950790750215c5a09002bcbd1ef6f9e660fc9eaac
➜ docker_start_test docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d6278f537113 4cbf48630b46 "ping 127.0.0.1" 3 seconds ago Up 2 seconds docker_run_test
# Add files to the container ➜ docker_start_test docker exec -it docker_run_test /bin/sh
sh-4.2# pwd
/
sh-4.2# touch test
sh-4.2# exit
exit
# Restart the container ➜ docker_start_test docker stop docker_run_test
docker_run_test
➜ docker_start_test docker ps -a | grep docker_run_test
d6278f537113 4cbf48630b46 "ping 127.0.0.1" About a minute ago Exited (137) 12 seconds ago docker_run_test
# Go in and check if the file exists ➜ docker_start_test docker start docker_run_test
docker_run_test
➜ docker_start_test docker exec -ti docker_run_test /bin/sh
sh-4.2# ls
anaconda-post.log bin dev etc home lib lib64 lost+found media mnt opt ​​proc root run sbin srv sys test tmp usr var

As you can see, the file test still exists. If you stop the container, delete the container using docker rm, and restart a container with the same name, you can see that there is no test file in the container.

# stop / rm the container ➜ docker_start_test docker stop docker_run_test
docker_run_test
➜ docker_start_test docker ps -a | grep docker_run
d6278f537113 4cbf48630b46 "ping 127.0.0.1" 7 minutes ago Exited (137) 13 seconds ago docker_run_test
➜ docker_start_test docker rm d6278f537113
d6278f537113
# Start a new container with the same name ➜ docker_start_test docker run -itd --name docker_run_test 4cbf48630b46 ping 127.0.0.1
99a6f5df0a86e4c07abf184e322a23e4fbec89ff354691459cdac8fcd8687ba3
# Enter the container to verify ➜ docker_start_test docker exec -ti docker_run_test /bin/sh
sh-4.2# ls
anaconda-post.log bin dev etc home lib lib64 lost+found media mnt opt ​​proc root run sbin srv sys tmp usr var
sh-4.2# ls test
ls: cannot access test: No such file or directory

Instructions for docker run

From the official website, the function of the start command is:

Start one or more stopped containers

Emmm, pretty straightforward, nothing much to say

PS

In fact, the best way is to mount the storage directory of the container... In addition, generally speaking, it seems that database services should not be started using containers

Summarize

The above is what I introduced to you about what to do if the container started by docker run hangs up the data. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • docker run -v mounts data volumes abnormally, and the container status is always restarting
  • A brief discussion on the problem of Docker run container being in created state
  • Solution to the automatic termination of docker run container
  • Docker Runc container life cycle detailed introduction
  • Solve the problem of the container showing Exited (0) after docker run

<<:  MySQL database monitoring software lepus usage problems and solutions

>>:  Example of Vue routing listening to dynamically load the same page

Recommend

How to create your first React page

Table of contents What is Rract? background React...

Difference between MySQL update set and and

Table of contents Problem Description Cause Analy...

Full analysis of Vue diff algorithm

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

How to implement the builder pattern in Javascript

Overview The builder pattern is a relatively simp...

html option disable select select disable option example

Copy code The code is as follows: <select> ...

A brief understanding of the difference between MySQL union all and union

Union is a union operation on the data, excluding...

Html Select uses the selected attribute to set the default selection

Adding the attribute selected = "selected&quo...

How to connect to MySQL database using Node-Red

To connect Node-red to the database (mysql), you ...

The button has a gray border that is ugly. How to remove it?

I used the dialog in closure and drew a dialog wit...

Example code of how to create a collapsed header effect using only CSS

Collapsed headers are a great solution for displa...

Installation process of CentOS8 Linux 8.0.1905 (illustration)

As of now, the latest version of CentOS is CentOS...

Element uses scripts to automatically build new components

Table of contents background How does element-ui&...

How to use Docker to build a pypi private repository

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