This Docker BuildKit error occurs when your Dockerfile has syntax issues preventing proper build stage detection. Most commonly caused by instructions placed before the FROM statement or incorrect multi-stage build references.
The "no build stage in current context" error is a Docker BuildKit error that indicates Docker cannot identify a valid build stage in your Dockerfile. This happens because Docker's build process requires a proper structure where each build stage must begin with a FROM instruction. In Docker, a build stage is defined by a FROM instruction, which initializes a new build stage and sets the base image. According to Docker's documentation, the first non-comment line of your Dockerfile must be the FROM instruction. When Docker encounters instructions before FROM (with the exception of ARG), or when it cannot resolve stage references in multi-stage builds, it throws this error. This error is particularly common when using Docker BuildKit (the modern build engine) with multi-stage builds, as BuildKit is stricter about Dockerfile syntax and build stage validation than the legacy builder.
Check that your Dockerfile starts with a FROM instruction (ignoring comments). The first non-comment line must be FROM.
Incorrect:
ENV SERVER_PORT=8080
FROM python:3.7-alpineCorrect:
FROM python:3.7-alpine
ENV SERVER_PORT=8080If you need to use variables before FROM, use ARG (the only instruction allowed before FROM):
ARG BASE_IMAGE=python:3.7-alpine
FROM ${BASE_IMAGE}
ENV SERVER_PORT=8080Move all instructions (ENV, RUN, LABEL, MAINTAINER, etc.) to after the FROM statement. Only ARG instructions are permitted before FROM.
Check for these common mistakes:
- MAINTAINER instruction before FROM (deprecated anyway, use LABEL instead)
- ENV declarations before FROM
- RUN commands before FROM
- LABEL instructions before FROM
# WRONG
MAINTAINER John Doe <[email protected]>
FROM ubuntu:20.04
# CORRECT
FROM ubuntu:20.04
LABEL maintainer="John Doe <[email protected]>"If using multi-stage builds, ensure each stage is properly named and referenced:
Check stage naming:
# Build Stage - use AS to name the stage
FROM maven:3.8.1-jdk-11 AS builder
WORKDIR /build
COPY . .
RUN mvn package
# Final Stage
FROM openjdk:11-jre
# Reference must match exactly
COPY --from=builder /build/target/app.jar /app.jar
CMD ["java", "-jar", "/app.jar"]Common mistakes:
- Typo in stage name: COPY --from=build when stage is named builder
- Missing AS clause: FROM maven:3.8.1-jdk-11 without a name
- Case sensitivity: COPY --from=Builder vs AS builder
If you're building a minimal image without a base image, you must explicitly declare this:
FROM scratch
COPY myapp /
CMD ["/myapp"]Don't omit the FROM instruction entirely, even for source images.
Hidden or non-printable characters can cause parsing errors. Try these steps:
1. View raw file contents:
cat -A DockerfileLook for ^M (Windows line endings) or other unusual characters.
2. Convert line endings if needed:
dos2unix Dockerfile
# or
sed -i 's/\r$//' Dockerfile3. Recreate the Dockerfile:
If suspicious, copy the content to a new file using a plain text editor to eliminate hidden characters.
Ensure your Dockerfile is in the build context and not excluded:
1. Run build from correct directory:
# If Dockerfile is in project root
cd /path/to/project
docker build -t myapp .
# If Dockerfile is elsewhere
docker build -t myapp -f path/to/Dockerfile .2. Check .dockerignore file:
cat .dockerignoreMake sure these patterns are NOT in .dockerignore:
- Dockerfile
- Dockerfile*
- * (excluding everything)
If the error appeared after enabling BuildKit, test with the legacy builder to isolate the issue:
# Disable BuildKit for one build
DOCKER_BUILDKIT=0 docker build -t myapp .If this works, the issue is BuildKit-specific. BuildKit is stricter about Dockerfile syntax. Review your Dockerfile for:
- Proper stage naming in multi-stage builds
- Correct instruction ordering
- Valid syntax according to Dockerfile reference
Note: While the legacy builder may work, it's better to fix the Dockerfile for BuildKit compatibility as the legacy builder is deprecated.
BuildKit vs Legacy Builder:
Docker BuildKit is the modern build engine that became the default in Docker 23.0. It's more strict about Dockerfile syntax but offers better performance, improved caching, and additional features like cache mounts and secret mounts. The legacy builder may accept some syntax errors that BuildKit rejects.
Multi-stage Build Debugging:
You can use the docker build --target=<stage> flag to build only up to a specific stage, which helps debug multi-stage build issues:
# Build only the first stage named "builder"
docker build --target=builder -t myapp:builder .Platform-specific Considerations:
When building multi-platform images, stage references must be consistent across all platforms. Use docker buildx for cross-platform builds:
docker buildx build --platform linux/amd64,linux/arm64 -t myapp .Debugging with BuildKit Output:
Enable BuildKit progress output for more detailed error information:
BUILDKIT_PROGRESS=plain docker build -t myapp .This provides line-by-line execution details that can help pinpoint exactly where the build stage resolution fails.
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