How do I mount/copy git repository to a docker container using gitlab ci docker executor DniD?

I’m using DinD, and I want to mount/copy the git repo of the testing branch to the docker container being built.

I could git clone it again in the container but seems unnecessary and time waste if I can just mount/copy it from where the runner already did it.

I tried to mount it by putting the following code in both the gitlab-ci.yml and docker-compose.yaml files but it’s not working since it’s not showing up in the container.

  volumes:
    # Mount the repo
    - .:/opt/repo:rw

Any help would be appreciated.

Thanks!

If you are using DinD with Docker executor using services: keyword, the DinD container where the actual build happens (because Docker has client-server architecture) does not have the GIT repo mounted. The GIT repo is mounted only in the “build” container.
Even if it was, Docker does not support mounting anything during build. All files required for the build must be in the context directory and copied/added in a Dockerfile.

What would the difference if I use the Docker executor without using the DinD service keyword?

From this:

build:
  stage: build
  image: docker:latest
  services:
    - docker:dind
  variables:
    DOCKER_TLS_CERTDIR: ""

to this:

build:
  stage: build
  image: docker:latest

So, it’s either to git clone again the repo in the Dockerfile or copy the files from the build container to the custom image? or something like that?

I’m a bit confused with the terms DinD container, build container and context directory, Would you mind explaining them?

build:
  stage: build
  image: docker:latest

won’t work, because this does not start Docker daemon in the container.

Each service you configured using services keyword is running in it’s own Docker container. Further, GitLab Runner by default also starts build container where GIT repo is cloned and the job steps are executed. So when a Job is running there are 2 Docker containers running on the server:

  • docker:dind
  • docker:latest

context directory or also called build context is a Docker term, and it means the working directory which you specify when you run docker build.

In case you want to use Docker to build your image, you need to copy the GIT repo into the Docker image in Dockerfile. Other options depends on your use-case, you can also copy only part of the GIT repo (couple of files you need) or if you need it to build/compile something you can use Multi-stage builds | Docker Docs to build/compile and then just use the binaries in another stage.