Docker in Docker cannot mount volume

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

    Lots of good info in these posts but I find none of them are very clear about which container they are referring to. So let's label the 3 environments:

    • host: H
    • docker container running on H: D
    • docker container running in D: D2

    We all know how to mount a folder from H into D: start D with

    docker run ... -v : -v /var/run/docker.sock:/var/run/docker.sock ...
    

    The challenge is: you want path-on-H to be available in D2 as path-on-D2.

    But we all got bitten when trying to mount the same path-on-H into D2, because we started D2 with

    docker run ... -v : ...
    

    When you share the docker socket on H with D, then running docker commands in D is essentially running them on H. Indeed if you start D2 like this, all works (quite unexpectedly at first, but makes sense when you think about it):

    docker run ... -v : ...
    

    The next tricky bit is that for many of us, path-on-H will change depending on who runs it. There are many ways to pass data into D so it knows what to use for path-on-H, but probably the easiest is an environment variable. To make the purpose of such var clearer, I start its name with DIND_. Then from H start D like this:

    docker run ... -v : --env DIND_USER_HOME=$HOME \
        --env DIND_SOMETHING=blabla -v /var/run/docker.sock:/var/run/docker.sock ...
    

    and from D start D2 like this:

    docker run ... -v $DIND_USER_HOME: ...
    

提交回复
热议问题