This Docker multi-stage build error occurs when the COPY --from or RUN --mount=from flag references a stage name or image that doesn't exist. The fix involves correcting stage name typos, ensuring stages are defined before being referenced, or verifying external image names.
The "--from argument must point to a valid previous stage" error is a Docker BuildKit error that occurs during multi-stage builds when the `COPY --from` or `RUN --mount=from` instruction references a build stage or image that cannot be found. In Docker multi-stage builds, you can copy artifacts from one stage to another using the `--from` flag. The `--from` value must reference either: 1. A named stage defined earlier in the Dockerfile using `FROM ... AS stagename` 2. A numeric index (0-based) referring to a previous FROM instruction 3. An external image name available locally or from a registry When Docker's BuildKit cannot resolve the `--from` reference to any of these valid sources, it fails with this LLB (Low-Level Builder) definition error. This commonly happens due to typos in stage names, incorrect stage ordering, or referencing stages that were removed during Dockerfile refactoring.
The most common cause is a typo or case mismatch. Stage names are case-sensitive. Check that the --from value exactly matches the stage name defined with AS.
Incorrect - case mismatch:
FROM node:18 AS Builder
WORKDIR /app
COPY package*.json ./
RUN npm install && npm run build
FROM nginx:alpine
# ERROR: 'builder' doesn't match 'Builder'
COPY --from=builder /app/dist /usr/share/nginx/htmlCorrect:
FROM node:18 AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install && npm run build
FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/htmlTip: Use lowercase for all stage names to avoid case-related issues.
In Docker multi-stage builds, you can only reference stages that appear earlier in the Dockerfile. The --from instruction cannot reference stages defined later.
Incorrect - referencing a later stage:
FROM nginx:alpine
# ERROR: 'builder' is not defined yet
COPY --from=builder /app/dist /usr/share/nginx/html
FROM node:18 AS builder
WORKDIR /app
RUN npm run buildCorrect - stages in proper order:
FROM node:18 AS builder
WORKDIR /app
COPY . .
RUN npm run build
FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/htmlIf you need to reorganize your Dockerfile, ensure all --from references are updated accordingly.
The AS keyword is required to give a stage a name that can be referenced. Without it, you can only reference stages by their numeric index.
Incorrect - missing AS clause:
FROM golang:1.21
WORKDIR /src
COPY . .
RUN go build -o /app
FROM alpine:latest
# ERROR: no stage named 'build' exists
COPY --from=build /app /appCorrect - with AS clause:
FROM golang:1.21 AS build
WORKDIR /src
COPY . .
RUN go build -o /app
FROM alpine:latest
COPY --from=build /app /appAlternative - using numeric index:
FROM golang:1.21
WORKDIR /src
COPY . .
RUN go build -o /app
FROM alpine:latest
# Using 0 to reference the first stage
COPY --from=0 /app /appNamed stages are preferred as they're more readable and don't break if stages are reordered.
The --from flag can also reference external images. If referencing an external image, verify it exists and the name is correct.
Example referencing external image:
FROM alpine:latest
# Copy nginx config from the official nginx image
COPY --from=nginx:1.25-alpine /etc/nginx/nginx.conf /etc/nginx/nginx.confCommon issues with external images:
- Image name typo: ngnix instead of nginx
- Missing tag: nginx when you need nginx:alpine
- Private image without authentication
- Image not available for your platform architecture
Verify the image exists:
docker pull nginx:1.25-alpineIf the pull fails, the --from reference will also fail.
If using build arguments in --from, ensure they're properly defined and have values.
Incorrect - ARG not in scope:
ARG BASE_STAGE=builder
FROM node:18 AS builder
RUN npm run build
FROM nginx:alpine
# ERROR: ARG goes out of scope after FROM
COPY --from=${BASE_STAGE} /app/dist /usr/share/nginx/htmlCorrect - re-declare ARG after FROM:
ARG BASE_STAGE=builder
FROM node:18 AS builder
RUN npm run build
FROM nginx:alpine
ARG BASE_STAGE
COPY --from=${BASE_STAGE} /app/dist /usr/share/nginx/htmlImportant: ARG values declared before the first FROM are global, but must be re-declared in each stage where they're used (the value is inherited).
Test with explicit value:
docker build --build-arg BASE_STAGE=builder -t myapp .When using --target to build a specific stage, other stages may not be available.
Example Dockerfile:
FROM node:18 AS deps
RUN npm install
FROM node:18 AS builder
COPY --from=deps /app/node_modules ./node_modules
RUN npm run build
FROM node:18-alpine AS runner
COPY --from=builder /app/dist ./distBuilding only the 'deps' stage:
# This works - deps has no --from references
docker build --target deps -t myapp:deps .Building 'builder' stage:
# This works - 'deps' stage exists and is built first
docker build --target builder -t myapp:builder .If you're using --target and getting this error, ensure all stages referenced by your target stage are defined earlier in the Dockerfile.
Enable verbose output to see exactly where the build fails:
docker build --progress=plain -t myapp .This shows detailed output including:
- Which stage is being processed
- The exact instruction that fails
- The --from value that couldn't be resolved
Example error output:
#8 [stage-1 2/2] COPY --from=bilder /app/dist /usr/share/nginx/html
#8 ERROR: failed to solve: failed to create LLB definition: the --from=bilder argument must point to a valid previous stageIn this example, you can see the typo: "bilder" instead of "builder".
List all stages in Dockerfile:
grep -E "^FROM.*AS" DockerfileThis helps you see all defined stage names at a glance.
Understanding LLB (Low-Level Builder):
BuildKit uses LLB as an intermediate representation for builds. The "failed to create LLB definition" message indicates BuildKit couldn't construct the build graph because of invalid stage references. This happens at build planning time, before any layers are created.
RUN --mount=from Syntax:
The --from flag is also used with RUN --mount for cache mounts and bind mounts:
FROM golang:1.21 AS builder
# Mount source from a named stage
RUN --mount=type=bind,from=source,target=/src go build
FROM builder AS source
COPY . /srcThe same rules apply: the from value must reference a valid stage or image.
Multi-platform Builds:
When building for multiple platforms with docker buildx, stage references must be valid across all target platforms:
docker buildx build --platform linux/amd64,linux/arm64 -t myapp .If a referenced external image doesn't support all platforms, the build will fail.
Stage Caching Behavior:
BuildKit aggressively caches stages. If you rename a stage, the cache may still contain references to the old name. Clear the build cache if you experience unexpected behavior:
docker builder pruneBest Practices for Stage Names:
- Use lowercase, descriptive names: builder, deps, test, production
- Avoid generic names like stage1 or temp
- Keep names consistent across related Dockerfiles in your project
- Document stage purposes with comments
image operating system "linux" cannot be used on this platform
How to fix 'image operating system linux cannot be used on this platform' in Docker
manifest unknown: manifest unknown
How to fix 'manifest unknown' in Docker
cannot open '/etc/passwd': Permission denied
How to fix 'cannot open: Permission denied' in Docker
Error response from daemon: failed to create the ipvlan port
How to fix 'failed to create the ipvlan port' in Docker
toomanyrequests: Rate exceeded for anonymous users
How to fix 'Rate exceeded for anonymous users' in Docker Hub