pQuestions123
pQuestions123

Reputation: 4611

No such file or directory in docker build

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.

Upvotes: 5

Views: 40048

Answers (3)

Alireza
Alireza

Reputation: 104870

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

Upvotes: 5

Caperneoignis
Caperneoignis

Reputation: 1399

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.

Upvotes: 7

Dan Lowe
Dan Lowe

Reputation: 56667

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.

Upvotes: 8

Related Questions