Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

A container started from inside a container sees an empty or missing mount

Errorthe mounted folder is empty inside the new container

Short answer

A bind mount is resolved by the Docker daemon, on the daemon's own host, not inside the container that typed the command. From a CI job with the host socket, the path you give must exist on the host. With -v, a missing path is created as an empty directory, which looks like an empty mount.

  • Applies to Docker Engine 28, docker image 28 (CLI and dind)
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find out which daemon is receiving your commands and whether the path you mount exists there. Run this inside the job or container that starts the other containers.

    ls -l /var/run/docker.sock
    docker info
    docker run --rm -v "$PWD":/work alpine ls -la /work
  2. 2Match what you see, then jump to its fix

    1. 1ls -la /work in the second container is empty, or the path is not what you expectThe path exists in your container but not on the daemon hostFix
    2. 2No error, but the mount point is an empty directory owned by root-v silently created an empty directory for a missing sourceFix
    3. 3The job talks to a docker:dind service over tcp://...:2376, and host paths are not visible to itA nested daemon (dind) has its own filesystemFix

What does the error look like?

  • the mounted folder is empty inside the new container
  • the files you mounted are not there in the second container

Cause 1 of 3

The path exists in your container but not on the daemon host

1Check

The docker CLI only sends your request. Bind mounts are created on the Docker daemon host, not on the client. When the CLI container shares the host's /var/run/docker.sock, the daemon is the host's, so $PWD (a path that only exists inside the job container, such as a CI workspace) points at nothing on the host.

docker run --rm -v "$PWD":/work alpine ls -la /work
docker info

2Fix

Give the daemon a path that really exists on the host. Either mount the same host folder into your job container at the same absolute path, so the two views match, or move the data into a named volume and use that volume in both containers.

# job container sees the host folder at the SAME path
docker run -v /srv/ci/work:/srv/ci/work -v /var/run/docker.sock:/var/run/docker.sock docker sh

# second container: use a named volume instead of a path
docker volume create build_data
docker run --rm -v build_data:/work alpine ls -la /work

3Verify

The folder inside the second container now lists the files you expect.

docker run --rm -v /srv/ci/work:/work alpine ls -la /work

Confirmed by docs.docker.com

Did this fix it?

Cause 2 of 3

-v silently created an empty directory for a missing source

1Check

With -v, Docker creates the source as a directory when it does not exist on the host. With --mount it does not: it returns an error unless bind-create-src is set. So a wrong path with -v fails quietly and with --mount fails loudly.

ls -ld /srv/ci/work

2Fix

Use --mount while debugging so a wrong path stops the run, and fix the path. Remove the stray empty directory so it does not hide the mistake later.

docker run --rm --mount type=bind,source=/srv/ci/work,target=/work alpine ls -la /work

3Verify

With the right source the command lists your files, and with a wrong one it now prints an error instead of an empty folder.

docker run --rm --mount type=bind,source=/srv/ci/typo,target=/work alpine ls /work

Confirmed by docs.docker.com

Did this fix it?

Cause 3 of 3

A nested daemon (dind) has its own filesystem

1Check

With Docker-in-Docker, the daemon runs inside its own container, so bind sources are resolved there. The official image shares files with the client container through a named volume: its TLS example mounts the same volume into both. It also says --privileged is required for dind.

docker ps

2Fix

Share what the nested daemon must read through a named volume mounted into the client and the dind container, as the image's own certificate setup does. If you only need the host's daemon, mount the host socket and use the host-path fix above instead of running dind.

docker volume create shared_work
docker run --privileged --name dind -d \
  -v shared_work:/work docker:dind

3Verify

The client container and the dind daemon list the same files in /work.

docker exec dind ls -la /work

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Decide per pipeline: host socket (paths are host paths) or dind (paths belong to the nested daemon). Do not mix the two.
  • Prefer named volumes over bind mounts for files that two containers must share.
  • Use --mount in scripts so a wrong source path stops the run.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.