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 = 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 \
nginx:latest

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 \
nginx:latest

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"
services:
web:
image: nginx:alpine
volumes:
# Create volume
- type: volume
source: mydata
target: /data
volume:
nocopy: true
# Bind volume
- type: bind
source: ./static
target: /opt/app/static

db:
image: postgres:latest
volumes:
- "/var/run/postgres/postgres.sock:/var/run/postgres/postgres.sock"
- "dbdata:/var/lib/postgresql/data"

volumes:
mydata:
dbdata:

πŸŽƒ Appendix: Container to host

If you want a Docker container to connect to host machine, please use the follows

  • Docker for Mac: docker.for.mac.localhost
  • Docker for Windows: docker.for.win.localhost

🎳 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!!