docker 升级到节点v16后,CI/CD中出现npm权限错误-从同一映像本地运行container时工作正常

3yhwsihp  于 2022-12-18  发布在  Docker
关注(0)|答案(1)|浏览(248)

从Node 14.15 =〉16.18.1升级后,我在GitHub Actions CI/CD工作流中尝试运行ESLint时开始收到Error: EACCES: permission denied, scandir '/root/.npm/_logs'错误。
注意:我在Docker容器中运行这个脚本,我可以在同一个映像构建的容器中本地运行ESLint脚本,但是当在Github Actions中运行时,工作流失败并出现以下错误:

Creating csps-lint ... done
Attaching to csps-lint
csps-lint    | 
csps-lint    | > lint
csps-lint    | > bash ./scripts/lint.sh
csps-lint    | 
csps-lint    | Running Prettier to identify code formatting issues...
csps-lint    | 
csps-lint    | Checking formatting...
csps-lint    | All matched files use Prettier code style!
csps-lint    | 
csps-lint    | Running ESLint to identify code quality issues...
csps-lint    | npm WARN logfile Error: EACCES: permission denied, scandir '/root/.npm/_logs'
csps-lint    | npm WARN logfile  error cleaning log files [Error: EACCES: permission denied, scandir '/root/.npm/_logs'] {
csps-lint    | npm WARN logfile   errno: -13,
csps-lint    | npm WARN logfile   code: 'EACCES',
csps-lint    | npm WARN logfile   syscall: 'scandir',
csps-lint    | npm WARN logfile   path: '/root/.npm/_logs'
csps-lint    | npm WARN logfile }
csps-lint    | npm ERR! code EACCES
csps-lint    | npm ERR! syscall mkdir
csps-lint    | npm ERR! path /root/.npm/_cacache/tmp
csps-lint    | npm ERR! errno -13
csps-lint    | npm ERR! 
csps-lint    | npm ERR! Your cache folder contains root-owned files, due to a bug in
csps-lint    | npm ERR! previous versions of npm which has since been addressed.
csps-lint    | npm ERR! 
csps-lint    | npm ERR! To permanently fix this problem, please run:
csps-lint    | npm ERR!   sudo chown -R 1001:123 "/root/.npm"
csps-lint    | 
csps-lint    | npm ERR! Log files were not written due to an error writing to the directory: /root/.npm/_logs
csps-lint    | npm ERR! You can rerun the command with `--loglevel=verbose` to see the logs in your terminal
csps-lint    | ESLint found code quality issues. Please address any errors above and try again.
csps-lint    | 
csps-lint exited with code 1
1
Aborting on container exit...
Error: Process completed with exit code 1.

我的GH操作工作流程如下:

name: test-eslint-fix
on:
  push:
    branches:
      - eslint-fix
jobs:
  login:
    runs-on: ubuntu-latest
    steps:
      - uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
  linting:
    needs: login
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - run: LINT_COMMAND=lint docker-compose -f docker-comp-lint.yml up --abort-on-container-

下面是从中创建映像的Dockerfile:

# start FROM a base layer of node v16.18.1
FROM node:16.18.1

# confirm installation
RUN node -v
RUN npm -v

# Globally install webpack in the container for webpack dev server
RUN npm install webpack -g

# Install db-migrate
RUN npm install db-migrate@0.11.11 -g

# Set up a WORKDIR for application in the container
WORKDIR /usr/src/app

# A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json /usr/src/app/

# npm install to create node_modules in the container
RUN npm install

# EXPOSE your server port
EXPOSE 3000

这是我的docker-compose文件:

version: '3'
services:
  csps-lint:
    image: <<my image>>
    container_name: csps-lint
    ports:
      - '3002:3002'
    volumes:
      - ./:/usr/src/app
      - node_modules:/usr/src/app/node_modules
    environment:
      - NODE_ENV=test
      - LINT_COMMAND=${LINT_COMMAND}
    command: npm run ${LINT_COMMAND}
