CodeBlanks Logo Icon CodeBlanks

The -v flag

Learn to mount volumes with the -v flag.

-v and --mount

There are two ways to write a volume mount. -v is short: source:target[:option]. --mount uses key=value form; it's longer but readable and reports errors more clearly:

docker run -d -v pgdata:/var/lib/postgresql/data postgres:16
docker run -d --mount type=volume,source=pgdata,target=/var/lib/postgresql/data postgres:16

Both give the same result.

Seeing the mounts

docker inspect shows a container's mounts with their types, sources and destinations:

$ docker inspect -f '{{range .Mounts}}{{.Type}} {{.Name}} -> {{.Destination}}{{"\n"}}{{end}}' db
volume pgdata -> /var/lib/postgresql/data

Practice Mount with --mount

Start a container named cache from redis:7 in the background; mount the redisdata volume at /data with --mount.

Starter code

terminal
# buraya yaz

Expected output

5e6f708192a3

Questions and answers

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

1 In a bind mount, if the host folder doesn't exist, what is the difference between -v and --mount?

  • -v silently creates the folder; --mount errors Correct answer
  • Both error
  • Both create it
  • --mount creates it; -v errors

Explanation: A mistyped path can silently become an empty folder with -v.

2 Give the target directory with --mount.

Question
docker run -d --mount type=volume,source=pgdata,____=/var/lib/postgresql/data postgres:16
Answer
docker run -d --mount type=volume,source=pgdata,target=/var/lib/postgresql/data postgres:16

Explanation: target (or destination/dst) is the container path.

3 Match.

Term Meaning
-v Short form
--mount Key=value form
type=volume Volume type
readonly Read-only in --mount

All lessons in Beginner