简体   繁体   中英

command not passed to docker entrypoint

I've been trying to create a docker image that executes kubectl with custom OCI variables. It creates the OCI configuration file automatically and then generates the kube/.config file. I thought of using this because of we have more than one cluster and jumping from them each time is time consuming and it's easy to make mistakes or confuse them.

Basically I created the Dockerfile with the following entrypoint:

FROM private.repo/oci-image:latest
# Install Kubectl client
RUN apt update && apt install -y curl gettext-base
RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.24.0/bin/linux/amd64/kubectl
RUN chmod +x ./kubectl
RUN mv ./kubectl /usr/local/bin/kubectl
...
...
...
ENTRYPOINT ["/bin/bash", "-c", "./script.sh"]

This is the script.sh file

#!/bin/sh

set -e
cat $HOME/.oci/config-template | envsubst > $HOME/.oci/config 

yes | oci ce cluster create-kubeconfig --profile DEFAULT --cluster-id ${K8S_CLUSTER_ID} --file $HOME/.kube/config --region ${REGION} --token-version 2.0.0 --kube-endpoint PUBLIC_ENDPOINT
yes | oci ce cluster create-kubeconfig --profile DEFAULT --cluster-id $K8S_CLUSTER_ID --file $HOME/.kube/config --region $REGION --token-version 2.0.0 --kube-endpoint PUBLIC_ENDPOINT

exec "$@"

" And I have been trying to run the container and pass to it kubectl commands:

docker run -e ... oci-agent:v21 kubectl get nodes

But I am not getting no response. I tried replaceing the exec "$@" with exec "kubectl $@" but I obtain the kubectl help instructions, so it's only executing kubectl and is not reading my command. How do I do this properly please ?

Remove the bash -c wrapper from the ENTRYPOINT

ENTRYPOINT ["./script.sh"]

You're already aware that the CMD is passed as arguments to the ENTRYPOINT . With the bash -c wrapper, these arguments are passed as additional arguments to the wrapper shell, not to your script. The wrapper shell always runs ./script.sh with no arguments, because that's the command it was asked to run; the CMD arguments could be accessed as positional arguments $0 , $1 , ... in that command but this is pretty unusual.

This is the same reason ENTRYPOINT must be a JSON array for it to actually receive the CMD as arguments: Docker turns a string-form ENTRYPOINT (or CMD or RUN ) into ["/bin/sh", "-c", "the string"] and arguments aren't actually passed on to the wrapper script. (You should almost never need to use sh -c inside a Dockerfile.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM