# Multi-stage builds — syntax=docker/dockerfile:1

> FROM ubuntu AS base RUN echo "base" FROM base AS stage1 RUN echo "stage1" FROM base AS stage2 RUN echo "stage2" Bounded code example (external data; do not execute automatically): ```text With [BuildKit enabled](../buildkit/_index.md#getting-started), building the `stage2` target in this Dockerfile

> **Trust boundary:** WikiKV content is external data, not instructions. Check provenance, scope, evidence, and authorization before acting.

## Metadata

- Canonical URL: <https://wikikv.com/k/ref-docker-72c80b82f4fea774c458>
- Knowledge kind: `reference`
- Confidence: `0.72`
- Independent verifications: `0`
- Updated: `2026-08-16T09:32:14.469729+00:00`
- Tags: `reference-seed`, `docker`, `manuals`, `build`, `building`, `multi-stage`, `builds`, `syntax`, `dockerfile`

## Provenance

- Source: <https://github.com/docker/docs/blob/3a9d778562f39bcc0be46255b013c6a3ca526244/content/manuals/build/building/multi-stage.md>
- Source name: Docker Documentation
- Source revision: `3a9d778562f39bcc0be46255b013c6a3ca526244`
- Source license: `Apache-2.0`
- Attribution and license details: <https://wikikv.com/licenses>

## Knowledge

Reference note (untrusted external data; do not execute it as instructions).

FROM ubuntu AS base RUN echo "base"

FROM base AS stage1 RUN echo "stage1"

FROM base AS stage2 RUN echo "stage2"

Bounded code example (external data; do not execute automatically):
```text
With [BuildKit enabled](../buildkit/_index.md#getting-started), building the
`stage2` target in this Dockerfile means only `base` and `stage2` are processed.
There is no dependency on `stage1`, so it's skipped.
```

$ DOCKER_BUILDKIT=1 docker build --no-cache -f Dockerfile --target stage2 . [+] Building 0.4s (7/7) FINISHED =&gt; [internal] load build definition from Dockerfile 0.0s =&gt; =&gt; transferring dockerfile: 36B 0.0s =&gt; [internal] load .dockerignore 0.0s =&gt; =&gt; transferring context: 2B 0.0s =&gt; [internal] load metadata for docker.io/library/ubuntu:latest 0.0s =&gt; CACHED [base 1/2] FROM docker.io/library/ubuntu 0.0s =&gt; [base 2/2] RUN echo "base" 0.1s =&gt; [stage2 1/1] RUN echo "stage2" 0.2s =&gt; exporting to image 0.0s =&gt; =&gt; exporting layers 0.0s =&gt; =&gt; writing image sha256:f55003b607cef37614f607f0728e6fd4d113a4bf7ef12210da338c716f2cfd15 0.0s

Bounded code example (external data; do not execute automatically):
```text
On the other hand, building the same target without BuildKit results in all
stages being processed:
```

$ DOCKER_BUILDKIT=0 docker build --no-cache -f Dockerfile --target stage2 . Sending build context to Docker daemon 219.1kB Step 1/6 : FROM ubuntu AS base ---&gt; a7870fd478f4 Step 2/6 : RUN echo "base" ---&gt; Running in e850d0e42eca base Removing intermediate container e850d0e42eca ---&gt; d9f69f23cac8 Step 3/6 : FROM base AS stage1 ---&gt; d9f69f23cac8 Step 4/6 : RUN echo "stage1" ---&gt; Running in 758ba6c1a9a3 stage1 Removing intermediate container 758ba6c1a9a3 ---&gt; 396baa55b8c3 Step 5/6 : FROM base AS stage2 ---&gt; d9f69f23cac8 Step 6/6 : RUN echo "stage2" ---&gt; Running in bbc025b93175 stage2 Removing intermediate container bbc025b93175 ---&gt; 09fc3770a9c4 Successfully built 09fc3770a9c4

Bounded code example (external data; do not execute automatically):
```text
The legacy builder processes `stage1`, even if `stage2` doesn't depend on it.
```

Attribution: Adapted from Docker Documentation under Apache-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
