Docker in Docker cannot mount volume

前端 未结 10 1831
情话喂你
情话喂你 2020-11-29 21:09

I am running a Jenkins cluster where in the Master and Slave, both are running as a Docker containers.

The Host is latest boot2docker VM running on MacOS.

T

10条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-29 21:28

    Based from the description mentioned by @ZephyrPLUSPLUS here is how I managed to solve this:

    vagrant@vagrant:~$ hostname
    vagrant
    vagrant@vagrant:~$ ls -l /home/vagrant/dir-new/
    total 4
    -rw-rw-r-- 1 vagrant vagrant 10 Jun 19 11:24 file-new
    vagrant@vagrant:~$ cat /home/vagrant/dir-new/file-new 
    something
    vagrant@vagrant:~$ docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock  docker /bin/sh
    / # hostname
    3947b1f93e61
    / # ls -l /home/vagrant/dir-new/
    ls: /home/vagrant/dir-new/: No such file or directory
    / # docker run -it --rm -v /home/vagrant/dir-new:/magic ubuntu /bin/bash
    root@3644bfdac636:/# ls -l /magic
    total 4
    -rw-rw-r-- 1 1000 1000 10 Jun 19 11:24 file-new
    root@3644bfdac636:/# cat /magic/file-new 
    something
    root@3644bfdac636:/# exit
    / # hostname
    3947b1f93e61
    / # vagrant@vagrant:~$ hostname
    vagrant
    vagrant@vagrant:~$ 
    

    So docker is installed on a Vagrant machine. Lets call it vagrant. The directory you want to mount is in /home/vagrant/dir-new in vagrant. It starts a container, with host 3947b1f93e61. Notice that /home/vagrant/dir-new/ is not mounted for 3947b1f93e61. Next we use the exact location from vagrant, which is /home/vagrant/dir-new as the source of the mount and specify any mount target we want, in this case it is /magic. Also note that /home/vagrant/dir-new does not exist in 3947b1f93e61. This starts another container, 3644bfdac636. Now the contents from /home/vagrant/dir-new in vagrant can be accessed from 3644bfdac636.

    I think because docker-in-docker is not a child, but a sibling. and the path you specify must be the parent path and not the sibling's path. So any mount would still refer to the path from vagrant, no matter how deep you do docker-in-docker.

提交回复
热议问题