I'm trying to build a docker image with a kubectl binary package installed in it to later use it in Kubernetes deployment as an image to perform kubectl operations on different clusters.
Unfortunately, the docker build was a success and also if I run locally with the docker run command, the kubectl commands work as expected. But, when I deploy this as a pod, and try to execute the kubectl command it throws me out, and the container restarts.
Below is my Dockerfile
FROM python:3.12-alpine3.18
USER root
RUN addgroup -S sregroup && adduser -S sreuser -G sregroup
# ensure local python is preferred over distribution python
ENV PATH /usr/local/bin:$PATH
# runtime dependencies
RUN set -eux
RUN apk update && \
apk add --no-cache bash mailx iputils bind-tools drill netcat-openbsd postfix curl openssl ca-certificates gcc musl-dev libffi-dev openssl-dev git tzdata
RUN pip3 install --no-cache-dir wheel
COPY requirements.txt .
RUN pip3 install --no-cache-dir -r requirements.txt
RUN mkdir /.kube && chmod g+rwX /.kube
RUN curl -LO https://s3.us-west-2.amazonaws.com/amazon-eks/1.27.9/2024-01-04/bin/linux/amd64/kubectl && \
cp kubectl /usr/local/bin/kubectl && \
chmod +x /usr/local/bin/kubectl
USER sreuser
ENTRYPOINT ["/bin/sh"]
Below is the container status and error
$ k get po
NAME READY STATUS RESTARTS AGE
kubectlll-test 1/1 Running 1 (7m40s ago) 8m45s
$ k exec -it kubectlll-test -- /bin/bash
kubectlll-test:/tmp$ kubectl version
command terminated with exit code 137
Can anyone please help me in resolving this issue.
"You must replace
CMD ["sleep", "infinity"]withENTRYPOINT ["/bin/sh"]. Additionally, you can simplify it without the need to install additional packages, as shown in the following example:"Note: The instruction
CMD ["sleep", "infinity"]is used to keep the container running indefinitely without consuming many resources.