From e1f1252a53c8a1dcf24e7e8092f4c51d423b1102 Mon Sep 17 00:00:00 2001 From: Dotta <34892728+cryppadotta@users.noreply.github.com> Date: Wed, 25 Nov 2026 20:01:08 -0600 Subject: [PATCH] fix(docker): use numeric owner ids for the linked node_modules copy (#23010) COPY --link builds the layer without the destination image's /etc/passwd, so --chown=node:node failed with 'invalid user index -1' and the master image could not build. Use the production stage's USER_UID/USER_GID build args, which the base stage assigns to the node user. A guard test requires numeric owner ids on every linked copy, in any flag order and instruction case, and equal id defaults in the base and production stages. Co-Authored-By: Claude Opus 5.5 Co-Authored-By: Devin Foley --- Dockerfile | 2 +- .../docker-image-layer-reuse.test.ts | 40 ++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index f3b7f04d013..e1dc55eb2bb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -400,7 +400,7 @@ RUN chmod +x /usr/local/bin/docker-entrypoint.sh # the lockfile alone neither re-copies 2.9 GB onto the parent layer nor pushes # or pulls them again. The build stage writes nothing under the root # node_modules, so the rest of /app is copied without it. -COPY --link --chown=node:node --from=deps /app/node_modules /app/node_modules +COPY --link --chown=${USER_UID}:${USER_GID} --from=deps /app/node_modules /app/node_modules COPY --chown=node:node --exclude=node_modules --from=build /app /app # Large provider binaries first, linked so the layer does not depend on the diff --git a/server/src/__tests__/docker-image-layer-reuse.test.ts b/server/src/__tests__/docker-image-layer-reuse.test.ts index e64ae5f0d6b..459c84243ea 100644 --- a/server/src/__tests__/docker-image-layer-reuse.test.ts +++ b/server/src/__tests__/docker-image-layer-reuse.test.ts @@ -25,7 +25,7 @@ function stageBody(stageName: string): string { it("copies installed dependencies as their own linked layer, before the per-commit app files", () => { const production = stageBody("production"); - const dependencies = production.search(/^COPY --link --chown=node:node --from=deps \/app\/node_modules \/app\/node_modules$/m); + const dependencies = production.search(/^COPY --link --chown=\$\{USER_UID\}:\$\{USER_GID\} --from=deps \/app\/node_modules \/app\/node_modules$/m); const app = production.search(/^COPY --chown=node:node --exclude=node_modules --from=build \/app \/app$/m); expect(dependencies, "dependencies must come from the deps stage with --link").toBeGreaterThanOrEqual(0); expect(app, "the app copy must leave out the root node_modules").toBeGreaterThan(dependencies); @@ -54,3 +54,41 @@ it("copies the large provider-pack files as a linked layer with fixed timestamps expect(large, "large files need --link so the layer does not depend on the app layer").toBeGreaterThanOrEqual(0); expect(rest).toBeGreaterThan(large); }); + +function linkedCopyOwners(text: string): string[] { + return [...text.matchAll(/^COPY\b.*$/gim)] + .map((m) => m[0]) + .filter((line) => /\s--link\b/.test(line)) + .flatMap((line) => [...line.matchAll(/\s--chown=(\S+)/g)].map((m) => m[1])); +} + +function argDefault(stageName: string, name: string): string | undefined { + return stageBody(stageName).match(new RegExp(`^ARG ${name}=(\\S+)$`, "m"))?.[1]; +} + +it("finds linked copy owners regardless of flag order or instruction case", () => { + const fixture = [ + "COPY --chown=node:node --link --from=deps /a /a", + "COPY --link --chown=0:0 --from=pack /b /b", + "COPY --chown=node:node --from=build /c /c", + "copy --chown=node:node --link --from=deps /d /d", + "CoPy --link --chown=0:0 --from=pack /e /e", + ].join("\n"); + expect(linkedCopyOwners(fixture)).toEqual(["node:node", "0:0", "node:node", "0:0"]); +}); + +it("gives every linked copy numeric owner ids, because a linked layer cannot look up user names", () => { + const owners = linkedCopyOwners(dockerfile); + expect(owners.length).toBeGreaterThan(0); + for (const owner of owners) { + expect(owner, `COPY --link --chown=${owner} must use numeric ids`).toMatch(/^(\d+|\$\{USER_UID\}):(\d+|\$\{USER_GID\})$/); + } +}); + +it("uses the same owner id defaults in the production stage as the base stage gives the node user", () => { + for (const name of ["USER_UID", "USER_GID"]) { + const base = argDefault("base", name); + expect(base, `the base stage must declare ${name}`).toBeDefined(); + expect(argDefault("production", name), `${name} defaults must match`).toBe(base); + } +});