Docker Build Context Isolation and Bind-Mount Masking

Docker

When a containerized project shares its source tree with a developer's host machine (for example, macOS or Windows running a Linux container), host dependency directories (.venv/, node_modules/) and generated artifacts (staticfiles/, mediafiles/) can cross the host-container boundary in two places: at build time via COPY . /app, and at runtime in development via bind mounts (./django-app:/app).

Build Time (docker build)                    Runtime in Dev (docker compose up)
┌─────────────────────────┐                  ┌──────────────────────────────────────┐
│ Host Workspace (macOS)  │                  │ Host Bind Mount: ./django-app:/app   │
│  ├─ .venv/ (Mach-O)     │                  │  (Would overwrite /app/node_modules) │
│  ├─ node_modules/       │                  └──────────────────┬───────────────────┘
│  └─ mediafiles/         │                                     │
└────────────┬────────────┘                                     ▼
             │ Blocked by .dockerignore      ┌──────────────────────────────────────┐
             ▼                               │ Anonymous Volume: - /app/node_modules│
┌─────────────────────────┐                  │  (Masks bind mount; keeps Linux      │
│ Clean Linux Build Ctx   │                  │   binaries built inside the image)   │
└─────────────────────────┘                  └──────────────────────────────────────┘

1. Build-Time Isolation with .dockerignore

Before running Dockerfile instructions, the Docker client packages the build context directory and sends it to the daemon. If .dockerignore does not exclude local environments and runtime output:

  1. Cross-OS binary pollution: COPY . /app copies host-compiled Python C-extensions (.venv/) or platform-specific Node binaries (node_modules/, such as Darwin Mach-O binaries from macOS) into the Linux image, overwriting or conflicting with packages installed during docker build.
  2. Artifact and data leakage: Local test uploads in mediafiles/ or stale assets in staticfiles/ get baked into production images.
  3. Slow context transfers: Sending tens of thousands of files inside .venv/ and node_modules/ balloons build context size and invalidates Docker layer caches whenever a local file changes.

A hardened .dockerignore for a Django + Node project explicitly excludes:

.venv/
node_modules/
staticfiles/
mediafiles/

2. Runtime Protection Against Bind-Mount Shadowing in Compose

In local development, mounting the application directory from the host into the container (- ./django-app:/app) enables live code reloading without rebuilding the image. However, a bind mount replaces the entire /app directory tree inside the container with the host's directory tree:

  • Any /app/node_modules directory installed during docker build is shadowed (hidden) by ./django-app/node_modules on the host.
  • If the host has no node_modules (or has macOS binaries installed on the host), in-container background processes such as npx tailwindcss --watch fail immediately.

To preserve the Linux node_modules built into the image while still bind-mounting the rest of /app, add an anonymous volume for the subdirectory in docker-compose.yml:

services:
  web:
    volumes:
      - ./django-app:/app
      - /app/node_modules

Docker mounts volumes in specificity order: it mounts ./django-app at /app first, and then mounts the anonymous container volume over /app/node_modules. When the anonymous volume is first initialized, Docker copies the contents of /app/node_modules from the underlying image into the volume, shielding the Linux dependencies from the host bind mount.

References

Review cards

Flashcard What goes wrong if `.dockerignore` doesn't exclude `.venv/` and `node_modules/`?

COPY . /app brings host-built binaries (macOS Mach-O, for example) into the Linux image, where they clash with the packages installed during the build. The context also gets huge and layer caches break on every local change.

Flashcard How does `- /app/node_modules` keep the image's dependencies under a `./django-app:/app` bind mount?

The anonymous volume is mounted on the more specific path, on top of the bind mount, and Docker fills it from the image the first time, so the Linux packages stay visible.