kubectl exec: $PATH: unknown command terminated with exit code 126 - bash

Trying to exec into a container with the following command
kubectl exec -it my-pod my-container1 -- bash
Gives error:
OCI runtime exec failed: exec failed: container_linux.go:367: starting container process caused: exec: "my-container1": executable file not found in $PATH: unknown
command terminated with exit code 126
The pod my-pod has two containers. And my-container1 has alipne image with bash installed.
Trying to get shell into the container, it is not able to find bash.
Kubectl client version: v1.17.0

Adding -c before the container name worked.
kubectl exec -it my-pod -c my-container1 -- bash

Related

Using "docker exec" to run ros2 commands

So i'm currently trying to write a shell script to execute many commands, and i have a certain ros2 commands that need to be run in a docker using bash. I saw that I may be able to use "docker exec" in order to run a command in a docker from a shell script but when i try and use docker exec with ros2 commands it doesn't seem to work.
I've tried...
sudo docker exec my_docker /bin/sh -c "ros2 node list"
/bin/sh: 1: ros2: not found
sudo docker exec my_docker /bin/bash -c "source /root/.bashrc && ros2 node list"
/bin/bash: ros2: command not found
sudo docker exec my_docker /bin/bash -c "./node_list.sh"
/bin/bash: ros2: command not found
./node_list.sh: line 4: ros2: command not found
node_list.sh file:
#!/bin/bash
source /root/.bashrc
#
ros2 node list
sudo docker exec my_docker /opt/ros/humble/bin/ros2 node list
OCI runtime exec failed: exec failed: unable to start container process: exec: "ros2": executable file not found in $PATH: unknown
The reason for this problem is that your ROS installation is not sourced.
Probably there is some issue with your bashrc file. Your local bashrc file does not work when calling a command within a docker container.
You can try to source your installation within the docker container fist and the run the desired command.
sudo docker exec my_docker /bin/bash -c ". /opt/ros/humble/setup.bash && ros2 node list"

How to run a bash script from a Dockerfile on a Mac

I'm trying to run a bash script from a Docker Image on a Mac. Here is my Dockerfile
FROM bash
ADD app.sh /
ENTRYPOINT ["/bin/bash", "/app.sh"]
Error
docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "/bin/bash": stat /bin/bash: no such file or directory: unknown.
This is a simple exercise in creating Docker Images where I need to execute app.sh when I run docker run.
Any idea what I'm doing wrong?
According to your error message, the file /bin/bash does not exist in your Docker image. Why is this?
The bash image puts the bash executable at /usr/local/bin/bash. Here's how I determined this:
$ docker run -it bash
bash-5.1# which bash
/usr/local/bin/bash
bash-5.1#
I ran the bash image with -it to make it interactive, then used the which command to give me the full path to bash, which is /usr/local/bin/bash.
For that reason, you need to change your Dockerfile like this:
FROM bash
ADD app.sh /
ENTRYPOINT ["/usr/local/bin/bash", "/app.sh"]

Run a shell script using kubectl exec - OCI runtime exec failed: exec failed: container_linux.go:346

I am trying a run a shell script via kubectl exec.
Eg- kubectl exec -n abc podxyz -- /root/test/./generate.sh
The script runs in the podxyz container but returns the below error, breaking the rest of the flow.
"command terminated with exit code 126"]
"OCI runtime exec failed: exec failed: container_linux.go:346: starting container process caused \"no such file or directory\": unknown"]}
I have tried to use -- /bin/sh and bash after the -- , but that did not help.
Note - the above command is executed as part of another script.

Kubectl: get a shell to a running container under Windows

I'm trying to log into running container using Kubectl, according to instructions in https://kubernetes.io/docs/tasks/debug-application-cluster/get-shell-running-container/, but I'm failing miserably:
kubectl exec -it mycontainer -- /bin/bash
Unable to use a TTY - input is not a terminal or the right kind of
file rpc error: code = 2 desc = oci runtime error: exec failed:
container_linux.go:247: starting container process caused "exec:
\"D:/Applications/Git/usr/bin/bash\": stat
D:/Applications/Git/usr/bin/bash: no such file or directory"
command terminated with exit code 126
It looks like kubectl tries to exec bash on my machine, which is totally not what I want to achieve.
I can exec commands without spaces:
$ kubectl exec mycontainer 'ls'
lib
start.sh
But with not:
$ kubectl exec mycontainer 'ls .'
rpc error: code = 2 desc = oci runtime error: exec failed: container_linux.go:247: starting container process caused "exec: \"ls .\": executable file not found in $PATH"
command terminated with exit code 126
What I'm doing wrong?
I've tried both in mingw git shell , as with plain windows console.
Seems it might be related to this github issue.
One of the workarounds might be to use winpty as specified here.
winpty kubectl.exe exec -it pod-name -- sh
You can also try /bin/sh instead of /bin/bash it worked for me, but I do not have a Windows machine to check it in the same environment as you.
Below command worked for me to launch windows command prompt
kubectl exec -it mycontainer -- cmd.exe

Can't change directories in a docker container?

I am attempting to run a few commands inside of a docker container from it's host machine. Typically when I want to execute a shell command in a docker container from the host I will do something like this:
docker exec -ti myContainer ls -l
which works fine. But when I try to change directories like this:
docker exec -ti myContainer cd myDirectory
it throws the error:
rpc error: code = 2 desc = "oci runtime error: exec failed: exec:
\"cd\": executable file not found in $PATH"
Does anyone have any idea why this would be happening and how I can resolve it?
cd isn't an OS executable, is a shell command. To run it, you'll need to run a shell to parse it. docker exec -ti myContainer /bin/sh -c "cd myDirectory"

Resources