Skip to content

Docker node_modules: inspect mounts, then refresh

Treat initial visibility and second-revision refresh as separate acceptance tests, without broad data resets.

Dopstack Engineering

· 9 min read

Original conceptual illustration: Overlapping translucent filesystem sheets reveal a dependency aperture and retained old revision; plum and mint.

Quick answer

If `node_modules` exists in a Docker image but disappears or becomes stale under Compose, inspect the mounts at `/app` and `/app/node_modules`. A bind mount can obscure image files; a non-empty dependency volume can obscure freshly rebuilt dependencies. Choose who owns dependencies, define how a lockfile change refreshes them, and verify the running filesystem separately from the image.

If node_modules exists in a Docker image but disappears or becomes stale under Compose, inspect the mounts at /app and /app/node_modules. A bind mount can obscure image files; a non-empty dependency volume can obscure freshly rebuilt dependencies. Choose who owns dependencies, define how a lockfile change refreshes them, and verify the running filesystem separately from the image. [1][2][3]

Which filesystem is Node actually reading?#

Scenario: a fictional Node.js service builds successfully and runs from its image. In local Compose, the same image reports that a package is missing. The developer rebuilds, but the error remains. The Compose service mounts the repository at /app, so the running container sees a different directory tree from the image's original /app.

The build established that installation succeeded inside an image layer. The runtime mount answered which files are visible at a path in this container. Those are separate observations. Rebuilding changes the image; it does not remove every mount that can cover the new files. Docker explicitly documents this obscuring behavior for bind mounts and non-empty volumes. [1][2]

Our case concerns a development service with live source changes. Production should have its own reviewed image and storage contract. Avoid copying a local-development mount pattern into a release manifest simply because it makes hot reload convenient.

Start by recording the image ID, the service's resolved Compose configuration, the container's mounts and the exact package-resolution error. A moving image tag is weaker evidence than an image ID. An error for one package may also involve the working directory or package export rules; do not assume every module error is a missing directory.

Compare the image with the mounted container#

Inspect the resolved Compose file and the running container. The first exposes overrides and interpolated configuration; the second shows the mounts Docker actually created. Keep the outputs private if your configuration contains sensitive environment values. The commands below are diagnostic sketches; replace service and image identifiers.

bash
docker compose config
docker compose ps -q app
docker inspect CONTAINER_ID --format '{{json .Mounts}}'
docker inspect CONTAINER_ID --format '{{.Image}}'

# Inspect the image without the development service's mounts.
docker run --rm --entrypoint sh IMAGE_ID \
  -c 'test -d /app/node_modules && ls /app/node_modules'

# Compare with the filesystem in the running service.
docker compose exec app sh \
  -c 'pwd; test -d /app/node_modules && ls /app/node_modules'

An image with dependencies and a mounted service without them points to runtime visibility. Inspect every mount whose destination is /app, /app/node_modules or an ancestor of the relevant package path. If both views lack the package, return to the image build, installation mode and lockfile rather than adding more volumes.

The bind source lives on the Docker daemon host. Docker Desktop provides host-sharing integration, while a remote daemon has a different filesystem context. Verify which Docker context the command uses before interpreting a source path as a directory on your current laptop. [1]

Use a tiny marker fixture when package resolution obscures the question. Put one named file into the image directory and a different named file into a host fixture. Mount the host fixture over the image directory, then inspect which marker is visible. That isolates mount behavior from Node, network access and dependency installation.

Does a nested dependency volume solve the problem?#

A common development arrangement binds the repository at /app and mounts a named volume at /app/node_modules. The more specific dependency mount gives that path its own storage. It can isolate Linux-installed packages from a host checkout. It also creates a persistent dependency store with a lifecycle distinct from the image. [1][2]

yaml
services:
  app:
    build: .
    working_dir: /app
    volumes:
      - type: bind
        source: .
        target: /app
      - type: volume
        source: app-deps
        target: /app/node_modules
volumes:
  app-deps:

Docker normally copies pre-existing container files into an empty volume mounted at that path. A non-empty volume instead presents its existing contents. Once the dependency volume has been populated, rebuilding an image with a different lockfile does not turn that volume into a synchronized copy of the new image. This is the second half of the failure, and the reason an apparently successful fix can become tomorrow's stale-package incident. [2]

