Skip to content

Reduce Dockerfile layers and optimize build #2913

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,24 @@ 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
# 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

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 \
&& 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/*
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" ]