How to bind/mount my repository into a Docker container

Hey there,
we’re trying to move from a standalone runner over to the Docker-based runner. So the concepts are bit different and not that easy to understand at all.

I have a repo with some code inside and i want to use a runner to validate it. I set up a Dockerfile with all my necessary tools and pushed it to my registry. In the next stage and want this container to check the code inside my repository. But I’m struggeling to bind/mount my repo-folder into the container.

variables:
  DOCKER_DRIVER: overlay2
  DOCKER_BUILDKIT: 1
  CI_REGISTRY: *************
  CI_REGISTRY_IMAGE: ************/test
  CI_REGISTRY_USER: ******
  CI_REGISTRY_PASSWORD: ***********


stages:
- build
- test

before_script:
  - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY

build_container:
  stage: build
  only:
    changes:
      - Dockerfile
  tags:
    - docker
  before_script:
    - docker pull $CI_REGISTRY_IMAGE:latest 
  script:
    - docker build --cache-from $CI_REGISTRY_IMAGE:latest -t $CI_REGISTRY_IMAGE .
  after_script:
    - docker push $CI_REGISTRY_IMAGE:latest

validate:
  stage: test
  script:
    - docker run $CI_REGISTRY_IMAGE /bin/ash -c "uname -a && java -version && ls -la && ls -la /mnt"

I already tried to extend the docker run with -v $CI_PROJECT_DIR:/mnt but I got an error that “-v” is not an executable.

So, there are two things I would like to know:

  1. Is my idea by building a container and re-use it for every validation run right?
  2. How can I mount a volume inside my container or is there another / better solution?

Thx, Holger

I think we need to see your full docker run to have any chance at helping, but -v is to be used on the docker switches (not after gitlab/gitlab-runner exec ...).

Also, your docker run command is the outter-most shell (unless you script it). It starts the running in the environment you tell docker to provide, then it pulls your code from repo. Despite the fact that the runner depends on the repo, it still doesn’t want to run unless WORKDIR is a git repo so the --workdir ${PWD} and -v ${PWD}:${PWD} takes care of that. It mounts your working dir (run from the root of your project) and makes that dir the Docker WORKDIR.

Another facet of this system I didn’t find well-documented is that exec is meant to do a single run (but will still pull from repo), while run is meant to act as a continuous runner that will be triggered by repo commits.

I’m having issues getting /var/run/docker.sock mounted for some reason today so I can’t be sure this is right (learning myself), but this should be close enough to give you the general idea of what I think is right/close.

# Manually execute gitlab-runner on our last commit locally
sudo docker run --name=runner-${PWD##*/} \
    --user $(id -u):$(id -g)
    --privileged -it --rm \
    --workdir ${PWD} \
    -e DOCKER_HOST \
    -e DOCKER_DRIVER=overlay2 \
    -e DOCKER_TLS_CERTDIR="" \
    -e FF_NETWORK_PER_BUILD=true \
    -v /var/run/docker.sock:/var/run/docker.sock \
    -v ${PWD}/.gitlab-runner/:/etc/gitlab-runner \
    -v ${PWD}:${PWD} \
    gitlab/gitlab-runner:alpine exec docker \
        build;