Assign an owner and a refresh rule to the dependency volume. Decide whether dependency installation runs explicitly in the development container, whether a verified lockfile identity triggers a refresh, or whether the team avoids the persistent dependency volume entirely. A volume name by itself is not a dependency version.

Compose's volume.nocopy option changes initial population behavior. It does not supply a package manager or a refresh protocol. If you enable it, an empty dependency volume stays empty until your own installation step fills it. Review initialization and refresh as separate operations. [4]

How should lockfile changes update the volume?#

For this fictional team, choose explicit installation after dependency changes. Stop concurrent writers and the development server before changing the shared dependency tree. Run the package manager in the container environment that will execute the application, then restart and verify the required package. Preserve the lockfile and installation output in the development record.

bash
# Development-only: ensure no other process is using this dependency volume.
docker compose stop app
docker compose run --rm --no-deps app npm ci
docker compose up -d app

This sketch assumes the service image can run npm, the service mount contains the intended manifests, its user can write the dependency volume, and no custom entrypoint changes the command's meaning. Check those assumptions in the resolved service configuration before running it. If installation fails, keep the service stopped until the dependency state is understood.

npm ci requires an existing lockfile, rejects a mismatch with package.json, and removes an existing node_modules before installation. That removal makes concurrent use particularly risky. Do not present an in-place install as an atomic swap for a running service. A failure partway through installation can leave an unusable dependency tree. [5]

If two services share the volume, stopping only one is insufficient. Inventory every writer and reader. For most local projects, a distinct dependency store per application and runtime platform is easier to reason about than one shared store with undocumented consumers. Package caches and installed dependency trees have different purposes; sharing one does not justify sharing the other.

A more automated system can record the lockfile hash, runtime version and platform alongside a successful install, then refuse to start with a mismatched identity. That is an application workflow you must implement and test. Docker does not infer the rule from those files, and a lockfile hash alone does not capture every native-build input.

Would narrower source mounts be easier?#

Often they are. If the application supports it, mount only the source paths that need live editing, leaving /app/node_modules owned by the image. A lockfile change then requires rebuilding and replacing the development container. The running dependency tree follows the image lifecycle, while source edits remain convenient.

Development arrangementDependency ownerRequired refresh rule
Whole repository bind onlyHost tree visible at /appInstall for the actual runtime platform or avoid this arrangement
Repository bind plus dependency volumePersistent container-side volumeExplicit serialized install or a tested identity-based refresh
Narrow source bindImage owns dependenciesRebuild and recreate when dependency inputs change
Compose Watch with rebuild ruleImage plus declared synchronization rulesRebuild on manifests; exclude host dependency artifacts
Choose one ownership model deliberately. These are workflow trade-offs based on documented mount and Watch behavior, not performance measurements. [1][2][3]

Narrow mounts cost configuration. Generated files, templates or runtime-loaded assets may live outside the mounted directories. List those inputs before choosing the pattern, and test a representative edit from each category. A developer experience that refreshes .js files but silently ignores templates is incomplete even if dependency ownership is clean.

Compose Watch is another option. It supports synchronization rules for source and rebuild rules for inputs that require a new image. Docker's guidance specifically discourages syncing node_modules across platforms because packages can contain native code. Verify the installed Compose version and the prerequisites for writing into the target path. [3]

Avoid combining a rebuild rule with an old dependency volume without thinking through the interaction. Replacing the container may give it a new image while reattaching the same non-empty volume. The very mount that made the original setup convenient can hide the dependencies produced by Watch's rebuild.

Why is deleting every volume the wrong diagnostic?#

A broad reset can erase a development database along with the stale dependency store. Inspect the mount first. The useful question is which exact mount supplies the incorrect dependency path. Answer it before deciding whether any disposable dependency data should be recreated.

Compose distinguishes named, anonymous and external volumes. Its down command does not remove every volume by default, while its volume-removal option can remove declared named volumes and attached anonymous volumes. External volumes have a separate lifecycle. Read the resolved project configuration and identify database or object-store volumes before applying any cleanup procedure. [6]

Prefer a refresh operation scoped to the dependency store, with the application stopped and its volume identity known. If the team chooses replacement instead of installation, preserve any needed state, verify the volume is disposable and record the exact intended target. Do not turn a tutorial's reset command into a routine repair for a shared project.

