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 <noreply@anthropic.com>
Co-Authored-By: Devin Foley <devinfoley@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-11-25 20:01:08 -06:00
1 parent 03d88e4216
commit e1f1252a53
2 files changed
+40 -2

No files matched your search

+1 -1
View File
@@ -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
@@ -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);
}
});