Dockerfile: improve CLI/rootlesskit caching

Use bind-mounts instead of a `COPY` for cli.sh, and use `COPY --link`
for rootlesskit's build stage.

Signed-off-by: Bjorn Neergaard <bjorn.neergaard@docker.com>
This commit is contained in:
Bjorn Neergaard 2023-07-17 09:50:27 -06:00
parent 235cd6c6b2
commit 12a19dcd84
No known key found for this signature in database

View file

@ -251,11 +251,11 @@ RUN --mount=type=cache,target=/root/.cache/go-build \
FROM base AS dockercli FROM base AS dockercli
WORKDIR /go/src/github.com/docker/cli WORKDIR /go/src/github.com/docker/cli
COPY hack/dockerfile/cli.sh /download-or-build-cli.sh
ARG DOCKERCLI_REPOSITORY ARG DOCKERCLI_REPOSITORY
ARG DOCKERCLI_VERSION ARG DOCKERCLI_VERSION
ARG TARGETPLATFORM ARG TARGETPLATFORM
RUN --mount=type=cache,id=dockercli-git-$TARGETPLATFORM,sharing=locked,target=./.git \ RUN --mount=source=hack/dockerfile/cli.sh,target=/download-or-build-cli.sh \
--mount=type=cache,id=dockercli-git-$TARGETPLATFORM,sharing=locked,target=./.git \
--mount=type=cache,target=/root/.cache/go-build,id=dockercli-build-$TARGETPLATFORM \ --mount=type=cache,target=/root/.cache/go-build,id=dockercli-build-$TARGETPLATFORM \
rm -f ./.git/*.lock \ rm -f ./.git/*.lock \
&& /download-or-build-cli.sh ${DOCKERCLI_VERSION} ${DOCKERCLI_REPOSITORY} /build \ && /download-or-build-cli.sh ${DOCKERCLI_VERSION} ${DOCKERCLI_REPOSITORY} /build \
@ -263,12 +263,12 @@ RUN --mount=type=cache,id=dockercli-git-$TARGETPLATFORM,sharing=locked,target=./
FROM base AS dockercli-integration FROM base AS dockercli-integration
WORKDIR /go/src/github.com/docker/cli WORKDIR /go/src/github.com/docker/cli
COPY hack/dockerfile/cli.sh /download-or-build-cli.sh
ARG DOCKERCLI_INTEGRATION_REPOSITORY ARG DOCKERCLI_INTEGRATION_REPOSITORY
ARG DOCKERCLI_INTEGRATION_VERSION ARG DOCKERCLI_INTEGRATION_VERSION
ARG TARGETPLATFORM ARG TARGETPLATFORM
RUN --mount=type=cache,id=dockercli-integration-git-$TARGETPLATFORM,sharing=locked,target=./.git \ RUN --mount=source=hack/dockerfile/cli.sh,target=/download-or-build-cli.sh \
--mount=type=cache,target=/root/.cache/go-build,id=dockercli-integration-build-$TARGETPLATFORM \ --mount=type=cache,id=dockercli-git-$TARGETPLATFORM,sharing=locked,target=./.git \
--mount=type=cache,target=/root/.cache/go-build,id=dockercli-build-$TARGETPLATFORM \
rm -f ./.git/*.lock \ rm -f ./.git/*.lock \
&& /download-or-build-cli.sh ${DOCKERCLI_INTEGRATION_VERSION} ${DOCKERCLI_INTEGRATION_REPOSITORY} /build \ && /download-or-build-cli.sh ${DOCKERCLI_INTEGRATION_VERSION} ${DOCKERCLI_INTEGRATION_REPOSITORY} /build \
&& /build/docker --version && /build/docker --version
@ -369,8 +369,8 @@ RUN --mount=from=rootlesskit-src,src=/usr/src/rootlesskit,rw \
xx-go build -o /build/rootlesskit-docker-proxy -ldflags="$([ "$DOCKER_STATIC" != "1" ] && echo "-linkmode=external")" ./cmd/rootlesskit-docker-proxy xx-go build -o /build/rootlesskit-docker-proxy -ldflags="$([ "$DOCKER_STATIC" != "1" ] && echo "-linkmode=external")" ./cmd/rootlesskit-docker-proxy
xx-verify $([ "$DOCKER_STATIC" = "1" ] && echo "--static") /build/rootlesskit-docker-proxy xx-verify $([ "$DOCKER_STATIC" = "1" ] && echo "--static") /build/rootlesskit-docker-proxy
EOT EOT
COPY ./contrib/dockerd-rootless.sh /build/ COPY --link ./contrib/dockerd-rootless.sh /build/
COPY ./contrib/dockerd-rootless-setuptool.sh /build/ COPY --link ./contrib/dockerd-rootless-setuptool.sh /build/
FROM rootlesskit-build AS rootlesskit-linux FROM rootlesskit-build AS rootlesskit-linux
FROM binary-dummy AS rootlesskit-windows FROM binary-dummy AS rootlesskit-windows