首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何更改官方docker镜像nginx的nginx进程用户?

如何更改官方docker镜像nginx的nginx进程用户?
EN

Stack Overflow用户
提问于 2016-04-24 22:13:50
回答 3查看 24.7K关注 0票数 17

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

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

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

代码语言:javascript
复制
  ...
  nginx:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./:/app
      - ./vhost.conf:/etc/nginx/conf.d/vhost.conf
    links:
      - fpm

  ...
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-04-28 15:39:10

仅供参考

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

的问题

做什么?

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

解决方案

https://stackoverflow.com/a/36130772/1032085

  • Solution用于官方php-fpm图像。创建Dockerfile:

从php:5.6-fpm运行usermod -u 1000 www-data

票数 19
EN

Stack Overflow用户

发布于 2019-07-14 07:06:08

我知道OP要求一个不扩展nginx镜像的解决方案,但是我在这里没有这个限制。因此,我将此Dockerfile设置为以www-data:www-data (33:33)身份运行nginx:

代码语言:javascript
复制
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镜像,例如:

代码语言:javascript
复制
docker build --build-arg nginx_uid=$(id -u) nginx_uid=$(id -g) .

nginx用户和组in目前在镜像中为hardcoded to 101:101

票数 6
EN

Stack Overflow用户

发布于 2020-09-15 20:05:26

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

代码语言:javascript
复制
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集

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36824222

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档