3

I am running Docker (1.10.2) on Windows. I created a script to echo 'Hello World' on my machine and stored it in C:/Users/username/MountTest. I created a new container and mounted this directory (MountTest) as a data volume. The command I ran to do so is shown below:

docker run -t -i --name mounttest -v /c/Users/sarin/MountTest:/home ubuntu /bin/bash

Next, I run the command to execute the script within the container mounttest.

docker exec -it mounttest sh /home/helloworld.sh

The result is as follows:

: not foundworld.sh: 2: /home/helloworld.sh:
Hello World

I get the desired output (echo Hello World) but I want to understand the reason behind the not found errors.

Note: This question might look similar to Run shell script on docker from shared volume, but it addresses permission related issues.

References: The helloworld.sh file:

#!/bin/sh

echo 'Hello World'

The mounted volumes information is captured below. enter image description here

Community
  • 1
  • 1
Sarin
  • 197
  • 3
  • 13

1 Answers1

1

Considering the default ENTRYPOINT for the 'ubuntu' image is sh -c, the final command executed on docker exec is:

sh -c 'sh /home/helloworld.sh'

It looks a bit strange and might be the cause of the error message.

Try simply:

docker exec -it mounttest /home/helloworld.sh
# or
docker exec -it mounttest sh -c '/home/helloworld.sh'

Of course, the docker exec should be done in a boot2docker ssh session, simalar to the shell session in which you did a docker run.
Since the docker run opens a bash, you should make a new boot2docker session (docker-machine ssh), and in that new boot2docker shell session, try the docker exec.

Trying docker exec from within the bash made by docker run means trying to do DiD (Docker in Docker). It is not relevant for your test.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for the answer. However, the commands suggested above do not solve the problem. I get the following error, running those commands: 'sh: 1: /home/helloworld.sh: not found' – Sarin Feb 29 '16 at 15:32
  • @Sarin Just to be sure, are you running `docker exec` just after the `docker run -t -i --name mounttest -v /c/Users/sarin/MountTest:/home ubuntu /bin/bash`? Meaning: are you running `docker exec` within the bash session made by the `docker run`? Because if so, that is not how it works. – VonC Feb 29 '16 at 16:01
  • @Sarin I have edited the answer to make clear what command should be typed where. – VonC Feb 29 '16 at 16:04
  • I execute the command from outside the bash session. Here is how I do it. 1. Create a container using docker run. 2. Exit the bash session, which also stops the container. 3. Start the container using docker start. 4. Then run docker exec. I am getting errors running other shell scripts as well. In those scripts I create directories which fails as well. Can you shed some light on how you would execute a shell script and maybe share the script. – Sarin Feb 29 '16 at 16:17
  • @Sarin instead of exiting and restarting the container, try and open a second `docker-machine ssh` session, and test your `docker exec` there. – VonC Feb 29 '16 at 16:19