docker / for-linux

Docker Engine for Linux
https://docs.docker.com/engine/installation/
756 stars 85 forks source link

bind mounts do not work for docker running inside a docker container #1416

Closed rhaschke closed 2 years ago

rhaschke commented 2 years ago

I am trying to run another docker command within an already running docker container. To this end, of course, I forwarded the socket via option -v /var/run/docker.sock:/var/run/docker.sock. This works fine. However, trying to bind-mount volumes within the inner docker container (referring to folders in the outer container) fails: the mounted volumes refer to the host's volume, not the ones of the external docker container as expected.

Steps to reproduce the behavior

start outer docker container: docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock ubuntu:focal

start inner docker container:

apt-get -q update
apt-get -q install -y docker.io
touch /tmp/file # create a dummy file in /tmp
docker run --rm -it -v /tmp:/tmp ubuntu:bionic
ls /tmp # Shows content of host's /tmp folder, not the one of the external docker image including the generated file

Output of docker version:

Client:
 Version:           20.10.12
 API version:       1.41
 Go version:        go1.16.2
 Git commit:        20.10.12-0ubuntu2~20.04.1
 Built:             Wed Apr  6 02:14:38 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server:
 Engine:
  Version:          20.10.12
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.2
  Git commit:       20.10.12-0ubuntu2~20.04.1
  Built:            Thu Feb 10 15:03:35 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.5.9-0ubuntu1~20.04.4
  GitCommit:        
 runc:
  Version:          1.1.0-0ubuntu1~20.04.1
  GitCommit:        
 docker-init:
  Version:          0.19.0
  GitCommit:        
nicks commented 2 years ago

Hmmm... When you mount the Docker socket inside the container, you're not running Docker-in-Docker. You're talking to the Docker daemon on the host machine. So this is working as expected.

Running Docker-in-Docker is more tricky. Try something like: https://github.com/nestybox/sysbox/blob/master/docs/user-guide/dind.md

rhaschke commented 2 years ago

Thanks for pointing out the difference between DinD and DooD. I will resort to sysbox then.