Docker build stops with circular dependency detected on stage
Your error
ERROR: failed to solve: circular dependency detected on stage: builder1 more line it can showFix it fast
Most common cause, 1 of 2Two stages copy files from each other
Make every arrow point one way.
# syntax=docker/dockerfile:1
FROM alpine AS shared
COPY config/ /config/
FROM busybox AS target
COPY --from=shared /config /out/config
FROM alpine AS build
COPY --from=shared /config /etc/appShort answer
failed to solve: circular dependency detected on stage means BuildKit followed COPY --from and RUN --mount from= references between stages and looped back to the start. Two stages copy from each other, or a stage names itself, often by sharing a name with an image. Rename the stage or move shared files into a third stage (Docker 29, BuildKit 0.35).
Which cause do you have?
Print every stage-to-stage reference in the Dockerfile as an arrow, with names lowercased the way BuildKit compares them. A line where the left and right names are equal is cause 2. Two lines that point at each other, or a chain that loops back, is cause 1.
awk 'toupper($1)=="FROM"{s="(unnamed)"; for(i=2;i<=NF;i++) if(toupper($i)=="AS") s=tolower($(i+1))} match($0,/[Ff][Rr][Oo][Mm]=[^ ,]+/){print NR": "s" -> "tolower(substr($0,RSTART+5,RLENGTH-5))}' DockerfileThen find the line you see in this list and open its fix:
- 1Two stages copy files from each other
BuildKit prints two Dockerfile snippets, each marking a COPY --from line in a different stage, and each line names the other stage - 2A stage refers to its own name
The listing shows a line such as composer -> composer: the stage is named after the image it copies from, or a RUN --mount uses from= with the stage's own name
What does the error look like?
ERROR: failed to solve: circular dependency detected on stage: buildererror: failed to solve: circular dependency detected on stage: target
Cause 1 of 2
Two stages copy files from each other
You seeBuildKit prints two Dockerfile snippets, each marking a COPY --from line in a different stage, and each line names the other stage
Check
The circular dependency error appears when stage A needs files from stage B while B also needs files from A, directly or through a longer chain. BuildKit cannot finish either stage first, so it refuses the whole graph. Current versions print the Dockerfile lines that form the loop above the error, which gives you both ends of it.
grep -niE "^FROM | from=" DockerfileFix
Make every arrow point one way. Pull the files both stages need into a separate earlier stage, and let A and B copy from that stage instead of from each other.
# syntax=docker/dockerfile:1
FROM alpine AS shared
COPY config/ /config/
FROM busybox AS target
COPY --from=shared /config /out/config
FROM alpine AS build
COPY --from=shared /config /etc/appVerify
The awk listing now shows no pair of stages pointing at each other, and the build gets past the load stage.
docker build -t app:test .Confirmed by github.com
Cause 2 of 2
A stage refers to its own name
You seeThe listing shows a line such as composer -> composer: the stage is named after the image it copies from, or a RUN --mount uses from= with the stage's own name
Check
BuildKit resolves a --from or from= value against stage names before treating it as an image, and the lookup ignores case. A stage written as FROM composer:2 AS composer that then runs COPY --from=composer therefore depends on itself, which the cycle check reports as circular. The Dockerfile reference lists --from as accepting an image, a stage or a named context, so the clash is easy to miss.
grep -niE "^FROM .* AS " DockerfileFix
Give the stage a name no --from in the file uses for an image, or write the image with its tag so it no longer matches any stage name.
# before: the stage depends on itself
# FROM composer:2 AS composer
# COPY --from=composer /usr/bin/composer /usr/bin/composer
FROM php:8.4-cli AS app-deps
COPY --from=composer:2 /usr/bin/composer /usr/bin/composerVerify
No arrow in the listing has the same name on both sides, and the image builds.
docker build -t app:test .Confirmed by raw.githubusercontent.com
How do I stop it coming back?
- Name stages after their role (deps, build, runtime) and never after an image you also pull with --from.
- Draw stage references as a one-way chain in review: earlier stages feed later ones, never the reverse.
- Run the awk listing in CI next to the image build so a looping reference fails with a readable line number.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: TaxiDex, Dispatch Pro.