0

I would like to send a command to an already existing docker container.

I am forced to do it via powershell or CMD, git bash not giving the correct Windows path using pwd.

Functionnal example

$cur_path = $pwd.Path

$container_id = docker run -it -d --volume $cur_path\:/matmuttools saagie/python:3.5.2-1.3.1-centos

docker exec $container_id 'ls'

docker stop $container_id

docker rm $container_id

Gives the following output:

anaconda-post.log
bin
dev
etc
git-credential-manager-2.0.4.jar
home
lib
lib64
lost+found
matmuttools
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var

Non functionnal example

However, when doing something more "complex", docker throws an error:

$cur_path = $pwd.Path

$container_id = docker run -it -d --volume $cur_path\:/matmuttools saagie/python:3.5.2-1.3.1-centos

docker exec $container_id 'ls /matmuttools'

docker stop $container_id

docker rm $container_id

Throws the following error:

OCI runtime exec failed: exec failed: container_linux.go:367: starting container process caused: exec: "ls /matmuttools": stat ls /matmuttools: no such file or directory: unknown

If I pass ls without quotes and without more argument (just ls), it runs in the container. If I pass ls without quotes and with arguments, it runs on the host.

Runs in the container:

docker exec $container_id ls

Runs on the host:

docker exec $container_id ls /

How do I properly send a command to execute to a docker container in powershell (or with CMD) ?

Itération 122442
  • 2,644
  • 2
  • 27
  • 73
  • tried `' -c ls /matmuttools '`? – Lei Yang Jun 28 '21 at 06:07
  • @LeiYang ``OCI runtime exec failed: exec failed: container_linux.go:367: starting container process caused: exec: " -c ls /matmuttools ": stat -c ls /matmuttools : no such file or directory: unknown`` – Itération 122442 Jun 28 '21 at 06:31
  • why you insist powershell? normally we use `docker exec -it -- bash`, and i don't see `-- bash` in your command. i think you can try using WSL. – Lei Yang Jun 28 '21 at 06:33
  • @LeiYang I need powershell because I need the current context path to create the volume. Git bash does not return the actual windows path, there is a layer in between. Moreover, I do not have WSL available. Finally, what do you mean by ``-- bash`` ? – Itération 122442 Jun 28 '21 at 06:39
  • I think you execute ls on a different folder rather than /. Try to execute `ls matmuttools` – Max Jun 28 '21 at 08:47
  • Not sure if this [post](https://stackoverflow.com/a/41489151/13642249) helps out – kyrlon Feb 24 '23 at 21:43

1 Answers1

1

In git bash, to get the actual windows path, use:

pwd -W

Now that I do not need powershell anymore (I am pretty sure it was not a blocking thing, but anyway):

docker exec -it frosty_lamport bash -c "cd /matmuttools; ls"
Itération 122442
  • 2,644
  • 2
  • 27
  • 73