Docker Data Storage - Volume Mounts

Q

How to create a new Volume mount to a Docker container?

✍: FYIcenter.com

A

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

Managing Data Storage in Docker

⇑⇑ Docker Container Platform - Tutorials

2021-08-15, 186👍, 0💬