What is the difference between the -v and --mount flags in Docker?
Both attach storage. -v is a compact colon-separated string, while --mount uses explicit key=value pairs. --mount is clearer and errors if a bind path is missing, while -v silently creates the host folder.
Explain it simply
-v is shorthand like writing 'Aman - 9 - B' on a slip. --mount is the full form with labels: name, roll number, section. Same information, harder to misread.
-v data:/app/data:ro packs source, destination and options into one string, and the meaning of the first part depends on its shape: a path means bind mount, a bare name means named volume. --mount type=volume,source=data,target=/app/data,readonly spells out everything. A key behavioural difference: with -v /host/path:/app and a missing host path, Docker creates an empty directory, often owned by root, which hides typos. With --mount type=bind, a missing source path produces an error.
Both work in Compose (short and long syntax). Docker documentation recommends --mount for new work because it is explicit.
Example
docker run -v data:/app/data:ro myapp
docker run --mount type=volume,source=data,target=/app/data,readonly myapp
docker run --mount type=bind,source="$(pwd)",target=/app myappWhat to say in the interview
"-v is the short form and --mount is the explicit key=value form. --mount fails when a bind source is missing, while -v creates the folder silently, so I use --mount when clarity matters."
Common mistakes
- Mistyping a host path with -v and getting an empty auto-created folder.