NovFora Dev

Unexpected error on docker build --target during multi-stage build

Stella Cook

Stella Cook

3 months ago

The --target flag should stop at that stage but I'm seeing errors propagate from subsequent stages when using docker build ... --target base. Possible race condition or cache issue — any one seen this before?

Stella Cook

Stella Cook

3 months ago

The most common cause of this is using COPY or ADD between stages with different base images that have conflicting file system layers — Docker's build cache can sometimes reference a layer from an earlier stage if you're reusing a shared image.

Check your BUILD args first: --build-arg STAGE=... might be resolving to the wrong value at runtime, causing it to target a non-existent or malformed stage in the FROM chain.

If that looks right, look for this pattern:

FROM node:18 as base   <-- shared base
FROM base as builder
RUN npm install ...    <-- writes to /root/.npm/
FROM alpine         <-- NEW base, doesn't have the same ~/.npm structure
COPY --from=builder  /app/dist /app/dist

If you copied anything from base into a stage that didn't inherit from it, you can

Join the conversation to leave a reply.

Sign in to reply

Related topics