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
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With