What if the container started by docker run has dropped its data

  • 2021-06-28 09:51:55
  • OfStack

Scenario Description

Functional service usage in a system docker stack deploy xxx Start, a domestic database service is used separately docker run xxx The database service did not mount the storage location when it started.

As a result, the client restarted the server.. When he logged in to the server to restart the service, he found a problem where the data in the database might disappear (if docker run was used to start again).

Terms of settlement

Try 1

The initial thought was that the data must have been lost, so you can only toss around the data once again, but the workload is too much..

But there's no way. Next time you boot up, just mount the storage on your hard disk, Orz

However, after communicating with colleagues, I found a simpler (but not permanent) solution, see Attempt 2

Try 2

Colleagues mentioned that you can use docker start container_name Start the container again so that the data is still there.After a try, the data is still there... although it's only a temporary solution

After thinking about it, the image started by docker will be stored in the default volume if the data is not mapped.Even if the container is restarted with docker restart xxx, the changed data still exists;That is, in this place, the server restarts and the container hangs up (checked using docker ps, whose container status is Exited), but in fact the previous data is still under the default volume, and only when the container is deleted will the changed data be lost.

Validation Tests

Package a mirror, start the container, create a file, stop and start again to see if the file exists


#  Start 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
#  Increase Files Into 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 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 see 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

You can see that the file test still exists;If you stop the container, use docker rm to delete the container and restart a container with the same name, you will see that there are no more test files in the container


# stop / rm Drop 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 Container Validation 
➜ 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

Description of docker run

From the official website, the start command is designed to:

Start one or more stopped containers

emmm, straight up, nothing to say

PS

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

summary


Related articles: