How do I login into a Docker container running inside the specific Kubernetes pod and run an test.sh file?

7/23/2019

I have a test.sh file which need to be run in an docker container which runs inside the Kubernetes pods via Jenkins job.

Flow:

Step1 : Run a Jenkins job which should login to a docker container running inside the pods

Step: From the container execute the test.sh script.

test.sh

echo "This is demo file"

I have a command which will give docker image running in a kubenertes pods

kubectl get pods -n eum -o go-template --template="{{range .items}}{{range .spec.containers}}{{.image}} {{end}}{{end}}" -l app=${TIER}

TIER = list of apps added in jenkins as choice parameter

I wanted to login to the container and run the test.sh script

-- Suresh Ravi
docker
jenkins
kubernetes

1 Answer

7/24/2019

You can use command "kubectl exec ${POD } /bin/bash --all-namespaces -- /bin/sh test.sh" to run directly test.sh inside container, but make sure test.sh is available inside container. But in this case you need to pass pod name in this command. you can get all pods name using command

kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}'

and if you want pods only for specific label then use

kubectl get pods -l -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}'

here replace with your label selector of pods

-- Pawan Kumar
Source: StackOverflow