Card 04/ 05
Mounting Over a Directory Hides the Image's Files
Why dependencies installed at build time vanish when you mount your source over the app folder.
A mount, a directory from your machine or a volume attached at a path inside a container, replaces whatever the image had at that path for as long as the container runs. Mount a host directory over a directory the image filled, and the container sees only your host directory: the image's files at that path are hidden, not merged with yours.
The image installed your application's dependencies into /app when it was built. To see your edits without rebuilding, you mount your source directory over /app, and the container starts up insisting nothing is installed:
docker run -v $(pwd):/app my-api
Error: Cannot find module 'express'Nothing was deleted. The container now sees your host directory at /app, and your host directory has no node_modules folder (where a Node.js project keeps its installed packages), because the install ran inside the image and not on your machine.
| Without the mount | With the mount |
|---|---|
The image's /app, dependencies included | Your host directory, exactly as it is |
The usual fix is a second mount: a named volume placed at /app/node_modules. The more specific mount wins at that path, so your host directory covers the source code and the volume covers the dependencies. The first time an empty named volume is mounted, Docker fills it with what the image had at that path, so the installed packages appear in it.
So when files the image should contain are missing inside a container, check the mounts before you suspect the image.
← → move through sections and cards