Tools, FAQ, Tutorials:
Docker Data Storage - Volume Mounts
How to create a new Volume mount to a Docker container?
✍: FYIcenter.com
Volume mounts are stored in a part of the host filesystem which is managed by Docker
(/var/lib/docker/volumes/ on Linux). Non-Docker processes should not modify this
part of the filesystem. Volumes are the best way to persist data in Docker.
Before creating a Volume mount to a new Docker container, you need to create Docker volume first with the "docker volume" command.
fyicenter# docker volume create fyi-vol fyi-vol fyicenter# docker volume ls DRIVER VOLUME NAME local fyi-vol
Find out where the "fyi-vol" volume is located on the hosting system:
fyicenter# docker volume inspect fyi-vol [ { "CreatedAt": "2021-08-15T15:30:33+08:00", "Driver": "local", "Labels": {}, "Mountpoint": "/var/lib/docker/volumes/fyi-vol/_data", "Name": "fyi-vol", "Options": {}, "Scope": "local" } ]
You have two options "--volume" and "--mount" on the "docker container create/run" command to create volume mounts.
"--volume" Option - Allows you to mount a "volume" file system to the new Docker container with default configuration.
"--mount source=..." Option - Allows you to mount a volume file system to the new Docker container with specific configurations.
For example, the following command "docker run --volume" runs a new container with an existing volume filesystem mounted at /app.
fyicenter# docker run --volume fyi-vol:/app -tid --name fyi_test alpine fyicenter# docker exec --tty --interactive fyi_test /bin/sh / # df -h Filesystem Size Used Available Use% Mounted on overlay 50.0G 22.3G 27.7G 45% / tmpfs 64.0M 0 64.0M 0% /dev tmpfs 3.7G 0 3.7G 0% /sys/fs/cgroup shm 64.0M 0 64.0M 0% /dev/shm /dev/mapper/cl-root 50.0G 22.3G 27.7G 45% /app /dev/mapper/cl-root 50.0G 22.3G 27.7G 45% /etc/resolv.conf /dev/mapper/cl-root 50.0G 22.3G 27.7G 45% /etc/hostname /dev/mapper/cl-root 50.0G 22.3G 27.7G 45% /etc/hosts
Create a test file in /app.
/ # touch /app/fyi_file / # ls -l /app -rw-r--r--. 1 root root 0 fyi_file
Stop and remove the docker container. Then verify the test file.
/ # exit fyicenter# docker container stop fyi_test fyi_test fyicenter# docker container rm fyi_test fyi_test fyicenter# ls -l /var/lib/docker/volumes/fyi-vol/_data -rw-r--r--. 1 root root 0 fyi_file
You can also create a volume mount the "docker run --mount source=...,destination=..." option as shown below.
fyicenter# docker run --mount source=fyi-vol,destination=/app -tid --name fyi_test alpine fyicenter# docker exec -it fyi_test /bin/sh / # ls -l /app -rw-r--r-- 1 root root 0 fyi_file / # exit fyicenter# docker container stop fyi_test fyi_test fyicenter# docker container rm fyi_test fyi_test
⇒ Docker Data Storage - Bind Mounts
⇐ Docker Data Storage - "tmpfs" Mounts
2021-08-15, 549👍, 0💬
Popular Posts:
What is Azure API Management Publisher Dashboard? Azure API Management Publisher Dashboard is an Azu...
How to include additional claims in Azure AD v2.0 id_tokens? If you want to include additional claim...
How To Use an Array as a Queue in PHP? A queue is a simple data structure that manages data elements...
How to install .NET Framework in Visual Studio Community 2017? I have the Visual Studio Installer in...
How To Loop through an Array without Using "foreach" in PHP? PHP offers the following functions to a...