volumes:
  node_modules:

这是运行ESLint命令的bash脚本:

# Run Prettier in check mode and store status
  echo -e "\033[1;33mRunning Prettier to identify code formatting issues...\033[0m\n"
  prettier --check .
  PRETTIER_STATUS=$?

  # Run ESLint in check mode and store status
  echo -e "\n\033[1;33mRunning ESLint to identify code quality issues...\033[0m"
  npx eslint .
  ESLINT_STATUS=$?

我尝试在多个位置添加错误消息中推荐的修复程序,但似乎无法解决问题。我已将此命令放入lint.sh脚本中,将其添加到Dockerfile中,还尝试在docker-compose文件中添加,但无论将命令放在何处,都无法解决问题。

sudo chown -R 1001:123 "/root/.npm"

在一些示例中,无法访问sudo,所以我想知道是否需要先安装sudo?是否需要设置这些权限?从Node v14到Node v16是否有什么变化是我忽略的?
我曾经尝试过使用其他帖子中提供的解决方案,这些帖子中有类似的错误消息,但似乎没有一个解决方案能解决这个问题。
我觉得我一定错过了一些非常小的事情,但我真正迷失的部分是,除了在Dockerfile中升级Node.js版本之外,我没有更改这个工作流。此外,我可以在本地运行这个工作流,而不会在从同一个映像创建的容器中出现问题,但在GH Actions中,我似乎无法通过这个流程。
在本地运行以下命令docker exec csps-lint ls -la /root/.npm/_logs时,输出如下:

total 1080
drwxr-xr-x 1 root root   4096 Dec  2 23:02 .
drwxr-xr-x 1 root root   4096 Dec  2 20:15 ..
-rw-r--r-- 1 root root   1589 Nov 15 06:46 2022-11-15T06_46_44_197Z-debug-0.log
-rw-r--r-- 1 root root   1589 Dec  2 20:15 2022-12-02T20_15_05_450Z-debug-0.log
-rw-r--r-- 1 root root  65102 Dec  2 20:15 2022-12-02T20_15_05_684Z-debug-0.log
-rw-r--r-- 1 root root  59037 Dec  2 20:15 2022-12-02T20_15_09_141Z-debug-0.log
-rw-r--r-- 1 root root 943351 Dec  2 20:15 2022-12-02T20_15_14_433Z-debug-0.log
-rw-r--r-- 1 root root   1706 Dec  2 20:23 2022-12-02T20_23_05_075Z-debug-0.log
-rw-r--r-- 1 root root   1756 Dec  2 20:23 2022-12-02T20_23_16_787Z-debug-0.log
-rw-r--r-- 1 root root   1599 Dec  2 23:02 2022-12-02T23_02_23_793Z-debug-0.log

**更新:**因此,我发现当我在本地运行csps-lint文件夹中的脚本时,用户是root,但当它在CI/CD中运行时,它作为ID为1001的未命名用户运行。我真的不清楚为什么无论在何处运行,从同一映像构建容器时,用户都是不同的。

7gcisfzg

7gcisfzg1#

我相信这个问题是由NPM更新不再在X1 M0 N1 X中运行引起的。经过多次来回和尝试101种不同的事情,我能够通过以下方法解决这个问题:
1.将以下命令添加到我的dockerfile

RUN chown -R node:node /usr/src/app

的末尾
1.将user键添加到我的docker-compose.yml文件

version: '3'
services:
  csps-lint:
    image: <<my image>>
    user: node
    container_name: csps-lint
    ports:
      - '3002:3002'
    volumes:
      - ./:/usr/src/app
      - node_modules:/usr/src/app/node_modules
    environment:
      - NODE_ENV=test
      - LINT_COMMAND=${LINT_COMMAND}
    command: npm run ${LINT_COMMAND}
volumes:
  node_modules:

相关问题