Basic knowledge for Docker Volume

Docker Volumes are the mechanism for persiting data.

In thtis article, I would like to share basic knowledge about Docker volume.

🐮 Advantage in Volume

Volumes have some advantages over bind mounts:

  • Volumes are asier to back up
  • You can easily manage values using Docker CLI commands
  • Volumes can be more safely shared among multiple containers
  • Volume’s contents exist outside the lifecycle of a given container

from Use bind mounts | Docker Documentation

🐞 --mount option

There are -v, --mount flag for volume. We should use the --mount syntax.

Consits of multiple key-value pairs, separated by commas and each consisting of a <key>=<value> tuple.

key Value
source For named vlumes, this is the name of the volume. For anonymous vlumes, this field is omitted.
destination it takes as its value the path where the file or directory will be mouted in the container.
readonly if present, causes the bind mount to be mouted into the container as read-only.
volume-opt it can be specified more than once, takes a key-value pair consisting of the option name and its value

😎 Create and manage volume

# Create a volume
docker volume create my-vol
# List volumes
docker volume ls
# Inspect a volume
docker volume inspect my-vol
# Remove a volume
docker volume rm my-vol

🏀 Start a container with a volume

If you start a container with a volume that does not yet exist, Docker creates the volume. The following example mounts the volume myvol2 into /app/ in the contaienr.

# Start a container with a volume
docker run -d \
-it \
--name devtest \
--mount source=myvol2,destination=/app \

Use docker inspect devtest to verify that the volume was created and mounted correctly. Look for the Mounts section:

"Mounts": [
"Type": "volume",
"Name": "myvol2",
"Source": "/var/lib/docker/volumes/myvol2/_data",
"Destination": "/app",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""

🐡 Start a container with a bind mount

docker run -d \
-it \
--name devtest \
--mount type=bind,source="$(pwd)"/hoge,target=/app \

Use docker inspect devtest to verify that the bind mount was created correctly. Look for the Mounts section:

"Mounts": [
"Type": "bind",
"Source": "/tmp/hoge",
"Destination": "/app",
"Mode": "",
"RW": true,
"Propagation": "rprivate"

🐠 Volume configuration for docker-compose

version: "3.2"
image: nginx:alpine
# Create volume
- type: volume
source: mydata
target: /data
nocopy: true
# Bind volume
- type: bind
source: ./static
target: /opt/app/static
image: postgres:latest
- "/var/run/postgres/postgres.sock:/var/run/postgres/postgres.sock"
- "dbdata:/var/lib/postgresql/data"

🍣 Special Thanks

🖥 Recommended VPS Service

VULTR provides high performance cloud compute environment for you. Vultr has 15 data-centers strategically placed around the globe, you can use a VPS with 512 MB memory for just $ 2.5 / month ($ 0.004 / hour). In addition, Vultr is up to 4 times faster than the competition, so please check it => Check Benchmark Results!!