CodeBlanks Logo Icon CodeBlanks

Summary - Volumes

Combine create, mount and remove to set up persistent data.

Summary: volumes

In this group:

  • The writable layer is temporary; persistent data is kept in a volume or bind mount.
  • Docker manages named volumes; they're created on first use and stay when the container is removed.
  • A bind mount mounts a host path; it's used in development for code and configuration files.
  • -v is the short form, --mount the explicit form; :ro/readonly is read-only.
  • docker volume ls/inspect/rm/prune manage them; backups are made by taking a tar with a temporary container.
docker run -d -v pgdata:/var/lib/postgresql/data postgres:16
docker run -d -v "$PWD/site":/usr/share/nginx/html:ro nginx

Practice Data and code

Write two commands: start a container named db from mysql:8 in the background with MYSQL_ROOT_PASSWORD=gizli, mounting the mysqldata volume at /var/lib/mysql; start a container named web from php:8.3-apache in the background mapping 8080 to 80 and mounting the current folder ("$PWD") at /var/www/html.

Starter code

terminal
# buraya yaz

Sample solution

terminal
docker run -d --name db -e MYSQL_ROOT_PASSWORD=gizli -v mysqldata:/var/lib/mysql mysql:8
docker run -d --name web -p 8080:80 -v "$PWD":/var/www/html php:8.3-apache

Expected output

708192a3b4c5
8192a3b4c5d6

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is the difference between -v ./veri:/data and -v veri:/data?

  • The second is temporary
  • The first is a bind mount, the second a named volume Correct answer
  • They are the same
  • The first is read-only

Explanation: If the source is a path it's a bind mount, if it's a plain name it's a volume.

2 Clean up unused volumes.

Question
docker volume ____ -f
Answer
docker volume prune -f

Explanation: prune deletes unused ones; -f doesn't ask.

3 Match.

Term Meaning
named volume Production data
bind mount Development code
:ro No writing
volume prune Cleanup

All lessons in Beginner