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

时间:2020-07-27 03:02:53

标签: linux docker nginx permissions containers

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

 the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:2

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;"]

2 个答案:

答案 0 :(得分:3)

user文件顶部有一个nginx.conf指令。当您以root的身份运行nginx时,此伪指令是相关的。它定义拥有您的nginx子进程的pid的用户。

如果您不以root的身份运行nginx,则此指令无关紧要,您的nginx子进程将与当前用户一起运行。

答案 1 :(得分:0)

使用nginx的无根docker-imager。

图片

nginxinc/nginx-unprivileged  

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

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