Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup GitLab+runners to cache Docker layers

Tags:

git

docker

gitlab

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.

like image 941
basickarl Avatar asked Sep 03 '26 06:09

basickarl


2 Answers

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.

like image 139
Kai Reichart Avatar answered Sep 05 '26 05:09

Kai Reichart


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.

like image 26
Rumen Kyusakov Avatar answered Sep 05 '26 04:09

Rumen Kyusakov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!