Kubectl: get a shell to a running container under Windows - 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

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"

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

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

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"]

Running shell script using Docker image

Input:
- There is Windows machine with Docker Toolbox installed.
- There is a shell script file baz.sh which calls py2dsc-deb.
Problem: py2dsc-deb is not available on Windows.
As I understand correctly, I can pull some Linux distro image from Docker repository, create a container and then execute shell-script file and it will run py2dsc-deb and do its job.
I have pulled:
debian - stretch-slim - 3ad21 - 3 weeks ago - 55.3MB
Now
How do I run my script using debian, something like: docker exec mycontainer /path/to/test.sh?
Running docker --rm debian:stretch-slim does nothing. Doesn't it suppose to run Debian distro at docker-machine ip?
I have tried to keep the container up using docker run -it debian:stretch-slim /bin/bash, then run the script using docker exec 1ef5b ./build.sh, but getting
$ docker exec 745 ./build.sh
rpc error: code = 2 desc = oci runtime error: exec failed: container_linux.go:247: starting container process caused "exec: \"./build.sh\": stat ./build.sh: no such file or directory"
Does it mean I can't run external script and has to always pass it inside the Docker?
You can execute bash command inside your container by typing
docker exec -ti -u `username` `container_name` bash -c "cd /path/to/ && ./test.sh"
lets say your container name is test_buildbox, you are root and your script stays inside /bin/test.sh You can call this script by typing
docker exec -ti -u root test_buildbox bash -c "cd /bin/ && ./test.sh
Please check if you have correct line endings in your .sh scripts (<LF>) when you built Docker image on Windows.

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