【问题标题】:How to change the nginx process user of the official docker image nginx?如何更改官方docker镜像nginx的nginx进程用户?
【发布时间】:2021-08-30 18:25:48
【问题描述】:

我使用的是 Docker Hub 的官方 nginx 镜像: https://hub.docker.com/_/nginx/

nginx的用户(定义在/etc/nginx/nginx.conf中)是nginx。有没有办法让 nginx 以www-data 运行而无需扩展 docker 映像?这样做的原因是,我有一个共享卷,供多个容器使用 - php-fpm 我以 www-datanginx 运行。共享卷中文件/目录的所有者是www-data:www-data,而nginx 无法访问该文件/目录 - 错误类似于*1 stat() "/app/frontend/web/" failed (13: Permission denied)

我有一个docker-compose.yml 并运行我的所有容器,包括带有docker-compose up 的 nginx 容器。

  ...
  nginx:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./:/app
      - ./vhost.conf:/etc/nginx/conf.d/vhost.conf
    links:
      - fpm

  ...

【问题讨论】:

  • 我认为更好的方法是创建自己的 Dockerfile,这样它是可重现的。您可以登录到容器并更改用户并重新启动容器,但这可能是将来的维护问题。

标签: nginx docker


【解决方案1】:

仅供参考

  1. 是php-fpm图片的问题
  2. 这不是关于用户名,而是关于 www-data 用户 ID

做什么

修复你的 php-fpm 容器,不要破坏好的 nginx 容器。

解决方案

【讨论】:

  • 经过数小时的搜索帮助很大!我建议其他与我遇到相同问题的人(Docker - nginx - php-fpm 组合的权限)注意这里。
  • 对于php:rc-fpm-alpine Alpine linux 镜像,usermod 不存在,因为 Alpine 使用了busybox。这是等效的:RUN deluser www-data && adduser -DH -h /home/www-data -s /sbin/nologin -u 1000 www-data - 希望对使用 alpine 的人有所帮助。
  • 为什么是1000??? nginx 容器中的 Nginx 用户不是 1000,因此无权读取 php-fpm 创建的文件
【解决方案2】:

我知道 OP 要求提供一个不扩展 nginx 映像的解决方案,但我已经在没有这个限制的情况下登陆了这里。所以我把这个Dockerfile 设置为www-data:www-data (33:33) 运行nginx:

FROM nginx:1.17

# Customization of the nginx user and group ids in the image. It's 101:101 in
# the base image. Here we use 33 which is the user id and group id for www-data
# on Ubuntu, Debian, etc.
ARG nginx_uid=33
ARG nginx_gid=33

# The worker processes in the nginx image run as the user nginx with group
# nginx. This is where we override their respective uid and guid to something
# else that lines up better with file permissions.
# The -o switch allows reusing an existing user id
RUN usermod -u $nginx_uid -o nginx && groupmod -g $nginx_gid -o nginx

它在映像构建期间接受命令行上的 uid 和 gid。制作一个作为当前用户 ID 和组 ID 运行的 nginx 映像,例如:

docker build --build-arg nginx_uid=$(id -u) nginx_uid=$(id -g) .

图片中的 nginx 用户和组 ID 当前为 hardcoded to 101:101

【讨论】:

  • 如果运行 alpine nginx 在尝试运行 usermod 之前添加阴影。将上面的 usermod 行更改为: RUN apk add shadow && usermod -u $nginx_uid -o nginx && groupmod -g $nginx_gid -o nginx
【解决方案3】:

另一种选择是从https://github.com/nginxinc/docker-nginx获取源并更改docker文件以支持构建参数例如:更改buser版本的稳定Dockerfile(https://github.com/nginxinc/docker-nginx/blob/master/stable/buster/Dockerfile)。将 nginx 用户/组 uid/gid 设置为构建参数

FROM debian:buster-slim

LABEL maintainer="NGINX Docker Maintainers <docker-maint@nginx.com>"

ENV NGINX_VERSION   1.18.0
ENV NJS_VERSION     0.4.3
ENV PKG_RELEASE     1~buster

#Change NGNIX guid/uid#
ARG nginx_guid=101
ARG nginx_uid=101

RUN set -x \
# create nginx user/group first, to be consistent throughout docker variants
    && addgroup --system --gid $nginx_guid nginx \
    && adduser --system --disabled-login --ingroup nginx --no-create-home --home /nonexistent --gecos "nginx user" --shell /bin/false --uid $nginx_uid nginx \

这种方式比只做 usermod 更安全,因为如果在其他地方做某事,比如 chown nginx:nginx 它将使用 GUID/UID 集

【讨论】:

    猜你喜欢
    • 2015-07-06
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    • 1970-01-01
    • 2022-12-21
    相关资源
    最近更新 更多