'Command sent to docker container shell exec failed

I am launching a container to issue some command on the host machine files.

This is done via a shell script:

echo "--- Starting container"
container_id=$(docker run -d -it --mount type=bind,source="$mount_path",target=/usr/share --name project-test python:3.5.2-alpine)

docker exec $container_id /bin/sh -c "cd /usr/share && pwd && ls -l"

However this throws an error that I do not understand how to fix:

OCI runtime exec failed: exec failed: container_linux.go:367: starting container process caused: exec: "C:/Program Files/Git/usr/bin/sh": stat C:/Program Files/Git/usr/bin/sh: no such file or directory: unknown

My host machine is a windows 10. From my little understanding, the exec command on the container should opperate inside the container. But the error shows path from my host machine. Why is that ? How to properly send a command to a docker container ?


Edit:

The logs of the container after the docker exec are:

Python 3.5.2 (default, Dec 27 2016, 21:33:11)
[GCC 5.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

Edit: result of the command issued manually (expected result)

/ # cd /usr/share && pwd && ls -l
/usr/share
total 5
-rwxr-xr-x    1 root     root           347 May 20 06:31 README.md  
drwxrwxrwx    1 root     root          4096 May 20 08:22 myproject

docker exec $container_id ls -l /usr/share

ls: C:/Program Files/Git/usr/share: No such file or directory


Solution 1:[1]

Your acquisition of container_id is maybe wrong. Ensure the value is correctly set in this variable before continuing.

Just a note before: you're using -d and -it options simultaneously. The -it is ignored since you tell the container to run as a daemon, you won't attach your TTY to it. The -it options are totally ignored here.

When you exec into a container, it runs a new session in the container. It means you won't see the result of the command you give in the logs since it's in the output of the new session.

To get access to the logs, you must use -it options when using exec. If you refer to @nish8690 on the question Docker exec in docker windows, you'll need to double your slashes in the command:

instead of

docker exec -it [containerid] /bin/sh

try to use

docker exec -it [containerid] //bin//sh

-- @nish8690, Docker exec in docker windows

Resulting maybe in:

docker exec -it $container_id //bin//sh -c "cd /usr/share && pwd && ls -l"

Solution 2:[2]

While using docker from a Windows host, the slash is converted to windows slash so the git is confusing. To access a docker machine from a Windows host, you most to override the windows slash format by using double slash instead.

an example how to run a file, instead of running /run.sh you need to run //run.sh.

This solution will solve your problem.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Paul Rey
Solution 2 Oren Hahiashvili