dockerkubectl

How to setup `kubectl krew` inside Docker Container?


I’m trying to use kubectl with krew inside a Docker container. Here’s my Dockerfile:

FROM alpine:latest
ENV KUBECTL_VERSION=v1.30.5 KREW_VERSION=v0.4.4
RUN apk update && apk add --no-cache curl bash git \
    && curl -LO "https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/linux/amd64/kubectl" \
    && chmod +x kubectl && mv kubectl /usr/local/bin/ \
    && curl -fsSL "https://github.com/kubernetes-sigs/krew/releases/download/${KREW_VERSION}/krew-linux_amd64.tar.gz" | tar xz -C /tmp \
    && mv /tmp/krew-* /usr/local/bin/krew \
    && /usr/local/bin/krew install konfig
ENV PATH="/root/.krew/bin:${PATH}"
CMD ["tail", "-f", "/dev/null"]

Steps I followed:

  1. Built the image: docker build -t kubectl-krew .
  2. Ran the container: docker run -it kubectl-krew
  3. Inside the container:
    • kubectl version works and shows:
      Client Version: v1.30.5
      Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3
      The connection to the server localhost:8080 was refused - did you specify the right host or port?
      
    • krew version shows:
      OPTION            VALUE
      GitTag            v0.4.4
      GitCommit         343e657
      IndexURI          https://github.com/kubernetes-sigs/krew-index.git
      BasePath          /root/.krew
      IndexPath         /root/.krew/index/default
      InstallPath       /root/.krew/store
      BinPath           /root/.krew/bin
      DetectedPlatform  linux/amd64
      
    • But kubectl krew update fails with:
      error: unknown command "krew" for "kubectl"

Environment variable $PATH is:
/root/.krew/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin.

Why is kubectl krew not recognized? How can I fix this?


Solution

  • HAHA!!! I solved the issue.

    If you look at the krew intallation page you will find the instruction to install krew in the Linux. So, I need to run

    (
      set -x; cd "$(mktemp -d)" &&
      OS="$(uname | tr '[:upper:]' '[:lower:]')" &&
      ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')" &&
      KREW="krew-${OS}_${ARCH}" &&
      curl -fsSLO "https://github.com/kubernetes-sigs/krew/releases/latest/download/${KREW}.tar.gz" &&
      tar zxvf "${KREW}.tar.gz" &&
      ./"${KREW}" install krew
    )
    

    and then finally:

    export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"

    So, I included the above changes in my Dockerfile to below:

    # Base image
    FROM ubuntu:20.04
    
    # Set non-interactive mode for APT
    ENV DEBIAN_FRONTEND=noninteractive
    
    # Install dependencies
    RUN apt-get update && apt-get install -y \
        curl \
        tar \
        git \
        ca-certificates \
        && apt-get clean
    
    # Install kubectl
    RUN curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" && \
        chmod +x kubectl && \
        mv kubectl /usr/local/bin/
    
    # Install krew
    RUN set -x; \
        cd "$(mktemp -d)" && \
        OS="$(uname | tr '[:upper:]' '[:lower:]')" && \
        ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')" && \
        KREW="krew-${OS}_${ARCH}" && \
        curl -fsSLO "https://github.com/kubernetes-sigs/krew/releases/latest/download/${KREW}.tar.gz" && \
        tar zxvf "${KREW}.tar.gz" && \
        ./"${KREW}" install krew && \
        rm -rf "${KREW}.tar.gz" ./"${KREW}"
    
    # Set up PATH for krew
    ENV PATH="${KREW_ROOT:-/root/.krew}/bin:$PATH"
    
    # Verify installation
    RUN kubectl krew version && kubectl version --client
    
    # Set the entrypoint to keep the container alive
    CMD ["tail", "-f", "/dev/null"]
    

    and now when i run kubectl krew version, I get:

    OPTION            VALUE
    GitTag            v0.4.4
    GitCommit         343e657
    IndexURI          https://github.com/kubernetes-sigs/krew-index.git
    BasePath          /root/.krew
    IndexPath         /root/.krew/index/default
    InstallPath       /root/.krew/store
    BinPath           /root/.krew/bin
    DetectedPlatform  linux/amd64