docker – Nginx configuration in OpenShift
I have been struggling with a solution to have nginx running, in OpenShift there is no use of root user during creation of the containers. Then, I was getting the following error:
2023/06/21 10:37:45 [emerg] 1#1: mkdir() "/var/cache/nginx/client_temp" failed (13: Permission denied)
nginx: [emerg] mkdir() "/var/cache/nginx/client_temp" failed (13: Permission denied)
This is happening because the user is not root. The Dockerfile used for the configuration is the following:
# base image
FROM node:14.21.2 as build-deps
#more lines
RUN yarn build
FROM nginx:1.19-alpine
COPY --from=build-deps /usr/src/app/build /usr/share/nginx/html
COPY --from=build-deps /usr/src/app/nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
RUN printenv
RUN ln -s /tmp/env.js /usr/share/nginx/html/static/env.js
CMD ["sh", "-c", "envsubst < /usr/share/nginx/html/static/env.template.js > /tmp/env.js && exec nginx -g 'daemon off;'"]
Then, I changed the nginx image to use nginxinc/nginx-unprivileged:1.23.2 to avoid the issue when creating the directory /var/cache/nginx/client_temp
. I modified the Dockerfile shown above to remove the symbolic link and modified the last line to refer to the original path:
CMD ["sh", "-c", "envsubst < /usr/share/nginx/html/static/env.template.js > /usr/share/nginx/html/static/env.js && exec nginx -g 'daemon off;'"]
However, because this image (Debian OS) is using nginx as user, I am having issues to create the file env.js:
sh: 1: cannot create /usr/share/nginx/html/static/env.js: Permission denied
If I want to proceed with the creation of symbolic link as the first dockerfile example, then I get the following error:
ln: failed to create symbolic link '/usr/share/nginx/html/static/env.js': Permission denied
This image is using nginx belonging to the group nginx:
$ howami
/bin/sh: 1: howami: not found
$ id nginx
uid=101(nginx) gid=101(nginx) groups=101(nginx)
attempting to create a symbolic link to env.js file in static folder is failing because nginx is not part of the root group:
$ cd /usr/share/nginx/html/
$ ls -ltr
drwxr-xr-x 5 root root 4096 Jun 20 22:58 static
I cannot change permissions of the directory because this is happening before having the container up.
How could I have the environment variables under /usr/share/nginx/html/static folder in the same way I was trying to do with envsubst and have env.js with the values?
Thanks a lot
Read more here: Source link