04 / 06

How do you mount a volume to a container?

Difficulty: 5/10
bind mounts, named volumes, data persistence

You mount a volume to a container using the -v or --mount flags with the docker run command, specifying which volume to mount and where to mount it inside the container.

Mounting a volume to a container makes the persistent data stored in the volume available at a specific path inside the container. Docker provides two syntax options for mounting volumes: the simpler -v (or --volume) flag, and the more explicit --mount flag. Both achieve the same result, but --mount is recommended for production environments because it's more verbose and less prone to errors.

Method 1: Using -v or --volume syntax
Method 2: Using --mount syntax (recommended)
Understanding Volume Mount Syntax
  1. 1

    The -v flag uses a compact format: -v volume-name:container-path:options where options like ro (read-only) can be added .

  2. 2

    The --mount flag uses explicit key-value pairs separated by commas: --mount type=volume,source=myvol,target=/app,readonly .

  3. 3

    If the specified volume doesn't exist, Docker automatically creates it for both syntaxes .

  4. 4

    You can mount the same volume to multiple containers, enabling data sharing between them .

  5. 5

    The container path must be an absolute path, and Docker will create the directory if it doesn't exist .

After mounting, any data written to the container's mount path is actually stored in the volume and persists even after the container is removed. You can verify the mount worked by inspecting the container with docker inspect and looking at the Mounts section, which shows the source volume, destination path, and mount options. For MySQL or PostgreSQL containers, this pattern is essential because it ensures database files survive container updates or crashes.

Scenario Questions

0-2 years experience

  1. 1We have a simple Flask app that writes logs to /app/logs. How would you run the container so that the logs are persisted on the host?
  2. 2If you start a container with `docker run -v /data:/app/data myimage` and the host's /data directory is empty, what will you see inside the container at /app/data after it starts?
  3. 3You need to inject a configuration file from the host into /etc/myapp/config.yaml in the container. Which Docker flag would you use and how would you specify it?

2-5 years experience

  1. 1Your team added a volume to store uploaded user files, but after a recent deployment the container can't see the existing files. Walk me through how you would troubleshoot this issue.
  2. 2We use a named volume for a database in Docker Compose. Explain the trade‑offs between using a bind mount versus a named volume for this use case.
  3. 3During CI a test suite fails because a volume is left read‑only after a previous run. How would you modify the Compose file to guarantee a clean, writable volume each time?

5-8 years experience

  1. 1Our microservice platform runs dozens of containers on shared hosts, each with its own data volume. How would you design a volume strategy to avoid contention and maintain performance at scale?
  2. 2We need to migrate from host‑based bind mounts to a distributed storage solution like NFS or a CSI driver. What changes would you make to the deployment pipeline and what risks would you monitor?
  3. 3Explain how Docker's copy‑on‑write behavior for volumes can affect startup time and disk usage when containers need large pre‑populated data sets.

8+ years experience

  1. 1The company is moving from monolithic Docker deployments to a multi‑cluster Kubernetes fleet and must keep data consistent across clusters. How would you architect the volume solution, considering backup, restore, and cross‑region replication?
  2. 2Legacy services rely on host‑path volumes that tie them to specific OS layouts. Propose a roadmap to refactor these services to use platform‑agnostic storage while minimizing downtime.
  3. 3When evaluating storage for a high‑throughput analytics pipeline, compare Docker volumes, bind mounts, and external block storage in terms of latency, scalability, and operational overhead.

Follow-up Questions

  • What would happen if the host directory doesn't exist before you run the container?
  • How would you handle permission issues on the mounted volume?
  • Can you describe how you would clean up orphaned volumes in production?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.