diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index 0fef3c625..fb4d799a3 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -93,6 +93,20 @@ jobs: - name: Test for npm run: docker run --rm node:${{ matrix.version }}-${{ matrix.variant }} npm --version + - name: Test npm install as a non-root user with an arbitrary UID + run: | + tmp_dir=$(mktemp -d) + chmod 777 "${tmp_dir}" + echo '{"name":"test","version":"1.0.0"}' > "${tmp_dir}/package.json" + docker run --rm --user 1002:1002 -v "${tmp_dir}:/app" -w /app node:${{ matrix.version }}-${{ matrix.variant }} npm install ms@2.1.3 + + - name: Test npm install with a read-only root filesystem + run: | + tmp_dir=$(mktemp -d) + chmod 777 "${tmp_dir}" + echo '{"name":"test","version":"1.0.0"}' > "${tmp_dir}/package.json" + docker run --rm --user 1002:1002 --read-only --tmpfs /tmp -v "${tmp_dir}:/app" -w /app node:${{ matrix.version }}-${{ matrix.variant }} npm install ms@2.1.3 + - name: Test for yarn if: startsWith(matrix.version, '22') || startsWith(matrix.version, '24') || startsWith(matrix.version, '25') run: docker run --rm node:${{ matrix.version }}-${{ matrix.variant }} yarn --version diff --git a/22/alpine3.23/Dockerfile b/22/alpine3.23/Dockerfile index 465453c59..4fdb98b6f 100644 --- a/22/alpine3.23/Dockerfile +++ b/22/alpine3.23/Dockerfile @@ -106,6 +106,11 @@ RUN apk add --no-cache --virtual .build-deps-yarn curl gnupg tar \ && yarn --version \ && rm -rf /tmp/* +# npm's cache defaults to $HOME/.npm, which is not writable when the image +# is run as a user that has no home directory (e.g. an arbitrary --user UID +# on CI runners), see https://github.com/nodejs/docker-node/issues/2495 +ENV NPM_CONFIG_CACHE=/tmp/.npm + COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["docker-entrypoint.sh"] diff --git a/22/alpine3.24/Dockerfile b/22/alpine3.24/Dockerfile index 4af3a5eb3..93abbf550 100644 --- a/22/alpine3.24/Dockerfile +++ b/22/alpine3.24/Dockerfile @@ -106,6 +106,11 @@ RUN apk add --no-cache --virtual .build-deps-yarn curl gnupg tar \ && yarn --version \ && rm -rf /tmp/* +# npm's cache defaults to $HOME/.npm, which is not writable when the image +# is run as a user that has no home directory (e.g. an arbitrary --user UID +# on CI runners), see https://github.com/nodejs/docker-node/issues/2495 +ENV NPM_CONFIG_CACHE=/tmp/.npm + COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["docker-entrypoint.sh"] diff --git a/22/bookworm-slim/Dockerfile b/22/bookworm-slim/Dockerfile index e31265c0a..1494e1f4f 100644 --- a/22/bookworm-slim/Dockerfile +++ b/22/bookworm-slim/Dockerfile @@ -95,6 +95,11 @@ RUN set -ex \ && yarn --version \ && rm -rf /tmp/* +# npm's cache defaults to $HOME/.npm, which is not writable when the image +# is run as a user that has no home directory (e.g. an arbitrary --user UID +# on CI runners), see https://github.com/nodejs/docker-node/issues/2495 +ENV NPM_CONFIG_CACHE=/tmp/.npm + COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["docker-entrypoint.sh"] diff --git a/22/bookworm/Dockerfile b/22/bookworm/Dockerfile index a23ea2c17..85800c3fe 100644 --- a/22/bookworm/Dockerfile +++ b/22/bookworm/Dockerfile @@ -69,6 +69,11 @@ RUN set -ex \ && yarn --version \ && rm -rf /tmp/* +# npm's cache defaults to $HOME/.npm, which is not writable when the image +# is run as a user that has no home directory (e.g. an arbitrary --user UID +# on CI runners), see https://github.com/nodejs/docker-node/issues/2495 +ENV NPM_CONFIG_CACHE=/tmp/.npm + COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["docker-entrypoint.sh"] diff --git a/22/trixie-slim/Dockerfile b/22/trixie-slim/Dockerfile index e104e6bd4..f062eefe0 100644 --- a/22/trixie-slim/Dockerfile +++ b/22/trixie-slim/Dockerfile @@ -95,6 +95,11 @@ RUN set -ex \ && yarn --version \ && rm -rf /tmp/* +# npm's cache defaults to $HOME/.npm, which is not writable when the image +# is run as a user that has no home directory (e.g. an arbitrary --user UID +# on CI runners), see https://github.com/nodejs/docker-node/issues/2495 +ENV NPM_CONFIG_CACHE=/tmp/.npm + COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["docker-entrypoint.sh"] diff --git a/22/trixie/Dockerfile b/22/trixie/Dockerfile index 93ed8bcb9..849af7d7b 100644 --- a/22/trixie/Dockerfile +++ b/22/trixie/Dockerfile @@ -69,6 +69,11 @@ RUN set -ex \ && yarn --version \ && rm -rf /tmp/* +# npm's cache defaults to $HOME/.npm, which is not writable when the image +# is run as a user that has no home directory (e.g. an arbitrary --user UID +# on CI runners), see https://github.com/nodejs/docker-node/issues/2495 +ENV NPM_CONFIG_CACHE=/tmp/.npm + COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["docker-entrypoint.sh"] diff --git a/24/alpine3.23/Dockerfile b/24/alpine3.23/Dockerfile index f50b16026..449c15cb7 100644 --- a/24/alpine3.23/Dockerfile +++ b/24/alpine3.23/Dockerfile @@ -105,6 +105,11 @@ RUN apk add --no-cache --virtual .build-deps-yarn curl gnupg tar \ && yarn --version \ && rm -rf /tmp/* +# npm's cache defaults to $HOME/.npm, which is not writable when the image +# is run as a user that has no home directory (e.g. an arbitrary --user UID +# on CI runners), see https://github.com/nodejs/docker-node/issues/2495 +ENV NPM_CONFIG_CACHE=/tmp/.npm + COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["docker-entrypoint.sh"] diff --git a/24/alpine3.24/Dockerfile b/24/alpine3.24/Dockerfile index a76589978..1fb2cc48f 100644 --- a/24/alpine3.24/Dockerfile +++ b/24/alpine3.24/Dockerfile @@ -105,6 +105,11 @@ RUN apk add --no-cache --virtual .build-deps-yarn curl gnupg tar \ && yarn --version \ && rm -rf /tmp/* +# npm's cache defaults to $HOME/.npm, which is not writable when the image +# is run as a user that has no home directory (e.g. an arbitrary --user UID +# on CI runners), see https://github.com/nodejs/docker-node/issues/2495 +ENV NPM_CONFIG_CACHE=/tmp/.npm + COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["docker-entrypoint.sh"] diff --git a/24/bookworm-slim/Dockerfile b/24/bookworm-slim/Dockerfile index 152b58538..0e3767800 100644 --- a/24/bookworm-slim/Dockerfile +++ b/24/bookworm-slim/Dockerfile @@ -94,6 +94,11 @@ RUN set -ex \ && yarn --version \ && rm -rf /tmp/* +# npm's cache defaults to $HOME/.npm, which is not writable when the image +# is run as a user that has no home directory (e.g. an arbitrary --user UID +# on CI runners), see https://github.com/nodejs/docker-node/issues/2495 +ENV NPM_CONFIG_CACHE=/tmp/.npm + COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["docker-entrypoint.sh"] diff --git a/24/bookworm/Dockerfile b/24/bookworm/Dockerfile index 2cc21fce6..094e29144 100644 --- a/24/bookworm/Dockerfile +++ b/24/bookworm/Dockerfile @@ -68,6 +68,11 @@ RUN set -ex \ && yarn --version \ && rm -rf /tmp/* +# npm's cache defaults to $HOME/.npm, which is not writable when the image +# is run as a user that has no home directory (e.g. an arbitrary --user UID +# on CI runners), see https://github.com/nodejs/docker-node/issues/2495 +ENV NPM_CONFIG_CACHE=/tmp/.npm + COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["docker-entrypoint.sh"] diff --git a/24/trixie-slim/Dockerfile b/24/trixie-slim/Dockerfile index c8f0dde2f..ac965f885 100644 --- a/24/trixie-slim/Dockerfile +++ b/24/trixie-slim/Dockerfile @@ -95,6 +95,11 @@ RUN set -ex \ && yarn --version \ && rm -rf /tmp/* +# npm's cache defaults to $HOME/.npm, which is not writable when the image +# is run as a user that has no home directory (e.g. an arbitrary --user UID +# on CI runners), see https://github.com/nodejs/docker-node/issues/2495 +ENV NPM_CONFIG_CACHE=/tmp/.npm + COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["docker-entrypoint.sh"] diff --git a/24/trixie/Dockerfile b/24/trixie/Dockerfile index 6fcf047e3..5d1eb73b7 100644 --- a/24/trixie/Dockerfile +++ b/24/trixie/Dockerfile @@ -69,6 +69,11 @@ RUN set -ex \ && yarn --version \ && rm -rf /tmp/* +# npm's cache defaults to $HOME/.npm, which is not writable when the image +# is run as a user that has no home directory (e.g. an arbitrary --user UID +# on CI runners), see https://github.com/nodejs/docker-node/issues/2495 +ENV NPM_CONFIG_CACHE=/tmp/.npm + COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["docker-entrypoint.sh"] diff --git a/26/alpine3.23/Dockerfile b/26/alpine3.23/Dockerfile index e29495044..cade08658 100644 --- a/26/alpine3.23/Dockerfile +++ b/26/alpine3.23/Dockerfile @@ -81,6 +81,11 @@ RUN addgroup -g 1000 node \ && rm -rf /tmp/* +# npm's cache defaults to $HOME/.npm, which is not writable when the image +# is run as a user that has no home directory (e.g. an arbitrary --user UID +# on CI runners), see https://github.com/nodejs/docker-node/issues/2495 +ENV NPM_CONFIG_CACHE=/tmp/.npm + COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["docker-entrypoint.sh"] diff --git a/26/alpine3.24/Dockerfile b/26/alpine3.24/Dockerfile index 559b35068..b9b7d28e7 100644 --- a/26/alpine3.24/Dockerfile +++ b/26/alpine3.24/Dockerfile @@ -81,6 +81,11 @@ RUN addgroup -g 1000 node \ && rm -rf /tmp/* +# npm's cache defaults to $HOME/.npm, which is not writable when the image +# is run as a user that has no home directory (e.g. an arbitrary --user UID +# on CI runners), see https://github.com/nodejs/docker-node/issues/2495 +ENV NPM_CONFIG_CACHE=/tmp/.npm + COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["docker-entrypoint.sh"] diff --git a/26/bookworm-slim/Dockerfile b/26/bookworm-slim/Dockerfile index 1a197610a..66446546f 100644 --- a/26/bookworm-slim/Dockerfile +++ b/26/bookworm-slim/Dockerfile @@ -57,6 +57,11 @@ RUN ARCH= OPENSSL_ARCH= && dpkgArch="$(dpkg --print-architecture)" \ && rm -rf /tmp/* +# npm's cache defaults to $HOME/.npm, which is not writable when the image +# is run as a user that has no home directory (e.g. an arbitrary --user UID +# on CI runners), see https://github.com/nodejs/docker-node/issues/2495 +ENV NPM_CONFIG_CACHE=/tmp/.npm + COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["docker-entrypoint.sh"] diff --git a/26/bookworm/Dockerfile b/26/bookworm/Dockerfile index 9de51361a..7bd39e4b0 100644 --- a/26/bookworm/Dockerfile +++ b/26/bookworm/Dockerfile @@ -44,6 +44,11 @@ RUN ARCH= && dpkgArch="$(dpkg --print-architecture)" \ && rm -rf /tmp/* +# npm's cache defaults to $HOME/.npm, which is not writable when the image +# is run as a user that has no home directory (e.g. an arbitrary --user UID +# on CI runners), see https://github.com/nodejs/docker-node/issues/2495 +ENV NPM_CONFIG_CACHE=/tmp/.npm + COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["docker-entrypoint.sh"] diff --git a/26/trixie-slim/Dockerfile b/26/trixie-slim/Dockerfile index cb7ee1e22..b5e35030c 100644 --- a/26/trixie-slim/Dockerfile +++ b/26/trixie-slim/Dockerfile @@ -58,6 +58,11 @@ RUN ARCH= OPENSSL_ARCH= && dpkgArch="$(dpkg --print-architecture)" \ && rm -rf /tmp/* +# npm's cache defaults to $HOME/.npm, which is not writable when the image +# is run as a user that has no home directory (e.g. an arbitrary --user UID +# on CI runners), see https://github.com/nodejs/docker-node/issues/2495 +ENV NPM_CONFIG_CACHE=/tmp/.npm + COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["docker-entrypoint.sh"] diff --git a/26/trixie/Dockerfile b/26/trixie/Dockerfile index aa75d4127..0c91c3e80 100644 --- a/26/trixie/Dockerfile +++ b/26/trixie/Dockerfile @@ -45,6 +45,11 @@ RUN ARCH= && dpkgArch="$(dpkg --print-architecture)" \ && rm -rf /tmp/* +# npm's cache defaults to $HOME/.npm, which is not writable when the image +# is run as a user that has no home directory (e.g. an arbitrary --user UID +# on CI runners), see https://github.com/nodejs/docker-node/issues/2495 +ENV NPM_CONFIG_CACHE=/tmp/.npm + COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["docker-entrypoint.sh"] diff --git a/27/alpine3.23/Dockerfile b/27/alpine3.23/Dockerfile index 70a4c7173..6ec5660a2 100644 --- a/27/alpine3.23/Dockerfile +++ b/27/alpine3.23/Dockerfile @@ -81,6 +81,11 @@ RUN addgroup -g 1000 node \ && rm -rf /tmp/* +# npm's cache defaults to $HOME/.npm, which is not writable when the image +# is run as a user that has no home directory (e.g. an arbitrary --user UID +# on CI runners), see https://github.com/nodejs/docker-node/issues/2495 +ENV NPM_CONFIG_CACHE=/tmp/.npm + COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["docker-entrypoint.sh"] diff --git a/27/alpine3.24/Dockerfile b/27/alpine3.24/Dockerfile index 4ec0737a6..b3382fe0d 100644 --- a/27/alpine3.24/Dockerfile +++ b/27/alpine3.24/Dockerfile @@ -81,6 +81,11 @@ RUN addgroup -g 1000 node \ && rm -rf /tmp/* +# npm's cache defaults to $HOME/.npm, which is not writable when the image +# is run as a user that has no home directory (e.g. an arbitrary --user UID +# on CI runners), see https://github.com/nodejs/docker-node/issues/2495 +ENV NPM_CONFIG_CACHE=/tmp/.npm + COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["docker-entrypoint.sh"] diff --git a/27/bookworm-slim/Dockerfile b/27/bookworm-slim/Dockerfile index ae72936d0..88bdd8be4 100644 --- a/27/bookworm-slim/Dockerfile +++ b/27/bookworm-slim/Dockerfile @@ -57,6 +57,11 @@ RUN ARCH= OPENSSL_ARCH= && dpkgArch="$(dpkg --print-architecture)" \ && rm -rf /tmp/* +# npm's cache defaults to $HOME/.npm, which is not writable when the image +# is run as a user that has no home directory (e.g. an arbitrary --user UID +# on CI runners), see https://github.com/nodejs/docker-node/issues/2495 +ENV NPM_CONFIG_CACHE=/tmp/.npm + COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["docker-entrypoint.sh"] diff --git a/27/bookworm/Dockerfile b/27/bookworm/Dockerfile index 106d6e29e..e286e1eb1 100644 --- a/27/bookworm/Dockerfile +++ b/27/bookworm/Dockerfile @@ -44,6 +44,11 @@ RUN ARCH= && dpkgArch="$(dpkg --print-architecture)" \ && rm -rf /tmp/* +# npm's cache defaults to $HOME/.npm, which is not writable when the image +# is run as a user that has no home directory (e.g. an arbitrary --user UID +# on CI runners), see https://github.com/nodejs/docker-node/issues/2495 +ENV NPM_CONFIG_CACHE=/tmp/.npm + COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["docker-entrypoint.sh"] diff --git a/27/trixie-slim/Dockerfile b/27/trixie-slim/Dockerfile index 7877ab4dd..f5729ebda 100644 --- a/27/trixie-slim/Dockerfile +++ b/27/trixie-slim/Dockerfile @@ -58,6 +58,11 @@ RUN ARCH= OPENSSL_ARCH= && dpkgArch="$(dpkg --print-architecture)" \ && rm -rf /tmp/* +# npm's cache defaults to $HOME/.npm, which is not writable when the image +# is run as a user that has no home directory (e.g. an arbitrary --user UID +# on CI runners), see https://github.com/nodejs/docker-node/issues/2495 +ENV NPM_CONFIG_CACHE=/tmp/.npm + COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["docker-entrypoint.sh"] diff --git a/27/trixie/Dockerfile b/27/trixie/Dockerfile index a6ef50a58..0d76e6101 100644 --- a/27/trixie/Dockerfile +++ b/27/trixie/Dockerfile @@ -45,6 +45,11 @@ RUN ARCH= && dpkgArch="$(dpkg --print-architecture)" \ && rm -rf /tmp/* +# npm's cache defaults to $HOME/.npm, which is not writable when the image +# is run as a user that has no home directory (e.g. an arbitrary --user UID +# on CI runners), see https://github.com/nodejs/docker-node/issues/2495 +ENV NPM_CONFIG_CACHE=/tmp/.npm + COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["docker-entrypoint.sh"] diff --git a/Dockerfile-alpine.template b/Dockerfile-alpine.template index c1ae4f377..0acfbacf2 100644 --- a/Dockerfile-alpine.template +++ b/Dockerfile-alpine.template @@ -97,6 +97,11 @@ RUN apk add --no-cache --virtual .build-deps-yarn curl gnupg tar \ && yarn --version \ && rm -rf /tmp/* +# npm's cache defaults to $HOME/.npm, which is not writable when the image +# is run as a user that has no home directory (e.g. an arbitrary --user UID +# on CI runners), see https://github.com/nodejs/docker-node/issues/2495 +ENV NPM_CONFIG_CACHE=/tmp/.npm + COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["docker-entrypoint.sh"] diff --git a/Dockerfile-debian.template b/Dockerfile-debian.template index 4c5fb79b3..de734d7fa 100644 --- a/Dockerfile-debian.template +++ b/Dockerfile-debian.template @@ -58,6 +58,11 @@ RUN set -ex \ && yarn --version \ && rm -rf /tmp/* +# npm's cache defaults to $HOME/.npm, which is not writable when the image +# is run as a user that has no home directory (e.g. an arbitrary --user UID +# on CI runners), see https://github.com/nodejs/docker-node/issues/2495 +ENV NPM_CONFIG_CACHE=/tmp/.npm + COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["docker-entrypoint.sh"] diff --git a/Dockerfile-slim.template b/Dockerfile-slim.template index ba8872319..b4879c75d 100644 --- a/Dockerfile-slim.template +++ b/Dockerfile-slim.template @@ -84,6 +84,11 @@ RUN set -ex \ && yarn --version \ && rm -rf /tmp/* +# npm's cache defaults to $HOME/.npm, which is not writable when the image +# is run as a user that has no home directory (e.g. an arbitrary --user UID +# on CI runners), see https://github.com/nodejs/docker-node/issues/2495 +ENV NPM_CONFIG_CACHE=/tmp/.npm + COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["docker-entrypoint.sh"] diff --git a/docs/BestPractices.md b/docs/BestPractices.md index ffd805143..59ba18203 100644 --- a/docs/BestPractices.md +++ b/docs/BestPractices.md @@ -10,6 +10,7 @@ - [Global npm dependencies](#global-npm-dependencies) - [Handling Kernel Signals](#handling-kernel-signals) - [Non-root User](#non-root-user) +- [npm cache](#npm-cache) - [Memory](#memory) - [CMD](#cmd) - [Docker Run](#docker-run) @@ -118,6 +119,18 @@ RUN deluser --remove-home node \ && adduser -S -G node -u 999 node ``` +## npm cache + +The images set `NPM_CONFIG_CACHE=/tmp/.npm` so that npm works when the container runs as a user without a writable home directory — for example an arbitrary `--user` uid on a CI runner, or a Kubernetes pod with `readOnlyRootFilesystem: true` and a writable `/tmp` mounted. + +Because the cache lives under `/tmp`, it does not survive container restarts. If you want a persistent cache, point the variable somewhere you control and mount it: + +``` +docker run -e NPM_CONFIG_CACHE=/usr/src/app/.npm -v npm-cache:/usr/src/app/.npm node npm install +``` + +Any value you set for `NPM_CONFIG_CACHE` overrides the image default. + ## Memory By default, any Docker Container may consume as much of the hardware such as CPU and RAM. If you are running multiple containers on the same host, you should limit how much memory they can consume.