Score:0

Nginx configuration in OpenShift

ro flag

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

pt flag
What version of OpenShift are you using? How are you deploying things in OpenShift? Please show us your deployment manifests.
Score:1
pt flag

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.

That's not correct; while OpenShift won't permit you to run containers as root by default, you can certainly configure the necessary permissions. If you're working with OpenShift 4, then the documentation of interest is the section on "Managing security context constraints".

I put together an example demonstrating this for a colleague just yesterday; you can find it here.


In your Dockerfile, this command is failing:

RUN ln -s /tmp/env.js /usr/share/nginx/html/static/env.js

That's because:

  • /usr/share/nginx/html is owned by root
  • You're currently running as the nginx user

You can fix that by explicitly switching to the root user in your Dockerfile:

USER root
RUN ln -s /tmp/env.js /usr/share/nginx/html/static/env.js
USER nginx
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.