Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File not found in docker container

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.

like image 702
Derek Chiang Avatar asked Mar 15 '16 02:03

Derek Chiang


People also ask

Where are my files in Docker container?

If you use the default storage driver overlay2, then your Docker images are stored in /var/lib/docker/overlay2 . There, you can find different files that represent read-only layers of a Docker image and a layer on top of it that contains your changes.

How do I retrieve a file from a container?

To summarize, follow these steps to copy a file from a Docker container to a host machine: Obtain the name or id of the Docker container. Issue the docker cp command and reference the container name or id. The first parameter of the docker copy command is the path to the file inside the container.

Can I mount a file in Docker?

The Docker CLI provides the –mount and –volume options with a run command to bind a single file or directory. Both flags work similarly but have different syntaxes. As a result, we can use them interchangeably.


1 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.

like image 55
Adam Albright Avatar answered Sep 30 '22 23:09

Adam Albright