How to Docker restart data is not lost, it teaches you to mount data volume Volume

When you use Docker deploy web applications or mysql database, you will find when the container after the restart, process vessels generated by the operation of the database or log data will be cleared, then how do we save the data? This need to know how to mount docker host disk directory for permanent storage of data.

Execution Docker Volume 1. Create a container

Use docker run command, a run Docker containers, using a mirror ubuntu / nginx, mounts local directory / tmp / source container to the directory / tmp / destination

docker run -itd --volume /tmp/source:/tmp/destination --name test ubuntu/nginx bash

We created a Docker container based ubuntu / nginx mirror.

Specify the name of the container for the test, designated by the --name option.

Docker Volume of --volume (may be abbreviated as -v) option is specified, the host / tmp / source catalog container / tmp / destination directory correspondence.

2. Check Docker Volume

Use docker inspect command, you can view details Docker containers:

docker inspect --format=’{{json .Mounts}}'test | python -m json.tool[{“Destination”: “/tmp/destination”,“Mode”: “”,“Propagation”: “”,“RW”: true,“Source”: “/tmp/source”,“Type”: “bind”}]

Use --format option, you can selectively view container information you need. .Mount Docker Volume information for the container.

python -m json.tool json string may be displayed formatted output.

Source represents a directory on the host, that is, / tmp / source.

Destination container in the directory, i.e., / tmp / destination.

3. The machine can be synchronized to the container document

New hello.txt file on the local / tmp / source directory

touch /tmp/source/hello.txtls /tmp/source/hello.txt

hello.txt visible in the container file / tmp / destination / directory

Use docker exec command, the command can be executed in the container.

docker exectest ls /tmp/destination/hello.txt

Therefore, modifications to the host directory / tmp / source / and to be synchronized to the container directory / tmp / destination / in.

4. The container file can be synchronized to the host

New world.txt file container / tmp / destination directory

docker exec test touch /tmp/destination/world.txtdocker exec test ls /tmp/destination/hello.txtworld.txt

world.txt visible in the host file / tmp / source / directory

ls /tmp/source/hello.txt world.txt

Published 225 original articles · won praise 385 · views 70000 +

Guess you like

Origin blog.csdn.net/qq_33709508/article/details/104788649
Recommended