6

We have our own gitlab repo. We have our own runners.

Hows does one set up the runners and gitlab up to be able to cache docker layers.

An example below of a .gitlab-ci.yml file:

before_script:
  - docker version

test:
  stage: test
  script:
    - docker-compose -f docker-compose.yml build my_job
    - docker-compose -f docker-compose.yml down
    - docker-compose -f docker-compose.yml up --remove-orphans --force-recreate --abort-on-container-exit

I've been trying to read how to do this but all of the documentation is just so confusing.

https://gitlab.com/gitlab-org/gitlab-ce/issues/17861 states:

Using shell executor

This is the best executor as for now to be used when you wan't to cache docker layers. It basically doesn't require any changes, other then adding gitlab-runner to docker group.

Given the git fetch it gives the best possibility of caching docker layers.

How does one set this up? I just cannot find this anywhere in the documentation.

basickarl
  • 37,187
  • 64
  • 214
  • 335

2 Answers2

7

I have found that using the docker-in-docker executor does in fact support cached layers (even though the docs say otherwise).
Simply pull the image first, and then specify it in the cache-from flag.
The || true allows for failure in pulling the image, this is important for when you build the image for the first time (e.g. in a new branch)

  script:
    - docker pull $OLD_DOCKERIMAGE || true
    - docker build -t $NEW_DOCKERIMAGE --cache-from $OLD_DOCKERIMAGE .
    - docker push $NEW_DOCKERIMAGE

Additional requirements: This feature require DOCKER_BUILDKIT enabled, and builds must be done with a build param: --build-arg BUILDKIT_INLINE_CACHE=1.

Cililing
  • 4,303
  • 1
  • 17
  • 35
Kai Reichart
  • 81
  • 1
  • 1
  • 2
    Nice one! But, there are two things more required: enabling `DOCKER_BUILDKIT` and running the build with `--build-arg BUILDKIT_INLINE_CACHE=1`. – Cililing May 25 '22 at 13:32
  • Hello! Does anyone know for how long the inline cache is stored on the gitlab-runner instance and how to manage cache? – Janiis Dec 15 '22 at 07:14
5

Since you are building docker images as part of the CI jobs it means you most likely already use the shell executor. There are only 3 ways to configure your gitlab runners in order to support building docker images as part of the CI jobs: https://docs.gitlab.com/ee/ci/docker/using_docker_build.html

This document also explains how to configure the shell executor and what are the trade-off between all 3 ways of configuration.

Basically the only runner configuration which does not allow for caching docker layers is using the docker-in-docker executor.

Using shell and docker socket binding always cache the layers locally on the VM which is hosting the gitlab runner so no further configuration is required to enabling caching.