From 6224240858e9255a4f5010d01403cce0d6e57d4a Mon Sep 17 00:00:00 2001 From: Yauheni Kaliuta Date: Tue, 19 Nov 2024 13:18:02 +0200 Subject: [PATCH] Dockerfile: merges manifests builder stages to one We can combine two build stages into one, as there is no need to always build both images (not done by podman) to only then decide from which one we want to copy manifests to the target image. Instead manifests stage will either copy local manifests or fetches using the script based on USE_LOCAL argument. Move USE_LOCAL and OVERWIRTE_MANIFESTS args under FROM since args have scope of the FROM they are declared in. It requires opt/manifests directory to exist, but since it's a part of git repo, it's fine. Original patch from: Bartosz Majsak [1] [1] https://github.com/opendatahub-io/opendatahub-operator/pull/773 Signed-off-by: Yauheni Kaliuta --- Dockerfiles/Dockerfile | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/Dockerfiles/Dockerfile b/Dockerfiles/Dockerfile index 2c6743185ff..77a916cd487 100644 --- a/Dockerfiles/Dockerfile +++ b/Dockerfiles/Dockerfile @@ -1,27 +1,21 @@ # Build the manager binary ARG GOLANG_VERSION=1.21 -ARG USE_LOCAL=false -ARG OVERWRITE_MANIFESTS="" ################################################################################ -FROM registry.access.redhat.com/ubi8/go-toolset:$GOLANG_VERSION as builder_local_false -ARG OVERWRITE_MANIFESTS -# Get all manifests from remote git repo to builder_local_false by script +FROM registry.access.redhat.com/ubi8/toolbox as manifests +ARG USE_LOCAL=false +ARG OVERWRITE_MANIFESTS="" USER root WORKDIR / -COPY get_all_manifests.sh get_all_manifests.sh -RUN ./get_all_manifests.sh ${OVERWRITE_MANIFESTS} - -################################################################################ -FROM registry.access.redhat.com/ubi8/go-toolset:$GOLANG_VERSION as builder_local_true -# Get all manifests from local to builder_local_true -USER root -WORKDIR /opt -# copy local manifests to build COPY opt/manifests/ /opt/manifests/ +COPY get_all_manifests.sh get_all_manifests.sh +RUN if [ "${USE_LOCAL}" != "true" ]; then \ + rm -rf /opt/manifests/*; \ + ./get_all_manifests.sh ${OVERWRITE_MANIFESTS}; \ + fi ################################################################################ -FROM builder_local_${USE_LOCAL} as builder +FROM registry.access.redhat.com/ubi8/go-toolset:$GOLANG_VERSION as builder USER root WORKDIR /workspace # Copy the Go Modules manifests @@ -45,7 +39,7 @@ RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -a -o manager main.go FROM registry.access.redhat.com/ubi8/ubi-minimal:latest WORKDIR / COPY --from=builder /workspace/manager . -COPY --chown=1001:0 --from=builder /opt/manifests /opt/manifests +COPY --chown=1001:0 --from=manifests /opt/manifests /opt/manifests # Recursive change all files RUN chown -R 1001:0 /opt/manifests &&\ chmod -R g=u /opt/manifests