38

Project contents:

rob@work:~/git/proj $ ls
lib     node_modules  props.json    start.sh
app.js  Dockerfile    package.json  README.md

start.sh ..

rob@work:~/git/proj $ cat start.sh 
#/bin/bash

# do things
/some/other/stuff

echo "Starting app .."
node app.js

Dockerfile ..

FROM somewhere.com/dependencyProj
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app
COPY props.json /etc/someService.d/props.json
EXPOSE 4101

ENTRYPOINT ["/start.sh"]

Build docker image:

rob@work:~/git/proj $ docker build -t dx/proj:0.0.1 .
Sending build context to Docker daemon 59.99 MB
Step 1 : FROM somewhere.com/dependencyProj
latest: Pulling from dependencyProj
420890c9e918: Already exists 
8ff1af46fe3d: Already exists 
6db3a1c6f4ca: Already exists 
d82a90c4ea1b: Already exists 
f32685681727: Already exists 
797dfb291196: Already exists 
3a713b0b523e: Already exists 
a9c617bff63b: Already exists 
9ab84732ac6e: Already exists 
2a85e0afdd4d: Already exists 
a56b24146ce4: Already exists 
0a91b00da1f7: Already exists 
836b0e7c1105: Already exists 
Digest: sha256:36b7a32bd12b85cbb2fb3203d43807c9b8735d6ceb50d813b76bfe2e5c3ebeb4
Status: Downloaded newer image for somewhere.com/dependencyProj:latest
 ---> 7c52bbbc3feb
Step 2 : RUN mkdir -p /usr/src/app
 ---> Running in aab7cf1f7974
 ---> 250317f63adf
Removing intermediate container aab7cf1f7974
Step 3 : WORKDIR /usr/src/app
 ---> Running in f60088532610
 ---> 60f3d9fe88c4
Removing intermediate container f60088532610
Step 4 : COPY . /usr/src/app
 ---> 004e0a440fb5
Removing intermediate container f247d134d88b
Step 5 : COPY props.json /etc/someService.d/props.json
 ---> 03b48249c94c
Removing intermediate container a3636849765d
Step 6 : EXPOSE 4101
 ---> Running in 0056e5c20264
 ---> 867765176927
Removing intermediate container 0056e5c20264
Step 7 : ENTRYPOINT /start.sh
 ---> Running in 80ae316b0629
 ---> d1e65def77ce
Removing intermediate container 80ae316b0629
Successfully built d1e65def77ce

Run docker image:

rob@work:~/git/proj $ docker run -d dx/proj:0.0.1
0fd1f8087cc5be3e085454cf99b7a3795b9ce15909b0f416ae39380f93feaa44
docker: Error response from daemon: Container command '/start.sh' not found or does not exist..
bobbyrne01
  • 6,295
  • 19
  • 80
  • 150

3 Answers3

104

On windows, while building the docker image, i also used to get the same error after building the image, that shell script is missing.. the path and the shebang was also correct.

Later on, i read some where that it may be due to the encoding issue. I just opened the file in sublime editor and then..VIEW->Line Endings-> UNIX and just save the file, and rebuilded the image. Everything worked fine.

I was getting this error, when i was building a image from windows.

Other Option:

Sometime, we forgot to manually change the line format. So,what we can do is add this Run statement before the EntryPoint in dockerfile. It will encode the file in LF format.

 RUN sed -i 's/\r$//' $app/filename.sh  && \  
        chmod +x $app/filename.sh

ENTRYPOINT $app/filename.sh
Utkarsh Yeolekar
  • 1,221
  • 2
  • 9
  • 4
  • 9
    It actually worked, thanks! For others, mind you that the *script file* itself has to use LF-style line endings (meaning that the lines should use \n as line endings instead of \r\n). – D. Visser Oct 02 '17 at 12:30
  • 11
    Omg... lost so many times because of that... I cloned a git repository and forgot that Git for Windows converts LF to CRLF (and does the opposite when you push). – Michaël Polla Oct 22 '17 at 20:03
  • 8
    This was the correct answer for me (running Docker for Windows) where I'd used VS Code to modify a '.sh' file forgetting it'll save it with CR-LF line endings. For someone who doesn't have Sublime Text, you can use Notepad++ to change these back to Linux line-endings. – Connor Goddard Apr 25 '18 at 12:03
  • 1
    Thank you! This was also my issue. VSCode makes it really easy to change line endings thankfully but it had me confused for a while. Weird error message. It exists, I'd assume I'd get some `filename.sh: corrupted` error message or something. Instead error message was `does not exist` which threw me off – pourmesomecode Nov 29 '18 at 11:39
  • Mine issue was also same. Everytime In my dockerfile when i am copying a linux system file like crontab or any script there were some characters ^m were added at the end of each line. I can think to add a command dos2unix in dockerfile after running the copy command. Is there any other reliable soultion that i can use ? – Manish Feb 24 '21 at 16:46
  • This solved my problem. It started suddenly not working, I have spent hours on this. Caused by using a wrong editor once, saving it in windows style. Thx. – Kristian Apr 14 '21 at 08:41
  • Despite having my script file locally with LF-style line endings, a setting in our Git repo converted the file to CRLF on the build server, causing the script to fail in the Docker container. +1 for including the `sed` fix, which I will now use. – Tobias Jun 28 '21 at 13:23
  • The bottom line is that we should probably get Linux or Mac – LexSav Dec 27 '22 at 10:21
  • Oh my god, thank you for pointing this out. This encoding thing makes me crazy when building docker images. In Windows and VSCode, it worked fine when I change from the encoding from CRLF to LF. – Dendi Handian Mar 10 '23 at 21:53
45

You have some issues:

  • You must use ./start.sh to run the start.sh file from current directory. /start.sh run start.sh in root /, which does not exist.

  • Your shebang line in start.sh script is wrong, it's must be #!/bin/bash.

  • You also must set executable permission for start.sh, by running chmod +x start.sh.

cuonglm
  • 2,766
  • 1
  • 22
  • 33
  • 49
    I had to change the line endings in the bash script from CRLF to LF. With CRLF line endings, i got the same error. – Gnagy Dec 18 '18 at 10:39
  • 1
    spent a few hours stuck on this and this is what happened to me, when my intellij committed the file it converted the file endings! – zaf187 Mar 29 '19 at 11:32
  • 1
    @Gnagy: that's worth an answer. It was also the case on my device. – Stefan Jul 31 '19 at 20:41
  • 1
    @Gnagy THANK YOU, that did it for me. It's always the little things huh. – Chris Sandvik Mar 11 '20 at 18:16
  • 1
    @Gnagy WOW! That was it as well! – Adrien Dec 27 '20 at 10:13
  • For me the error was triggered by running in an alpine without bash. Strangely didn't complain that cannot find `/bin/bash`. I changed the shebang to `#!/bin/sh` and was ok. – raisercostin May 02 '22 at 17:55
1

In my case, #!/bin/bash didn't work. I needed to use #!/bin/sh

Optimus
  • 1,354
  • 1
  • 21
  • 40