Skip to content

Instantly share code, notes, and snippets.

@EpicWink
Created November 17, 2025 09:56
Show Gist options
  • Select an option

  • Save EpicWink/87036284acb7aa2d7a61e0f7b062b88c to your computer and use it in GitHub Desktop.

Select an option

Save EpicWink/87036284acb7aa2d7a61e0f7b062b88c to your computer and use it in GitHub Desktop.
GitLab CI YAML with Docker image build cache mount persistence
# Docker build cache mount persistence
#
# Usage: add `extends: .docker-build` to your Docker build CI job, and set
# environment variable `DOCKER_CACHE_MOUNTS` to a comma-separated list of
# build cache mount IDs, then use `--mount=type=cache,id=...,target=...`
# in your Dockerfile
#
# Adapted from: https://github.com/reproducible-containers/buildkit-cache-dance
#
# See bottom for license
.docker-build:
image: docker:latest
services:
- docker:dind
variables:
DOCKER_DRIVER: overlay2
DOCKER_TLS_CERTDIR: /certs
before_script:
# Log in to GitLab container registry
- echo "$CI_REGISTRY_PASSWORD"
| docker login --password-stdin
--username "$CI_REGISTRY_USER"
"$CI_REGISTRY"
# Inject Docker build cache mount
- echo -e "\e[0Ksection_start:`date
+%s`:inject-cache-mounts[collapsed=true]\r\e[0K\e[35;1mPopulate Docker
build cache mounts\e[0m"
# build Dockerfile
- cacheInjectDockerfile='FROM alpine:latest'
- |
for name in $(ls .docker-cache-mounts || true); do
echo "Populating Docker build cache mount: $name"
cacheInjectDockerfile="$cacheInjectDockerfile
RUN \
--mount=type=bind,source=.docker-cache-mounts/${name},target=/mnt/i \
--mount=type=cache,id=${name},target=/mnt/o \
echo '${name}:' && \
ls -A /mnt/i && \
cp -rp /mnt/i/* /mnt/o && \
ls -A /mnt/o"
done
- echo "$cacheInjectDockerfile"
# build Docker image, discarding result (only care about build cache mount)
- echo "$cacheInjectDockerfile" | docker build -f - -o - . > /dev/null
|| true
- echo -e "\e[0Ksection_end:`date +%s`:inject-cache-mounts\r\e[0K"
after_script:
# Extract Docker build cache mount
- echo -e "\e[0Ksection_start:`date
+%s`:extract-cache-mounts[collapsed=true]\r\e[0K\e[35;1mSave Docker build
cache mounts\e[0m"
# build Dockerfile
- cacheExtractDockerfile='FROM alpine:latest'
- |
for name in ${DOCKER_CACHE_MOUNTS//,/ }; do
echo "Saving Docker build cache mount: $name"
cacheExtractDockerfile="$cacheExtractDockerfile
RUN \
--mount=type=cache,id=${name},target=/mnt/i \
echo '${name}:' && \
ls -A /mnt/i && \
mkdir -pv '/var/build-cache-extract/$name' && \
cp -rp /mnt/i/* '/var/build-cache-extract/$name' && \
ls '/var/build-cache-extract/$name'"
done
- cacheExtractImage="$CI_REGISTRY_IMAGE/build-cache-extract:$CI_COMMIT_SHORT_SHA"
- echo "$cacheExtractDockerfile"
# build Docker image
- echo "$cacheExtractDockerfile"
| docker build -f - -t "$cacheExtractImage" .
# copy files out of Docker image
- docker run --rm --volume "$CI_PROJECT_DIR/.docker-cache-mounts:/mnt/o"
"$cacheExtractImage"
ash -c 'cp -rp /var/build-cache-extract/* /mnt/o'
- ls -A .docker-cache-mounts/*
# remove Docker image
- docker image rm "$cacheExtractImage"
- echo -e "\e[0Ksection_end:`date +%s`:extract-cache-mounts\r\e[0K"
cache:
key: docker-build-cache
paths:
- .docker-cache-mounts
# MIT License
#
# Copyright (c) 2025 Laurie O
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment