From abe37c8c1fed95e9c8dc0e60fc980e0cd881756f Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Wed, 3 Jan 2024 12:49:56 -0600 Subject: [PATCH] docs: explain how to cache the base image (#46) --- README.md | 20 ++++++++++++++++++++ envbuilder.go | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b67620e..6f4de66 100644 --- a/README.md +++ b/README.md @@ -168,6 +168,26 @@ Each layer is stored in the registry as a separate image. The image tag is the h The performance improvement of builds depends on the complexity of your Dockerfile. For [`coder/coder`](https://github.com/coder/coder/blob/main/.devcontainer/Dockerfile), uncached builds take 36m while cached builds take 40s (~98% improvement). +## Image Caching + +When the base container is large, it can take a long time to pull the image from the registry. You can pre-pull the image into a read-only volume and mount it into the container to speed up builds. + +```bash +# Pull your base image from the registry to a local directory. +docker run --rm \ + -v /tmp/kaniko-cache:/cache \ + gcr.io/kaniko-project/warmer:latest \ + --cache-dir=/cache \ + --image= + +# Run envbuilder with the local image cache. +docker run -it --rm \ + -v /tmp/kaniko-cache:/image-cache:ro \ + -e BASE_IMAGE_CACHE_DIR=/image-cache +``` + +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). + ## Setup Script The `SETUP_SCRIPT` environment variable dynamically configures the user and init command (PID 1) after the container build process. diff --git a/envbuilder.go b/envbuilder.go index dd85e11..fb40ad4 100644 --- a/envbuilder.go +++ b/envbuilder.go @@ -103,7 +103,7 @@ type Options struct { // BaseImageCacheDir is the path to a directory where the base // image can be found. This should be a read-only directory // solely mounted for the purpose of caching the base image. - BaseImageCacheDir string `env:"BASE_IMAGECACHE_DIR"` + BaseImageCacheDir string `env:"BASE_IMAGE_CACHE_DIR"` // LayerCacheDir is the path to a directory where built layers // will be stored. This spawns an in-memory registry to serve