GitLab CI docker in docker can't create volume

最后都变了- 提交于 2019-12-04 05:20:38

Background

Your problem is in the fact that DIND runs ALL containers on your host (or top-level Docker engine) so when you mount a directory to your $CONTAINER_TEST_IMAGE (2nd level Docker) this image in fact runs on your host through the mounted socket and thus the container is looking for that directory on your Docker host.

I've had this same issue mounting tests in containers and solved it through linking volumes between containers.

Solution

In your case I think the docker cp command could solve your need to copy the /key/application_default_credentials.json file to the container.

Something like:

- docker run --name="myContainer" -d $CONTAINER_TEST_IMAGE
- docker cp /key/application_default_credentials.json myContainer::/.config/gcloud/application_default_credentials.json
- docker exec -it myContainer 'run_tests_or_whatever_command'
- docker rm -f myContainer

The other solution given is perfectly valid but I wanted to share my solution:

Apparently dind will mount the /build directory so subcontainers can "see" its contents. So by placing the key in "./" it is viewable by those containers. I use $(pwd) because docker run doesn't accept ~ or .

test run:
    stage: deploy
    script:
        - *docker_login
        - mkdir ./key
        - echo $GCP_SVC_KEY > ./key/application_default_credentials.json
        - docker run --rm -v "$(pwd)/key:/.config/gcloud/" $CONTAINER_TEST_IMAGE
    tags:
        - docker
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!