23.3e Volume mounts: -v and --mount for persisting training datasets and checkpoints¶
🧠 Context Introduction¶
When training AI models inside Docker containers, one critical challenge arises: containers are ephemeral. By default, any data you create inside a container — including trained model checkpoints, logs, or downloaded datasets — disappears when the container stops. For AI workloads, this is unacceptable. You need a way to persist data across container restarts and share data between your host machine and containers.
This is where volume mounts come in. They allow you to attach a directory from your host filesystem directly into a container's filesystem. Think of it as creating a "window" between your host and the container — anything saved in that window is visible on both sides.
⚙️ Why Volume Mounts Matter for AI Workflows¶
AI training pipelines have three specific needs that volume mounts solve:
- Persisting checkpoints: Training runs can last hours or days. If a container crashes mid-training, you need to resume from the last saved checkpoint — not start over.
- Accessing large datasets: Downloading terabytes of training data into every new container is wasteful. Instead, mount a dataset directory once on your host and share it with any container.
- Sharing results: After training completes, you need to copy model weights and logs to your host for evaluation or deployment. Volume mounts make this seamless.
🛠️ The Two Volume Mount Methods: -v vs --mount¶
Docker provides two syntaxes for mounting volumes. Both achieve the same goal, but they differ in readability and flexibility.
📊 Comparison Table¶
| Feature | -v (Short Syntax) |
--mount (Long Syntax) |
|---|---|---|
| Readability | Compact, but can be cryptic | Verbose, self-documenting |
| Flexibility | Limited options | Supports advanced options (read-only, tmpfs, etc.) |
| Best for | Quick prototyping, simple mounts | Production scripts, complex configurations |
| Syntax style | Single string with colons | Key-value pairs separated by commas |
🕵️ How -v Works (Short Syntax)¶
The -v flag uses a colon-separated format: host_path:container_path:options
- The host_path is the absolute path on your machine where data lives.
- The container_path is where that data appears inside the container.
- The options (optional) control behavior like read-only access.
For example, to mount a dataset directory from your host into a container:
- Host path: /home/user/datasets/coco
- Container path: /data/training
- Result: Any file in /home/user/datasets/coco on your host appears in /data/training inside the container.
If the host path doesn't exist, Docker will automatically create it as an empty directory. This is convenient but can lead to surprises if you misspell a path.
🧩 How --mount Works (Long Syntax)¶
The --mount flag uses explicit key-value pairs for clarity:
- type=bind — indicates a bind mount (the most common type for AI workloads)
- source= — the host path
- target= — the container path
- readonly — optional flag to prevent the container from modifying host files
This syntax is more verbose but eliminates ambiguity. It also supports additional mount types like tmpfs (in-memory storage) and volume (Docker-managed volumes).
📊 Visual Representation: Bind Mounts vs. Named Volumes¶
This diagram contrasts Bind Mounts (linking directly to a specific host folder) with Named Volumes (managed internally by the Docker daemon).
🎯 Practical AI Use Cases¶
📥 Persisting Training Datasets¶
When you have a large dataset (e.g., ImageNet, COCO, or custom data) stored on your host machine: - Mount the dataset directory as read-only to prevent accidental modification. - Multiple containers can share the same dataset simultaneously without copying. - The dataset survives container restarts and removals.
💾 Saving Model Checkpoints¶
During training, frameworks like PyTorch or TensorFlow save checkpoint files periodically:
- Mount a dedicated output directory (e.g., /workspace/checkpoints).
- After training, the checkpoints are immediately available on your host.
- If the container crashes, you can restart it with the same mount and resume training.
📝 Logging and Monitoring¶
Training logs, TensorBoard events, and metrics files: - Mount a logs directory to capture real-time training progress. - View logs on your host while the container is still running. - Preserve logs for post-training analysis.
🔍 Key Differences Between -v and --mount in Practice¶
- Path creation: With
-v, Docker creates missing host paths automatically. With--mount, Docker throws an error if the host path doesn't exist — preventing silent failures. - Read-only enforcement:
--mounthas a dedicatedreadonlyoption. With-v, you append:roto the end of the string. - Mount types:
--mountsupportstype=tmpfsfor temporary in-memory storage (useful for high-speed data loading).-vdoes not support this directly.
🧪 Common Pitfalls for New Engineers¶
- Absolute paths required: Both methods require absolute host paths (starting with
/on Linux/macOS or a drive letter on Windows). Relative paths like./datawill fail or behave unexpectedly. - Permission issues: Files created inside a container may have different ownership than your host user. You may need to adjust user IDs (UID) or use
chmodon the host. - Overwriting container directories: If you mount a host directory over an existing container directory (e.g.,
/workspace), the container's original contents are hidden. Always mount to an empty or dedicated path. - Performance considerations: Bind mounts (the default for both
-vand--mount) have near-native performance. However, mounting over network drives (NFS) can introduce latency.
✅ Best Practices for AI Engineers¶
- Always use
--mountin production scripts — it's self-documenting and prevents path-creation surprises. - Use
-vfor quick experiments — it's faster to type during interactive development. - Mount datasets as read-only — add
readonly(with--mount) or:ro(with-v) to protect your data. - Separate checkpoints from datasets — use one mount for input data and another for output checkpoints.
- Verify mounts at container start — check that your mounted directories contain expected files before launching training.
📌 Summary¶
Volume mounts are the bridge between your host's persistent storage and Docker's ephemeral containers. For AI workloads, they are essential for:
- Keeping training datasets accessible without copying them into every container.
- Saving model checkpoints that survive container crashes and restarts.
- Sharing results between containers and your host environment.
The -v flag offers simplicity for quick tasks, while --mount provides clarity and control for production pipelines. Master both, and you'll avoid one of the most common mistakes new engineers make: losing hours of training progress because data wasn't persisted.