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

Vue+Router+Element to implement a simple navigation bar

This project shares the specific code of Vue+Rout...

MariaDB under Linux starts with the root user (recommended)

Recently, due to the need to test security produc...

JavaScript implements fireworks effects with sound effects

It took me half an hour to write the code, and th...

Share the pitfalls of MySQL's current_timestamp and their solutions

Table of contents MySQL's current_timestamp p...

Vue3.0+vite2 implements dynamic asynchronous component lazy loading

Table of contents Create a Vite project Creating ...

Summary of the minesweeping project implemented in JS

This article shares the summary of the JS mineswe...

vue3.0+echarts realizes three-dimensional column chart

Preface: Vue3.0 implements echarts three-dimensio...

Vue+Openlayer uses modify to modify the complete code of the element

Vue+Openlayer uses modify to modify elements. The...

Detailed steps for debugging VUE projects in IDEA

To debug js code, you need to write debugger in t...

A brief discussion on the principle of Vue's two-way event binding v-model

Table of contents explain: Summarize Replenish Un...

How to import and export Cookies and Favorites in FireFox

FireFox is a commonly used browser with many exte...

Learn javascript iterator

Table of contents Introduction What does an itera...

How to redirect PC address to mobile address in Vue

Requirements: The PC side and the mobile side are...

JavaScript color viewer

This article example shares the specific code of ...