diff --git a/README.md b/README.md index 6f4de667..61718e22 100644 --- a/README.md +++ b/README.md @@ -188,6 +188,8 @@ docker run -it --rm \ In Kubernetes, you can pre-populate a persistent volume with the same warmer image, then mount it into many workspaces with the [`ReadOnlyMany` access mode](https://kubernetes.io/docs/concepts/storage/persistent-volumes/#access-modes). +A sample script to pre-fetch a number of images can be viewed [here](./examples/kaniko-cache-warmer.sh). This can be run, for example, as a cron job to periodically fetch the latest versions of a number of base images. + ## Setup Script The `SETUP_SCRIPT` environment variable dynamically configures the user and init command (PID 1) after the container build process. diff --git a/examples/kaniko-cache-warmer.sh b/examples/kaniko-cache-warmer.sh new file mode 100755 index 00000000..1c7ef39f --- /dev/null +++ b/examples/kaniko-cache-warmer.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash + +# This is an example script to pull a number of images into the Kaniko cache +# to have them ready for consumption by envbuilder. +# Ref: https://github.com/coder/envbuilder/blob/main/README.md#image-caching +KANIKO_CACHE_VOLUME=${KANIKO_CACHE_VOLUME:-"kanikocache"} +IMAGES=( + alpine:latest + debian:latest + ubuntu:latest +) + +set -euo pipefail + +if ! docker volume inspect "${KANIKO_CACHE_VOLUME}" > /dev/null 2>&1; then + echo "Kaniko cache volume does not exist; creating it." + docker volume create "${KANIKO_CACHE_VOLUME}" +fi + +for img in "${IMAGES[@]}"; do + echo "Fetching image ${img} to kaniko cache ${KANIKO_CACHE_VOLUME}" + docker run --rm \ + -v "${KANIKO_CACHE_VOLUME}:/cache" \ + gcr.io/kaniko-project/warmer:latest \ + --cache-dir=/cache \ + --image="${img}" +done