如何以非 root 用户身份运行 nginx 容器?

Ano*_*ous 5 linux permissions containers nginx docker

每次我尝试以非 root 用户身份运行容器时,都会出现以下错误:

 the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:2
Run Code Online (Sandbox Code Playgroud)

Dockerfile:

FROM nginx:1.17.6
RUN chown -R nginx:nginx /var/cache/nginx && \
        chown -R nginx:nginx /var/log/nginx && \
        chown -R nginx:nginx /etc/nginx/conf.d
RUN chmod -R 777 /etc/nginx/conf.d

USER nginx

COPY app/build /usr/share/nginx/html

RUN rm /etc/nginx/conf.d/default.conf

COPY nginx/nginx.conf /etc/nginx/conf.d


CMD ["nginx","-g","daemon off;"]
Run Code Online (Sandbox Code Playgroud)

use*_*157 8

使用来自 nginx 的无根 docker-imager。

图片

nginxinc/nginx-unprivileged  
Run Code Online (Sandbox Code Playgroud)

DockerHub
https://hub.docker.com/r/nginxinc/nginx-unprivileged

GitHub
https://github.com/nginxinc/docker-nginx-unprivileged

  • 如何将默认的 nginx 用户更改为我自己的用户?我不能在此之上执行 addgroup 或 adduser 吗? (3认同)

Mic*_*nne 7

您可以删除(或注释)文件user顶部的指令nginx.conf

当您将 nginx 运行为root. 它定义了拥有 nginx 子进程的 pid 的用户。

当您不运行 nginx 因为root此指令无关紧要时,您的 nginx 子进程将与您的当前用户一起运行。