Docker in Docker cannot mount volume

前端 未结 10 1811
情话喂你
情话喂你 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:27

    I have same problem in Gitlab CI, I solved this by using docker cp to do something like mount

    script:
      - docker run --name ${CONTAINER_NAME} ${API_TEST_IMAGE_NAME}
    after_script:
      - docker cp ${CONTAINER_NAME}:/code/newman ./
      - docker rm ${CONTAINER_NAME}
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-29 21:34

    You can solve this passing in an environment variable. Example:

    .
    ├── docker-compose.yml
    └── my-volume-dir
        └── test.txt
    

    In docker-compose.yml

    version: "3.3"
    services:
      test:
        image: "ubuntu:20.04"
        volumes:
          - ${REPO_ROOT-.}/my-volume-dir:/my-volume
        entrypoint: ls /my-volume
    

    To test run

    docker run -e REPO_ROOT=${PWD} \
       -v /var/run/docker.sock:/var/run/docker.sock \
       -v ${PWD}:/my-repo \
       -w /my-repo \
       docker/compose \
       docker-compose up test
    

    You should see in the output:

    test_1  | test.txt
    
    0 讨论(0)
  • 2020-11-29 21:35

    Regarding your use case related to Jenkins, you can simply fake the path by creating a symlink on the host:

    ln -s $HOST_JENKINS_DATA_DIRECTORY/jenkins_data /var/jenkins_home
    
    0 讨论(0)
提交回复
热议问题