The Error Message
Few things stall a CI/CD pipeline faster than a cryptic Docker build error. You might see a failure like this in your terminal or logs:
failed to solve: rpc error: code = Unknown desc = failed to solve with frontend dockerfile.v0: failed to create LLB definition: target stage my-app-stage could not be found
While terms like rpc error and LLB definition sound complex, the problem is usually straightforward. Docker is simply telling you it cannot find the specific build stage you requested with the --target flag. It's looking for a destination that doesn't exist.
Root Causes
The culprit usually hides in one of these three places:
- Naming Mismatches: A simple typo or case-sensitivity mismatch between your
Dockerfileand your build command. - Syntax Errors: Forgetting the
ASkeyword in theFROMinstruction. - Variable Expansion: Empty or null environment variables in CI/CD pipelines (like GitHub Actions) that leave the
--targetflag empty.
Fix 1: Check Case Sensitivity and Typos
Computers are literal. To Docker, build-stage and Build-Stage are two completely different entities. If your Dockerfile defines a stage as builder but your build command calls BUILDER, the build will fail immediately.
Incorrect Dockerfile:
FROM node:20 AS build-stage
# ... instructions
Incorrect Command:
docker build --target build_stage -t my-app .
In this example, a single underscore (_) replaced the hyphen (-). This 1-character difference is enough to break the build. Always double-check that the string following --target matches the name after AS exactly.
Fix 2: Verify the 'AS' Keyword
It is easy to omit the AS keyword when writing Dockerfiles from memory. Without it, Docker treats the second string as part of the image tag or ignores it entirely. This means the stage name never gets registered in the build graph.
Wrong:
FROM golang:1.21 builder
Correct:
FROM golang:1.21 AS builder
Ensure every stage you want to reference has that AS keyword. It's the bridge that links your base image to your custom stage name.
Fix 3: BuildKit and 'frontend dockerfile.v0' Issues
The error failed to solve with frontend dockerfile.v0 appears because BuildKit (Docker's modern build engine) cannot parse the Dockerfile structure. In Docker Desktop 20.10 and later, BuildKit is the default. It's fast, but its error messages can be vague when syntax is broken.
Try disabling BuildKit temporarily. This often forces the legacy builder to give you a more descriptive error message:
DOCKER_BUILDKIT=0 docker build --target my-stage -t my-app .
If the legacy builder works, the issue might involve hidden characters, such as BOM or CRLF line endings, which sometimes confuse BuildKit's parser.
Fix 4: Handling CI/CD Variable Expansion
If this error only happens in GitHub Actions or GitLab CI, check your environment variables. If a variable used for the target name is empty, the shell command might execute like this:
docker build --target -t my-app .
Docker sees an empty string for the target and throws the "stage could not be found" error. To debug this, add an echo command in your CI script to print the variable right before the build step. Ensure your secrets and variables are correctly exported to the runner environment.
Practical Example: A Robust Multi-stage Build
Use this template to create a clean, multi-stage Dockerfile that avoids common pitfalls:
# Stage 1: Build
FROM node:20-alpine AS build_env
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
# Stage 2: Production
FROM nginx:stable-alpine AS runtime_env
COPY --from=build_env /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
To build only the first stage for unit testing, run:
docker build --target build_env -t app-test .
Verification Steps
Confirm the fix by running the build with the --target flag. If successful, you will see BuildKit logging the specific steps for that stage:
# Run the build
docker build --target build_env -t verification-test .
# Check for the resulting image
docker images | grep verification-test
A successful build confirms the stage name was recognized.
Prevention Tips
- Keep names simple: Avoid special characters. Stick to lowercase alphanumeric characters and hyphens.
- Standardize: Use
base,builder, andrunneracross all your projects to minimize confusion. - Use a Linter: Tools like Hadolint catch missing
ASkeywords and syntax errors before you even trigger a build. - Check Line Endings: If you develop on Windows but build for Linux, ensure your IDE is set to
LFinstead ofCRLF. Hidden Windows characters can occasionally break Dockerfile parsing.

