nodejs / nodejs/docker-node

Broken NPM cache folder settings do not allow running image without root

オープン
#2,495 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

permissions
主要言語
Dockerfile
スター
8.6k
フォーク
2k
平均マージ
10時間 19分
マージ済み PR(30日)
16

説明

Environment

  • Platform: Linux
  • Docker Version: Docker Engine v29 (but can be replicated with older versions)
  • Node.js Version: LTS 24 and 22 (can be replicated with all versions from v16)
  • Image Tag: 22.22.2-alpine

Expected Behavior

Unmodified Node.js Docker image should be runnable without root privileges.

Current Behavior

The image is completely broken as a base build image in environments where one could not modify a running user, e.g., on Jenkins pipelines which by default run under Jenkins user and the UID is most probably not the same as UID 1000 which is used in the image. The same issue can be observed on the free GitHub Actions runners.

It is also broken as a base runtime image on read-only root filesystem environments, e.g., Kubernetes containers with readOnlyRootFilesystem: true.

The source of these issues is that npm_config_prefix is set to /usr/local and npm_config_userconfig is not set at all, so NPM is trying to use HOME folder which in the docker image is set to /. That folder is not writable by any of the users except root.

The only workaround on Jenkins is to set NPM_CONFIG_CACHE to something like /tmp/jenkins/.npm. GitLab/GitHub runners can be fixed in similar manner.

Possible Solution

The real fix should be to create .npm cache folder somewhere writable by any user on the docker image, so at least it works by default on CI pipelines. Maybe create some kind of documented folder specified via npm_config_userconfig so anyone can map it and modify the behaviour of a running user.

Steps to Reproduce

Create a Jenkins pipeline for any Node.js project using NPM and try to build it:

pipeline {
  agent {
    docker {
      image 'node:22.22.2'
    }
  }
  stages {
    stage('Build and publish') {
      steps {
        sh "npm ci"
        sh "npm run build"
        sh "npm publish"
      }
    }
  }
}
12:51:45  npm error code EACCES
12:51:45  npm error syscall mkdir
12:51:45  npm error path /.npm
12:51:45  npm error errno EACCES
12:51:45  npm error
12:51:45  npm error Your cache folder contains root-owned files, due to a bug in
12:51:45  npm error previous versions of npm which has since been addressed.
12:51:45  npm error
12:51:45  npm error To permanently fix this problem, please run:
12:51:45  npm error   sudo chown -R 1002:1002 "/.npm"

Additional Information

I chose Jenkins as an example. The same issue can be reproduced in any CI/CD environment which doesn't run under root user: rootless Docker, GitHub Actions, etc.

As evident from the past, considering cryptic and sometimes incorrect error messages coming from NPM, people constantly struggle to understand why it doesn't work for them in one way or another:
https://github.com/nodejs/docker-node/issues/1734
https://github.com/npm/cli/issues/3910

I know that there are some practices documented how to run this image without a root user, but that's not the point of this ticket. It can be fixed at the source of this image without the need to use all those workarounds.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、npm_config_prefix、npm_config_userconfig、HOME、および書き込み可能なキャッシュの場所について Docker イメージの設定を調べます。報告された Jenkins パイプラインを、root 以外のユーザーとして、かつルートファイルシステムを読み取り専用にした状態で再現します。npm ci、npm run build、npm publish が、root も実行ごとのキャッシュ上書きも必要とせずに動作すれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
docker, node.js
領域
devops, infrastructure
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
静か
明瞭さ
おおむね明確
初心者へのやさしさ
48/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。