Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 976
Gaurav Agnihotri Avatar asked Nov 16 '22 04:11

Gaurav Agnihotri


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

  • https://marcofranssen.nl/nginx-1-19-supports-environment-variables-and-templates-in-docker/

  • https://hub.docker.com/_/nginx

like image 148
Abdennour TOUMI Avatar answered Dec 09 '22 21:12

Abdennour TOUMI