Mounting source within nested container
Hi,
There appears to be a docker limitation on mounting a volume inside a container that was mounted from outside. I'll explain with an example:
Running with
image: docker:git
build:
stage: build
script:
- docker run -v "$CI_PROJECT_CIR:/mysrcdir" -t mycontainer
Now I initially expected this to mount my source inside the nested container, but that directory is empty. As it turns out, this is a "known" docker pitfall, because mounting of volumes should be done relative to the docker daemon, which is not running inside the docker container but on the system itself. So the correct way to do what I wanted to do is:
image: docker:git
build:
stage: build
script:
- docker run -v "<dir that was volume mounted to $CI_PROJECT_CIR>:/mysrcdir" -t mycontainer
Now, I'm not sure if that directory is known inside the container? Is there a workaround for this? My usecase is that I want to build and test my source within my own container, running inside the docker:git container.
See https://github.com/docker/docker/issues/21510 for more information on this pitfall.
EDIT: I found a workaround for this was to call the clone command inside the container... though of course that feels completely ridiculous. I'd love to hear from someone more experienced what the best way to do this is.
image: docker:git
build:
stage: build
script:
- docker run -t mycontainer /bin/sh -c "mkdir -p /src/dir && git clone ${CI_BUILD_REPO:0:8}gitlab-ci-token:$CI_BUILD_TOKEN@${CI_BUILD_REPO:8} /src/dir && cd /src/dir && git checkout $CI_BUILD_REF"
EDIT 2: More dicussion on this subject with another potential workaround: https://www.develves.net/blogs/asd/2016-05-27-alternative-to-docker-in-docker/
EDIT 3: I found I was able to use the cloned directory by using --volumes-from:
image: docker:git
build:
stage: build
script:
- docker run --volumes-from $(docker ps -aq -f label="com.gitlab.gitlab-runner.cache.dir") --rm -t mycontainer /bin/bash -c "ls $CI_PROJECT_DIR"
Bask in my hacks!