File not found in docker container

Viewed 31294

I'm doing something extremely simple. Here is my Dockerfile:

FROM alpine:latest

ADD hello.sh /bin/hello
RUN chmod +x /bin/hello

CMD /bin/hello

Then I build the image:

docker build -t hello .

Then I run the image:

docker run hello

And here is the output:

/bin/sh: /bin/hello: not found

Why is this happening? If I run:

docker run hello cat /bin/hello

I can see the content of my script, which makes me even more confused.

5 Answers

In my case the problem was that the line-endings for the script were Windows encoded instead of Linux. (Note: This was actually caused by git automatically converting them, which I disabled)

The first line of the script provides the file path to the shell. Because of the extra \r that is included with Windows EOL the system was trying attaching that hidden character to the filename and that's what caused the Not Found error.

This sounds like you were trying to execute a bash script but alpine by default does not include bash. Instead you need to have your script execute using sh by changing your shebang line to be #!/bin/sh.

In effect, the script is being executed but it's actually saying that /bin/bash does not exist.

Related