tezvyn:

Docker Bind Mounts: A Portal to Your Host Filesystem

AI-drafted, machine-checkedSource: docs.docker.comintermediate

A bind mount is a portal from your host machine's filesystem directly into a container, where changes on either side are reflected instantly. Use it for live code development, but never for production data, as it creates a major security risk.

WHY IT EXISTS: Containers need a way to interact with files on the host machine, especially during development. Rebuilding an image for every single code change is slow and inefficient. Bind mounts solve this by creating a live link between the host and the container for rapid iteration and for sharing configuration.

THE MENTAL MODEL: A bind mount is a direct, two-way portal between a specific file or directory on your host machine and a path inside a container. It is not a copy; it is the exact same data on disk. The container process reads and writes to the host filesystem as if it were its own, with no abstraction layer managed by Docker.

HOW IT WORKS: You provide a mapping from a source path on the host to a target path in the container, using the -v or --mount flag (e.g., -v /path/on/host:/path/in/container). When the container starts, Docker ensures that any filesystem access to the target path inside the container is redirected to the source path on the host.

WHEN TO USE IT: The primary use case is local development. Mount your source code into a container running a server with a file watcher (like nodemon) for instant live reloads. It's also useful for providing configuration files from the host or for advanced use cases like accessing the Docker socket itself (/var/run/docker.sock).

WHEN NOT TO USE IT: Avoid bind mounts for application data in production. They make your container non-portable because they depend on a specific host directory structure. More critically, they are a security risk; if a process in the container is compromised, it has direct access to the mounted host path. File permission issues (UID/GID mismatches) between the host and container are also a common source of errors.

ONE CANONICAL EXAMPLE: To develop a Node.js application, you can run your container with a bind mount for your source code. The command docker run -p 3000:3000 -v ./src:/app/src my-node-app maps your current src directory into the container's /app/src. When you edit a file in ./src on your host, a process like nodemon inside the container detects the change and restarts the server automatically.

Read the original → docs.docker.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.