Skip to content

Add support for max layer caching in build #477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

dh-seanmurphy
Copy link

@dh-seanmurphy dh-seanmurphy commented Feb 18, 2025

With the change to buildkit as the default option, layers which are not required for the final image are not cached.

This PR support caching of all images in a build process, specifically by leveraging the containerd image store and the flag mode=max for the cache-to.

It requires:

  • addition of a flag to support enabling the containerd image store
  • addition of a CACHE_TO flag
  • addition of a CACHE_FROM_EXPLICIT flag which can take an image name of the form type=registry,ref=<registry>/<cache-image>:<branch>

Build using the standard instructions.

Testing:

  • main.go
package main

import (
        "fmt"
        "log"
        "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
        fmt.Fprint(w, "Hello from Go Docker multistage")
}

func main() {
        http.HandleFunc("/", handler)
        fmt.Println("server running at 0.0.0.0:8080")
        log.Fatal(http.ListenAndServe("0.0.0.0:8080", nil))
}
  • Dockerfile
FROM golang:alpine AS builder
WORKDIR /src/app
COPY go.mod main.go ./
RUN go build -o server

FROM alpine
WORKDIR /root/
COPY --from=builder /src/app ./app
CMD ["./app/server"]
  • build script
#! /usr/bin/env bash

docker run --rm \
  -e PLUGIN_TAG=latest \
  -e PLUGIN_REPO=proxy-it-apps-harbor.localhost/test-project/go-project \
  -e PLUGIN_CACHE_FROM_EXPLICIT="type=registry,ref=proxy-it-apps-harbor.localhost/test-project/dependencies" \
  -e PLUGIN_CACHE_TO=type=registry,image-manifest=true,oci-mediatypes=true,ref=proxy-it-apps-harbor.localhost/test-project/dependencies,mode=max \
  -e DOCKER_REGISTRY=proxy-it-apps-harbor.localhost \
  -e DOCKER_USERNAME=admin \
  -e DOCKER_PASSWORD=<REDACTED> \
  -v $(pwd):$(pwd) \
  -w $(pwd) \
  --privileged \
  --net=host \
  plugins/docker
  • requires a registry to post the images to
  • run go mod init
  • build with mode=min and mode=max and you can see the different sizes of content pushed to the repository

@dh-seanmurphy dh-seanmurphy force-pushed the add-support-for-max-layer-caching-in-build branch from 57c9379 to 9b9bf46 Compare May 20, 2025 07:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant