8

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.

mzjn
  • 48,958
  • 13
  • 128
  • 248
Derek Chiang
  • 3,330
  • 6
  • 27
  • 34
  • I replicated your steps above using my own docker image, and it worked just fine, no problems. So, the issue must be with the docker image you are basing off of. The file clearly exists. This has to be a permissions issue. You should check to see what user you are by default, your permissions, etc. Can you point us to the `Dockerfile` that alpine came from, and we can give more specifics? – CtheGood Mar 15 '16 at 04:05
  • 1
    After doing some more thinking, I have another hypothesis. The entrypoint for the docker image could be changing your user, or messing with your bash terminal. Try running this `docker run -it hello bash`, and then running `hello` once inside of the container (and post the output). That would tell us if the `entrypoint` and/or `CMD` for the image is doing something strange. The reason why your second example would have worked is because it was overriding the `CMD` of the Dockerfile specifically. – CtheGood Mar 15 '16 at 04:35
  • Hi @CtheGood, thanks for looking into this issue. Your comment made me thinking and I do think that it's some sort of user/group/permission issues. So I wrote the script on a ubuntu computer and I'm trying to run it in an alpine image, and that seems to be what's causing the problem. Once I switched to an ubuntu image, the problem disappears. – Derek Chiang Mar 15 '16 at 05:07

5 Answers5

12

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.

Adam Albright
  • 5,917
  • 1
  • 21
  • 14
7

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.

Geoff
  • 377
  • 3
  • 13
6

Problem solved by using an ubuntu image instead of an alpine image. Not exactly sure why, but might have to do with the file's user/permission bits getting copied over and not interpreted correctly.

Derek Chiang
  • 3,330
  • 6
  • 27
  • 34
  • It can be different shell issue as well! You can try checking the current shell. – UserASR Jan 10 '17 at 12:34
  • 1
    That solved my issue, but I'm still very unsatisfied: what *was* the reason? – payne Jun 15 '20 at 20:43
  • 1
    See my answer below. His script is likely trying to use bash but bash is not in alpine OS by default. Changing his shebang line to /bin/sh would've fixed this instead of switching to ubuntu (which has bash installed by default) – Geoff Mar 09 '23 at 15:01
5

The file not found can be from:

  • the file actually isn't there, check for typos, make sure you aren't trying to run a quoted command and all the arguments together when you should only be running the command (not this issue, but seen it many times). And make sure you aren't mounting a volume over top of where you expect your command.
  • the command at the top of the script doesn't exist. E.g. trying to call bash when the container only has sh. This also applies to those that create the script on Windows and have invalid line feeds at the end of the command. I don't believe Alpine includes a bash where other base images do.
  • the binary you are trying to run links libraries that do not exist inside the container. This is seen with Alpine with the musl libc version differences.
BMitch
  • 231,797
  • 42
  • 475
  • 450
0

I've had similar problem where, no matter how my python app was simple (imagine just hello.py that outputs 'hello world'), when running through docker-compose build && up commands I got error hello.py - not found. It turned out that the issue was that virtualbox did not mount my folder (app) properly. This came as a rescue and resolved my problem in 5 minutes: Docker + Ubuntu + Virtualbox: "volumes" directive in dockerfile not working

Dusan Trtica
  • 71
  • 1
  • 3
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes – slfan Feb 18 '18 at 12:52