在用 alpine 构建一个运行静态编译的 nginx 的 docker 镜像时遇到的用户问题

2018-12-08 20:06:54 +08:00
 fourstring

Dockerfile 如下:

FROM ubuntu:latest AS build
LABEL author="fourstring"

ARG NGINX_VER=1.15.7
ARG NGINX_URL=http://nginx.org/download/nginx-${NGINX_VER}.tar.gz
ARG OPENSSL_VER=1.1.1a
ARG OPENSSL_URL=https://www.openssl.org/source/openssl-${OPENSSL_VER}.tar.gz
ARG PCRE_VER=8.42
ARG PCRE_URL=https://ftp.pcre.org/pub/pcre/pcre-${PCRE_VER}.tar.gz
ARG ZLIB_VER=1.2.11
ARG ZLIB_URL=https://zlib.net/zlib-${ZLIB_VER}.tar.gz
ARG NGINX_BUILD_PARAS='--prefix=/usr/local/nginx --with-cc-opt=-static --with-ld-opt=-static --with-threads --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_sub_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --with-openssl=../openssl-${OPENSSL_VER} --with-pcre=../pcre-${PCRE_VER} --with-pcre-jit --with-zlib=../zlib-${ZLIB_VER}'
ARG APT_PACKAGES='build-essential wget tar upx'
ARG APT_REMOVE_PACKAGES='build-essential upx'
ARG MAX_THREADS=3

RUN groupadd -g 50000 www && \
    useradd -g 50000 -u 50000 -s /sbin/nologin www

RUN apt -y update && \
    apt -y install ${APT_PACKAGES} && \
    cd /usr/local/src && \
    wget ${ZLIB_URL} && \
    wget ${PCRE_URL} && \
    wget ${OPENSSL_URL} && \
    wget ${NGINX_URL} && \
    tar xzf nginx-${NGINX_VER}.tar.gz && \
    tar xzf openssl-${OPENSSL_VER}.tar.gz && \
    tar xzf zlib-${ZLIB_VER}.tar.gz && \ 
    tar xzf pcre-${PCRE_VER}.tar.gz && \
    cd nginx-${NGINX_VER} && \
    ./configure ${NGINX_BUILD_PARAS} && \
    make -j${MAX_THREADS} && \
    make install && \
    upx /usr/local/nginx/sbin/nginx && \
    mkdir -p /usr/local/nginx/conf/vhost 

FROM alpine

RUN adduser -g 50000 -u 50000 -s /sbin/nologin -D -H www 

COPY --from=build --chown=www:www /usr/local/nginx /usr/local/nginx
COPY nginx.conf /usr/local/nginx/conf
COPY xxx.conf /usr/local/nginx/conf/vhost

EXPOSE 80
EXPOSE 443
USER www
CMD [ "/usr/local/nginx/sbin/nginx","-g","daemon off;" ]

现在遇到了一些诡异的用户问题。

  1. 如果我不使用最后的USER www指令,那么会报错getpwnam("www") (No such file or directory),由于一般使用不存在的用户运行 nginx 的报错并不会出现(No such file or directory),我用 Google 搜索后找到一个解释说当使用指定的用户以外的用户运行 nginx 就会出现这个错误,加USER www指令后这个报错确实也消失了
  2. 但是由于使用低权限运行,nginx 无法使用 80 和 443 端口
  3. 1.里面提到的那个解释我也表示存疑,因为一般情况下 nginx 主进程需要以高权限开启,并不会报这样的错误

那么请问我应该如何处理这个用户问题?还是我对 Linux 或者 nginx 用户机制的理解有问题呢?谢谢

5364 次点击
所在节点    Alpine Linux
7 条回复
fourstring
2018-12-08 20:34:23 +08:00
另外直接使用 nobody 和 nogroup 运行也依然会出现上面的问题
Lax
2018-12-08 21:21:25 +08:00
看一下执行命令时的工作目录是什么。
手上没环境没法现在测了
fourstring
2018-12-08 21:33:31 +08:00
@Lax #2 把 CMD 换成了 pwd 命令,返回结果就是根目录
zbinlin
2018-12-08 23:05:21 +08:00
去掉
RUN groupadd -g 50000 www && \
useradd -g 50000 -u 50000 -s /sbin/nologin www

RUN adduser -g 50000 -u 50000 -s /sbin/nologin -D -H www

这两句,再把 `--chown=www:www` 也去掉,试试,还有 `USER www`
fourstring
2018-12-09 07:31:01 +08:00
@zbinlin #4 nginx: [emerg] getpwnam("nobody") failed (2: No such file or directory)
我觉得有点奇怪,如果说用自定义的用户不行的话为啥 nobody 也不行呢?我一开始怀疑是不同镜像里 uid 不一样,但是后来手动打开 ubuntu 和 alpine 镜像发现 nobody 用户的 uid 和 gid 都是一样的
silverfox
2018-12-09 09:05:48 +08:00
我注意到你的 builder 是 ubuntu,而 runtime 是 alpine。

Ubuntu 用的是 glibc,Alpine 用的是 musl-c,它们之间应该不能直接共享二进制程序。
在 Ubuntu 上 getpwnam 是调用 nsswitch 来访问用户信息的,也许 Alpine 缺少 nsswitch 相关配置。
[REF]( https://unix.stackexchange.com/questions/386548/nginx-wont-start-getpwnamnginx-failed-in-etc-nginx-nginx-conf5#comment687896_386548)

推荐做法还是使用 Alpine 作为 builder,可以参考官方版本
https://github.com/nginxinc/docker-nginx/blob/1.15.7/mainline/alpine/Dockerfile
fourstring
2018-12-09 13:54:28 +08:00
@silverfox #6 谢谢您!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/515707

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX