【问题标题】:Could not get uid/gid when building Node/Docker构建 Node/Docker 时无法获取 uid/gid
【发布时间】:2025-12-18 01:50:01
【问题描述】:

我的 Dockerfile 正在使用 alpine 并全局安装 react-scripts。当它尝试安装它时,它会因“无法获得 uid/gid”错误而失败。我在 npm install -g 命令中添加了“---unsafe-perm”选项。 docker 容器已成功创建,但容器中的权限已针对已安装的文件发出消息。我看到所有用户名和组都设置为 1000。我尝试在安装步骤之前将以下命令添加到 Dockerfile,但这没有帮助。

RUN npm -g config set user root

编译错误

Error: could not get uid/gid
[ 'nobody', 0 ]

    at /usr/local/lib/node_modules/npm/node_modules/uid-number/uid-number.js:37:16
    at ChildProcess.exithandler (child_process.js:296:5)
    at ChildProcess.emit (events.js:182:13)
    at maybeClose (internal/child_process.js:961:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:250:5)
TypeError: Cannot read property 'get' of undefined
    at errorHandler (/usr/local/lib/node_modules/npm/lib/utils/error-handler.js:205:18)
    at /usr/local/lib/node_modules/npm/bin/npm-cli.js:76:20
    at cb (/usr/local/lib/node_modules/npm/lib/npm.js:228:22)
    at /usr/local/lib/node_modules/npm/lib/npm.js:266:24
    at /usr/local/lib/node_modules/npm/lib/config/core.js:83:7
    at Array.forEach (<anonymous>)
    at /usr/local/lib/node_modules/npm/lib/config/core.js:82:13
    at f (/usr/local/lib/node_modules/npm/node_modules/once/once.js:25:25)
    at afterExtras (/usr/local/lib/node_modules/npm/lib/config/core.js:173:20)
    at Conf.<anonymous> (/usr/local/lib/node_modules/npm/lib/config/core.js:231:22)
/usr/local/lib/node_modules/npm/lib/utils/error-handler.js:205
  if (npm.config.get('json')) {
                 ^

TypeError: Cannot read property 'get' of undefined
    at process.errorHandler (/usr/local/lib/node_modules/npm/lib/utils/error-handler.js:205:18)
    at process.emit (events.js:182:13)
    at process._fatalException (internal/bootstrap/node.js:472:27)
ERROR: Service 'sample-app' failed to build: The command '/bin/sh -c npm install react-scripts@1.1.1 -g' returned a non-zero code: 

Dockerfile

/usr/src/app # cat Dockerfile
# build environment
FROM node:10-alpine as builder
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
ENV PATH /usr/src/app/node_modules/.bin:$PATH
COPY package.json /usr/src/app/package.json
RUN npm install
RUN npm install react-scripts@1.1.1 -g 
COPY . /usr/src/app
RUN npm run build

# production environment
FROM nginx:1.13.9-alpine
COPY --from=builder /usr/src/app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

【问题讨论】:

    标签: reactjs docker create-react-app react-scripts


    【解决方案1】:

    UPD 修复了in nodejs@12.4.0?

    检查这是否链接到nodejs/docker-node issue 813

    根本原因似乎是:Thread stack size

    glibc 上新线程的默认堆栈大小取决于管理主线程堆栈的资源限制 (RLIMIT_STACK)。
    它通常最终为 2-10 MB。

    有三种可能的解决方案:

    您已经尝试过第三个选项,但也请考虑一下:

    或者,您应该切换到 slim (Debian) 变体,直到 Alpine 团队在上游修复此问题。

    【讨论】:

    • RUN npm config set unsafe-perm true 在 npm 安装之前的 docker 文件中
    • 我选择了第三个选项,它就像一个魅力。对于我的使用,任何性能下降都是微不足道的
    【解决方案2】:

    当我对我的 react 应用程序进行 dockerizing 时,我在 Docker 中遇到了节点高山映像的相同问题

    我通过以下 dockerfile 配置解决了。

    FROM node:8.10.0-alpine
    
    # Set a working directory
    WORKDIR /usr/src/app
    
    COPY ./build/package.json .
    COPY ./build/yarn.lock .
    
    # To handle 'not get uid/gid'
    RUN npm config set unsafe-perm true
    
    # Install Node.js dependencies
    RUN yarn install --production --no-progress
    
    # Copy application files
    COPY ./build .
    
    # Install pm2
    RUN npm install -g pm2 --silent
    
    
    # Run the container under "node" user by default
    USER node
    
    CMD ["pm2", "start", "mypm2config.yml", "--no-daemon", "--env", "preprod"]
    

    【讨论】: