From 1e317872a67134ee38794cafcaef430ea32ade4f Mon Sep 17 00:00:00 2001 From: Youssef Bayouli <75679079+YoussefBayouli@users.noreply.github.com> Date: Sat, 12 Oct 2024 11:29:56 +0200 Subject: [PATCH 1/2] Optimize Dockerfile by reducing layers and consolidating package installation and cleanup steps Combined multiple apk add steps in both stages to reduce the number of layers. Removed the apk del --purge deps since curl and vim are still required, so no need to remove them. Removed redundant apk --update since --no-cache already ensures that the latest package versions are installed without caching. --- Dockerfile | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index 44b5a19a0b..167bfb2e6f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,7 +7,8 @@ WORKDIR /k9s COPY go.mod go.sum main.go Makefile ./ COPY internal internal COPY cmd cmd -RUN apk --no-cache add --update make libx11-dev git gcc libc-dev curl && make build +# Combine the package installation and build steps +RUN apk --no-cache add make libx11-dev git gcc libc-dev curl && make build # ----------------------------------------------------------------------------- # Build the final Docker image @@ -15,13 +16,12 @@ RUN apk --no-cache add --update make libx11-dev git gcc libc-dev curl && make bu FROM alpine:3.20.3 ARG KUBECTL_VERSION="v1.29.0" +# Combine apk installs, kubectl download, and cleanup into a single layer COPY --from=build /k9s/execs/k9s /bin/k9s -RUN apk add --update ca-certificates \ - && apk add --update -t deps curl vim \ +RUN apk --no-cache add ca-certificates curl vim\ && TARGET_ARCH=$(arch | sed s/aarch64/arm64/ | sed s/x86_64/amd64/) \ && curl -L https://storage.googleapis.com/kubernetes-release/release/${KUBECTL_VERSION}/bin/linux/${TARGET_ARCH}/kubectl -o /usr/local/bin/kubectl \ && chmod +x /usr/local/bin/kubectl \ - && apk del --purge deps \ - && rm /var/cache/apk/* + && rm -rf /var/cache/apk/* ENTRYPOINT [ "/bin/k9s" ] From 6e4100569f108f01a6bdce7494f133d2a14d15b1 Mon Sep 17 00:00:00 2001 From: Youssef Bayouli <75679079+YoussefBayouli@users.noreply.github.com> Date: Sat, 12 Oct 2024 17:51:10 +0200 Subject: [PATCH 2/2] Removing build-time dependencies Removed Build-time Dependencies: In the build stage, I removed the unnecessary build tools (gcc, make, git, etc.) after building the k9s binary. These tools are no longer needed, and deleting them helps reduce the size of the build layer. Cleaned Up kubectl Artifacts: After downloading kubectl, the kubectl.sha256 file (or other temporary files generated by the download process) could take up unnecessary space, so I added a cleanup step to remove them. Hope it helps :D --- Dockerfile | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/Dockerfile b/Dockerfile index 167bfb2e6f..94d23f9a5a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,8 +7,11 @@ WORKDIR /k9s COPY go.mod go.sum main.go Makefile ./ COPY internal internal COPY cmd cmd -# Combine the package installation and build steps -RUN apk --no-cache add make libx11-dev git gcc libc-dev curl && make build +# Install dependencies, build, and clean up +RUN apk --no-cache add make libx11-dev git gcc libc-dev curl \ + && make build \ + && apk del gcc libc-dev git make libx11-dev curl +# Remove build-time dependencies # ----------------------------------------------------------------------------- # Build the final Docker image @@ -18,10 +21,10 @@ ARG KUBECTL_VERSION="v1.29.0" # Combine apk installs, kubectl download, and cleanup into a single layer COPY --from=build /k9s/execs/k9s /bin/k9s -RUN apk --no-cache add ca-certificates curl vim\ - && TARGET_ARCH=$(arch | sed s/aarch64/arm64/ | sed s/x86_64/amd64/) \ - && curl -L https://storage.googleapis.com/kubernetes-release/release/${KUBECTL_VERSION}/bin/linux/${TARGET_ARCH}/kubectl -o /usr/local/bin/kubectl \ - && chmod +x /usr/local/bin/kubectl \ - && rm -rf /var/cache/apk/* +RUN apk --no-cache add ca-certificates curl vim \ + && TARGET_ARCH=$(arch | sed s/aarch64/arm64/ | sed s/x86_64/amd64/) \ + && curl -L https://storage.googleapis.com/kubernetes-release/release/${KUBECTL_VERSION}/bin/linux/${TARGET_ARCH}/kubectl -o /usr/local/bin/kubectl \ + && chmod +x /usr/local/bin/kubectl \ + && rm -rf /var/cache/apk/* /tmp/* /usr/local/bin/kubectl.sha256 ENTRYPOINT [ "/bin/k9s" ]