10

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.

3
  • 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? Commented Mar 15, 2016 at 4: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. Commented Mar 15, 2016 at 4: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. Commented Mar 15, 2016 at 5:07

6 Answers 6

16

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.

Sign up to request clarification or add additional context in comments.

Comments

11

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.

1 Comment

Most likely answer. Also, you might be able to use /bin/ash (Almquist Shell) but I haven't tried it.
7

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.

4 Comments

It can be different shell issue as well! You can try checking the current shell.
That solved my issue, but I'm still very unsatisfied: what was the reason?
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)
Sheesh, thank you! Not the first time I've been bitten by Alpine. For me this happened when building in WSL2. I thought it was a \r\n problem, but nope, it was Alpine.
7

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.

Comments

1

The problem might be:

On alpine containers, having the not found error is a typical symptom of dynamic link failure. We need to bear in mind that alpine uses the musl libc library so if the executable is looking for glibc executables it won't find them.

To solve the problem add the following line to your Dockerfile.

RUN apk add libc6-compat

The solution and explanation was found at https://pet2cattle.com/2022/11/alpine-not-found

Comments

-1

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

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.