🐳 Docker: Fix a Volume Mount Hiding Files Baked Into the Image
A bind mount or named volume mounted over a path the image already populated during its build doesn’t merge with what the image put there – it overlays the mount’s own content on top, completely replacing the container’s view of that directory regardless of what the Dockerfile’s RUN steps spent minutes building into the image. If the host side (or the volume) is empty at that path, the container doesn’t fall back to the image’s version of the directory – it just sees an empty directory, and whatever dependencies or generated files the build produced there are invisible at runtime, even though the image itself is completely intact.
🔎 The Problem
# Dockerfile: RUN npm install (populates /app/node_modules in the image)
# docker-compose.yml:
services:
web:
build: .
volumes:
- ./app:/app # bind mount for live code editing
# host ./app/node_modules is empty or doesn't exist locally ->
# it now overlays and HIDES the image's /app/node_modules entirely
command: node server.js
# Container error: Cannot find module 'express'
# -- even though `docker build` completed npm install successfully
✅ The Fix
- Declare the subdirectory that must keep the image’s build output as its own separate volume (for example a named volume mounted specifically at /app/node_modules) so that nested path keeps the image’s content instead of being blanked out by the broader bind mount covering /app.
- In Compose, verify the EFFECTIVE mount list with docker compose config (or docker inspect on the running container) rather than assuming the compose file’s intent is what actually took effect – a more general mount can silently take priority over what you expected a nested one to protect.
- When a ‘missing dependency’ error only happens under the dev compose setup and not in a plain docker build or in CI, check the container’s actual mounts first – the image is very likely fine, and the mount is the only thing hiding its content.
🚨 Why This Is Dangerous
Because the exact same image runs perfectly in CI or production (where no development bind mount exists to hide anything), this looks exactly like a ‘works everywhere except my machine’ environment difference, sending time toward comparing environments when the real cause is a single mount path overlapping a directory the image already built.
A volume mount doesn’t ask what’s already there before it takes over – it replaces it, even when what’s already there took your build five minutes to produce.





