大网数据广告活动
服务器运维问答

Nginx configuration in OpenShift

· 52CE 我爱测

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/ca…

问题原文

原帖链接 · John Barton · CC BY-SA 4.0

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

已采纳解答

原帖链接 · larsks · CC BY-SA 4.0

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2023-06-21)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。