nginx: invalid option: "off" and entrypoint.sh: : Permission denied

Viewed 7457

I am creating build using Dockerfile and then deploying that build/image via jenkins job but container status is "CrashLoopBackOff" and when I am checking the logs, below error occurs..

Error:-

nginx: invalid option: "off"
/etc/nginx/entrypoint.sh: 5: /etc/nginx/entrypoint.sh: : Permission denied 

Note:- Full permission given to entrypoint.sh. (check the dockerfile)

Dockerfile:-

FROM node:12.18.4-alpine AS BUILD_IMAGE
WORKDIR /app

# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH

COPY package.json .
COPY package-lock.json .
COPY .npmrc .
RUN npm install
COPY . .
RUN npm run build

FROM nginx:latest
RUN apt-get update
RUN apt-get -y install sudo
COPY --from=BUILD_IMAGE /app/build /usr/share/nginx/html
COPY nginx/nginx.conf /etc/nginx/conf.d/default.conf
COPY .build/envs/uat/env.js /app/uat/env.js
COPY .build/envs/prod/env.js /app/prod/env.js
COPY entrypoint.sh /etc/nginx/entrypoint.sh
RUN  sudo chmod 777 /etc/nginx/entrypoint.sh
RUN  sudo nginx -t
RUN  ls -lrt /etc/nginx/

EXPOSE 80
ENTRYPOINT ["/etc/nginx/entrypoint.sh"]
# replace ENVIRONMENT with uat, prod
CMD ["ENVIRONMENT"]

entrypoint.sh file:-

#!/bin/sh
set -e

if [ "$1" = 'uat' ]; then
    "$(cp /app/uat/env.js /usr/share/nginx/html && nginx -g daemon off;)"
elif [ "$1" = 'prod' ]; then
    "$(cp /app/prod/env.js /usr/share/nginx/html && nginx -g daemon off; )"
fi
2 Answers

I spent couple of hours. last versions of nginx images requires calling /docker-entrypoint.sh

# my entrypoint

# do something
# do another thing
#....

# DON't run "exec $@" directly but run:
exec /docker-entrypoint.sh $@

Do not play with CMD. keep it as the default.

imporant point: since nginx 1.19

since nginx 1.19, there are a lot of features in the default entrypoint which lets me to get rid off my custom entrypoints.

e.g: support of nginx config templates files

default.conf.template


http {
  listen ${MY_PORT}

}

Dockerfile

FROM nginx:1.19-alpine
ENV MY_PORT=80
COPY default.conf.template /etc/nginx/templates/

build it then run:

docker run myimage
#--> will run on 80 ( see dockerfile)
docker run -e MY_PORT=8080 myimage
#--> will run on 8080 ( see -e MY_PORT)

If you are curious about the logic of this feature specifically, exec to the container and check the content of this script /docker-entrypoint.d/20-envsubst-on-templates.sh

More details:

  1. Check the directory(/app) is created or not. If not use the below statement in the cfg file and give permissions to that directory
RUN mkdir -p /app

RUN chmod -R 777 /app
  1. Give permissions in the entry point script also
#!/bin/sh
set -e

chmod -R 777 /app

if [ "$1" = 'uat' ]; then
    "$(cp /app/uat/env.js /usr/share/nginx/html && nginx -g daemon off;)"
elif [ "$1" = 'prod' ]; then
    "$(cp /app/prod/env.js /usr/share/nginx/html && nginx -g daemon off; )"
fi
  1. For the container to spin up use an infinite loop in the entry point script and copy it to the respective location and restart the container and then can debug

Entry point script:

#!/bin/sh
while true; do sleep 120; done

Copy command:

docker cp <entry point script path> <container_name>:<path to copy the script in container>

docker restart <container_name>
Related