Docker Bind Mount Hides Image Files: Compare It with a Named Volume
Contents
If a file exists in your Docker image but disappears when you mount a host directory over it, check the mount destination first. A bind mount at /demo makes that path show the host directory. The same image run without the mount still shows its packaged files. A fresh named volume can look like a fix because Docker copies the packaged files into it by default. That copy can become stale after an image upgrade.
Compare the same path with and without a mount
This Bash fixture uses Docker Engine and a daemon that can read the temporary host directory. Run it from a writable, Docker-shared directory or set DOCKER_DEMO_ROOT to one. If Docker reports that the bind source does not exist, check whether the daemon can see that path. It builds two versions of a tiny image, runs both with the same volume, then checks a narrower bind mount. The cleanup trap removes the temporary directory, volume, and image tags created by the fixture.
( set -eu work=$(mktemp -d "${DOCKER_DEMO_ROOT:-$PWD}/mount-demo.XXXXXXXX") work=$(cd "$work" && pwd -P) suffix=$(basename "$work" | tr '[:upper:]' '[:lower:]') image="mount-demo-$suffix" volume=$(docker volume create "$image-data") trap 'docker volume rm "$volume" >/dev/null docker image rm "$image:v1" "$image:v2" >/dev/null 2>&1 || true rm -rf -- "$work"' EXIT
mkdir "$work/host" cat > "$work/Dockerfile" <<'DOCKERFILE'FROM node:26-alpineARG RELEASERUN mkdir -p /demo/data && printf '%s\n' "$RELEASE" > /demo/release.txtDOCKERFILE
for release in v1 v2; do docker build --quiet --build-arg RELEASE="$release" \ --tag "$image:$release" "$work" >/dev/null done
check='if [ -f /demo/release.txt ]; then cat /demo/release.txt; else echo missing; fi'
printf 'v1 without mount: ' docker run --rm "$image:v1" sh -c "$check"
printf 'v1 with host directory at /demo: ' docker run --rm --mount "type=bind,src=$work/host,dst=/demo,readonly" \ "$image:v1" sh -c "$check"
printf 'v1 with fresh named volume at /demo: ' docker run --rm --mount "type=volume,src=$volume,dst=/demo" \ "$image:v1" sh -c "$check"
printf 'v2 with the same named volume: ' docker run --rm --mount "type=volume,src=$volume,dst=/demo" \ "$image:v2" sh -c "$check"
printf 'v2 with host directory at /demo/data: ' docker run --rm --mount "type=bind,src=$work/host,dst=/demo/data,readonly" \ "$image:v2" sh -c "$check")Docker Engine 28.4.0 on Linux arm64 under Colima 0.10.1, accessed from macOS, produced these results on November 25, 2026.
v1 without mount: v1v1 with host directory at /demo: missingv1 with fresh named volume at /demo: v1v2 with the same named volume: v1v2 with host directory at /demo/data: v2The third and fourth lines explain why swapping a bind mount for a named volume can appear to work on the first run. Docker copies the files into an empty volume by default. Once that volume contains the copied files, it still supplies them when a container starts from the newer image. Docker documents both the copy behavior and the volume-nocopy option, which disables the initial copy. Docker volume documentation
Check the mount on your container
Replace my-container with the affected container name or ID.
docker inspect --format '{{range .Mounts}}{{println .Type .Source "->" .Destination}}{{end}}' my-containerLook for a destination equal to the missing path or one of its parent directories. Compare it with a clean container started from the same image without that mount. If the file is also absent in the clean container, check the Docker build context and COPY paths before changing the runtime mounts.
When only a data directory needs to live on the host, mount that directory alone and leave the bundled files outside the mount. If an existing named volume owns the path, inspect its contents before planning a migration. Rebuilding the image leaves the reused volume’s files in place.
References: