5

I am new to docker and attempting to port an app over to it. Essentially, on startup, the app needs to perform some environment dependent work which I have encapsulated in a script.

My app looks like

Dockerfile
scripts/run.sh
build-development/stuff
build-production/stuff

Here is my Dockerfile:

FROM nginx
RUN chmod +x scripts/run.sh
CMD ["scripts/run.sh", APP_ENV]
EXPOSE 80

and here is scripts/run.sh:

#!/bin/bash

mkdir dist

chmod -R 777 dist

if [ $1 == "development" ]
then
    cp -R build-development/. /usr/share/nginx/html
fi

if [ $1 == "stage" ]
then
    cp -R build-production/. /usr/share/nginx/html
    sed -i 's/production/stage/g' dist/index.html
fi

if [ $1 == "production" ]
then
    cp -R build-production/. /usr/share/nginx/html
fi

The idea is that I will run the image like:

docker run -e APP_ENV=development app-name

and it will be fed into the script, which will copy the correct files.

Unfortunately, I never get to run the image because during the build step:

docker build -t app-name .

I get errors about

scripts/run.sh not being a file or directory

I don't understand why I am getting this error, I guess it is from naive misunderstanding on my part. What am I doing wrong and how can I make my project work?

Thanks in advance.

pQuestions123
  • 4,471
  • 6
  • 28
  • 59

3 Answers3

8

It seems like you want scripts/run.sh to be a part of the container. In that case, before you try to run the chmod you need to copy it into the image.

FROM nginx
COPY scripts/run.sh /scripts/run.sh
RUN chmod +x /scripts/run.sh
CMD ["/scripts/run.sh", APP_ENV]

This will copy ./scripts/run.sh (relative to your build directory) into the image at /scripts/run.sh, so your later RUN and CMD have that path available.

Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
7

The issue I ran into with this, is I was editing in windows, which adds the character '/r' to new lines. This was causing errors with SH but not bash. It took me the better part of several hours to figure that out. The other issue was tabs versus spaces, this was also throwing the shell off when it ran the command.

Just figured I'd add that, since that was the issue with the error I was getting.

If you are using notepad++ you can actually change the line endings and text encoding. I normally switch to ANSI and unix line endings to ensure shell does not throw errors.

Otherwise everything worked fine, just when editing in windows line endings get added. Don't know why shell can't handle the extra line endings, but this maybe what someone else is experiencing, when they get the error :no such file. I also had to ensure my docker file was adding the +x to the scripts even if they were originally set in linux, since windows some times messes those up, so I have to be explicit with scripts, just to make sure if I or someone else is downloading the dockerfile/files to their computer they don't lose the executable permission.

Caperneoignis
  • 1,353
  • 13
  • 16
  • 2
    Thanks for pointing this out. Was running into a similar issue with a docker-for-win + WSL environment and the EOL /r was the issue. Thank you thank you! – raupie Jan 31 '18 at 20:30
  • 1
    Here is a helpful answer if you have the script in a git repo, and want to set the .gitattribute. https://stackoverflow.com/a/2517442/970011 – Ashitakalax Oct 28 '20 at 15:52
  • @Ashitakalax thanks! that is something I didn't know. I got use to my IDE doing the removal for me, never looked up if they finally had something to take care of it for me. – Caperneoignis Dec 03 '20 at 20:49
5

In some cases when directory or file get added in .dockerignore, will cause this problem...

So open or vi your .dockerignore and make sure the directory or file not added there...

and run docker build . again..

.dockerignore

Alireza
  • 100,211
  • 27
  • 269
  • 172