Also distinguish file visibility from permissions. A dependency directory may be visible but unreadable by the container's configured user, or writable only by the user that performed the first install. Inspect identity and permissions before installing as root. Fix the ownership contract rather than leaving a development server dependent on a privileged repair shell.

How do you verify the repair stays repaired?#

Run a sequence that includes both first initialization and a second dependency revision. First, start from the chosen clean development state and verify the package resolves. Then change a disposable fixture dependency or marker, rebuild or refresh according to the documented rule, and verify the running service sees the second revision.

For the volume arrangement, include a negative case: rebuild the image while preserving an already populated dependency volume. The fixture should show that the old volume content remains visible. Then perform the intended refresh and require the new content. This tests the failure the team actually encountered instead of merely proving that a container can start once.

Keep source hot reload as a separate acceptance criterion. Edit a source file without changing dependencies and confirm the intended reload. Next, change a manifest and verify that the workflow chooses the stronger rebuild or installation path. Finally, restart the project and inspect the same dependency identity again. Persistence is useful only when the persisted content is the intended content.

Record the Docker Engine, Compose and Node versions, platform, image identity, volume name and the refresh operation actually tested. An isolated marker test proves filesystem visibility and volume lifecycle. It does not prove npm registry access, native-module compatibility or every framework's watcher behavior.

Example verification: an isolated marker fixture on Docker Engine 29.1.3 showed image files hidden by a bind mount, initial empty-volume population, old volume content after an image rebuild and explicit scoped refresh. The fixture used an existing Alpine image without network access. It did not install npm packages or test native modules and framework reload.

Frequently asked questions#

Did the bind mount delete dependencies from the image?
It obscures files at the mounted destination in that container. Inspect a new container without the development mount to compare the image's original contents. [1]
Why does a rebuilt image still use old packages?
A non-empty volume mounted at node_modules presents its existing contents over the image directory. Rebuild and dependency-volume refresh are separate lifecycle operations. [2] Verify the current volume contents explicitly.
Should I sync host node_modules into a Linux container?
Check platform compatibility first. Packages can include native code, and Docker's Watch guidance recommends excluding node_modules from cross-platform synchronization. Install in the intended runtime environment. [3]
Can npm ci run while the app uses the volume?
Avoid that arrangement. npm ci removes the existing dependency directory before installing, so concurrent readers or installers can observe an incomplete tree. Stop consumers and serialize refreshes. [5]

Technologies

  • Docker
  • Node.js

Sources

  1. [1]
    Bind mounts, Docker

    Read 2026-11-06T08:03:32.376659+00:00; publication/update date not asserted; dated event specified in article where relevant. Supports: Bind sources refer to the daemon host.

  2. [2]
    Volumes, Docker

    Read 2026-11-06T08:03:32.382165+00:00; publication/update date not asserted; dated event specified in article where relevant. Supports: A volume outlives an individual container.

  3. [3]
    Compose Watch, Docker

    Read 2026-11-06T08:03:32.484567+00:00; publication/update date not asserted; dated event specified in article where relevant. Supports: Native packages may not be portable across platforms.

  4. [4]
    Compose services: volumes, Docker

    Read 2026-11-06T08:03:32.528750+00:00; publication/update date not asserted; dated event specified in article where relevant. Supports: Compose can disable implicit bind-source directory creation.

  5. [5]
    npm ci, npm

    Read 2026-11-06T08:03:32.560340+00:00; publication/update date not asserted; dated event specified in article where relevant. Supports: npm ci removes an existing dependency directory.

  6. [6]
    docker compose down, Docker

    Read 2026-11-06T08:03:32.583037+00:00; publication/update date not asserted; dated event specified in article where relevant. Supports: External volumes have a separate removal lifecycle.

Written by

Dopstack Engineering

Engineering team

The engineering team at Dopstack. Illustrative scenarios are composites written to explain a pattern, not accounts of client work.

All articles Website

Get new articles by email

Engineering notes on AI, cloud and SaaS. Occasional, never sold or shared.

Send me new Dopstack Insights articles by email. I can unsubscribe at any time. We’ll email you once to confirm. Privacy

Continue reading