Search code examples
dockerdocker-volumedocker-in-docker

$(pwd) at docker-in-docker volume mounts unexpected directory


Example:

pavel@Z546:~/test/dind-volumes$ ls
test.txt
pavel@Z546:~/test/dind-volumes$ docker run -v /var/run/docker.sock:/var/run/docker.sock -v $(pwd):/app -w /app -it docker sh
/app # ls
test.txt
/app # pwd
/app
/app # docker run -v $(pwd):/app2 -w /app2 -it alpine sh
/app2 # ls
share       tomita-cfg
/app2 # pwd
/app2

Which location -v $(pwd):/app2 mounts? It's not ~/test/dind-volumes on host OS. share and tomita-cfg are folders from host OS, but path is exactly different, it's even not started from ~


Solution

  • The directory that gets mounted into the new container is whatever is in the /app directory on the host.

    This is not Docker in Docker as I understand the term to mean: you are not running a second copy of Docker inside a Docker container, you are merely making access to the host's Docker socket available inside a container.

    In the scenario you show, when you are inside a container with the host's Docker socket mounted and run docker run -v $(pwd):/app2 ..., first the shell inside the container expands $(pwd) to /app, and then it sends a request to the Docker daemon to launch a container with /app bind-mounted to /app2. From the Docken daemon's point of view, this is indistinguishable from running the same command on the host directly.