Get the App
SLTechnology News&Howtos  ›  Database  › 

What is Docker Volume?

Shulou Source: shulou.com Published: 2022-06-01 10:27:37 09月11日 Update

Docker Volume, usually translated as a data volume, is used to store persistent data. When we run a database such as MySQL in a Docker container, the data is generally saved on the host through Docker Volume, so that even if the MySQL container is deleted, the data is still saved on the host, effectively ensuring the security of the data. This blog will help you understand Docker Volume through simple practices.

All commands in this article are executed on an online Docker instance of play-with-docker, Docker version 17.05.0-ce.

1. Specify Docker Volume

Docker container can be run using docker run command

docker run -itd --volume /tmp/data1:/tmp/data2--name test ubuntu bash creates a Docker container based on the ubuntu image. The name of the container is test, specified by the--name option. Docker Volume is specified by the--volume option, and the/tmp/data1 directory of the host corresponds to the/tmp/data2 directory in the container. 2. View Docker Volume

Using the docker inspect command, you can view the details of Docker containers:

docker inspect --format='{{json .Mounts}}' test | python -m json.tool[ { "Destination": "/tmp/data2", "Mode": "", "Propagation": "", "RW": true, "Source": "/tmp/data1", "Type": "bind" }] Use the--format option to selectively view desired container information. Mount is the Docker Volume information of the container. python -m json.tool formats the output json string for display. Source represents the directory on the host, which is/tmp/data1. Destination is the directory in the container, i.e./tmp/data2. 3. Native files can be synchronized to containers Create a new hello.txt file in the native/tmp/data1 directory Touch /tmp/data1/hello.txtls /tmp/data1/hello.txthello.txt File visible in container/tmp/data2/directory

With the docker exec command, you can execute commands in containers.

docker exec test ls /tmp/data2/hello.txt

As you can see, changes made in the local directory/tmp/data1/can be synchronized to the container directory/tmp/data2/.

4. Container files can be synchronized to the host. Create a new world.txt file in the container/tmp/data2 directory docker exec test touch /tmp/data2/world.txtdocker exec test ls /tmp/data2/hello.txtworld.txtworld.txt file in the host/tmp/data1/directory ls/tmp/data1/hello.txt world.txt

As you can see, changes in the container directory/tmp/data2/can be synchronized to the host directory/tmp/data1/.

5. conclusion

Docker volumes are essentially directories or files shared between containers and hosts, so that data in Docker volumes can be synchronized in real time between hosts and containers. When creating virtual machines using Virtualbox, you can also configure shared directories, much like Docker Volume.

About Fundebug

Fundebug focuses on JavaScript, Weixin Mini Programs, WeChat Mini games, Alipay Mini Programs, React Native, Node.js and Java real-time BUG monitoring. Since the official launch of Double Eleven in 2016, Fundebug has handled a total of 700 million + error events, which has been recognized by many well-known users such as Google, 360, Jinshan Software and People's Network. Welcome to try it for free!

copyright notice

Please indicate the author Fundebug and the address of this article when reprinting:

https://blog.fundebug.com/2017/06/07/what-is-docker-volume/

Tags: Containers directories hosts data files commands synchronization information local machines programs Zhongxin running effective similar well-known one-to-one correspondence between events free trial double ten Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi vpn Huawei Shulou Information Docker