Using Docker in CI

Build, tag, and push Docker images in your CI pipelines.

Docker is very popular in software development. Lots of teams use it for distributing software products.

One of the most popular use cases is this workflow:

  1. Build the application using a pre-existing Docker image with the required dependencies.
  2. Run tests against the build.
  3. Create a Docker image based on the build.
  4. Push to a remote Docker repository.
  5. Deploy the Docker image to a cluster.

There are few ways to enable Docker support during job execution. All of them require a special Snake Runner configuration.

Docker Socket Binding

Requirements: snake-runner version >= 0.3

Specify a special configuration parameter in the Snake Runner config (which is located in /etc/snake-runner/snake-runner.conf):

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

If you don’t have a configuration file for Snake Runner yet, you can create a file on your host system in /etc/snake-runner/snake-runner.conf and pass it as a Docker volume (using the -v flag) to your docker run command:

docker run \
    <your-docker-flags-here> \
    -v /etc/snake-runner/snake-runner.conf:/etc/snake-runner/snake-runner.conf \

Also, you can specify the list of volumes using the environment variable SNAKE_DOCKER_VOLUMES with the -e flag for your docker run command instead of passing the configuration file:

docker run \
    <your-docker-flags-here> \
    -e 'SNAKE_DOCKER_VOLUMES=["/var/run/docker.sock:/var/run/docker.sock"]' \

NOTE: SNAKE_DOCKER_VOLUMES is a YAML list of strings, so don’t forget about brackets and quotes.

Use cases

Building the Docker image with dependencies and using it for the next job

Requirements: snake-runner version >= 0.3

In the following example, we will show you how to build a Docker image and run the next job based on this Docker image. This is very useful for software products that require lots of dependencies to be installed at build time.

  - infra
  - build

build deps:
  image: docker
  stage: infra
    - docker build -t $CI_REPO_SLUG:$CI_BRANCH -f .

build my code:
  stage: build
    - make PREFIX=. build install

The pipeline consists of two stages: infra and build. The goal of infra stage is to prepare the infrastructure required for running jobs in the build stage.

  • The build deps job uses the docker image in order to utilize the docker command. It builds a Docker image based on a file, and tags it as $CI_REPO_SLUG:$CI_BRANCH. This string will be expanded to a slug made up of the current repository and the name of the current branch separated by a colon.
  • The build my code job uses the $CI_REPO_SLUG:$CI_BRANCH image for building a job container. The commands will be executed inside of the container built from that image.
Last modified October 21, 2020