@@ -68,6 +68,7 @@ name with `target:` prefix.
6868``` dockerfile {title=baseapp.Dockerfile}
6969FROM scratch
7070```
71+
7172``` dockerfile {title=Dockerfile}
7273# syntax=docker/dockerfile:1
7374FROM baseapp
@@ -89,104 +90,3 @@ target "app" {
8990In most cases you should just use a single multi-stage Dockerfile with multiple
9091targets for similar behavior. This case is only recommended when you have
9192multiple Dockerfiles that can't be easily merged into one.
92-
93- ## Deduplicate context transfer
94-
95- > [ !NOTE]
96- >
97- > As of Buildx version 0.17.0 and later, Bake automatically de-duplicates
98- > context transfer for targets that share the same context. In addition to
99- > Buildx version 0.17.0, the builder must be running BuildKit version 0.16.0 or
100- > later, and the Dockerfile syntax must be ` docker/dockerfile:1.10 ` or later.
101- >
102- > If you meet these requirements, you don't need to manually de-duplicate
103- > context transfer as described in this section.
104- >
105- > - To check your Buildx version, run ` docker buildx version ` .
106- > - To check your BuildKit version, run ` docker buildx inspect --bootstrap ` and
107- > look for the ` BuildKit version ` field.
108- > - To check your Dockerfile syntax version, check the ` syntax `
109- > [ parser directive] ( /reference/dockerfile.md#syntax ) in your Dockerfile. If
110- > it's not present, the default version whatever comes bundled with your
111- > current version of BuildKit. To set the version explicitly, add
112- > ` #syntax=docker/dockerfile:1.10 ` at the top of your Dockerfile.
113-
114- When you build targets concurrently, using groups, build contexts are loaded
115- independently for each target. If the same context is used by multiple targets
116- in a group, that context is transferred once for each time it's used. This can
117- result in significant impact on build time, depending on your build
118- configuration. For example, say you have a Bake file that defines the following
119- group of targets:
120-
121- ``` hcl {title=docker-bake.hcl}
122- group "default" {
123- targets = ["target1", "target2"]
124- }
125-
126- target "target1" {
127- target = "target1"
128- context = "."
129- }
130-
131- target "target2" {
132- target = "target2"
133- context = "."
134- }
135- ```
136-
137- In this case, the context ` . ` is transferred twice when you build the default
138- group: once for ` target1 ` and once for ` target2 ` .
139-
140- If your context is small, and if you are using a local builder, duplicate
141- context transfers may not be a big deal. But if your build context is big, or
142- you have a large number of targets, or you're transferring the context over a
143- network to a remote builder, context transfer becomes a performance bottleneck.
144-
145- To avoid transferring the same context multiple times, you can define a named
146- context that only loads the context files, and have each target that needs
147- those files reference that named context. For example, the following Bake file
148- defines a named target ` ctx ` , which is used by both ` target1 ` and ` target2 ` :
149-
150- ``` hcl {title=docker-bake.hcl}
151- group "default" {
152- targets = ["target1", "target2"]
153- }
154-
155- target "ctx" {
156- context = "."
157- target = "ctx"
158- }
159-
160- target "target1" {
161- target = "target1"
162- contexts = {
163- ctx = "target:ctx"
164- }
165- }
166-
167- target "target2" {
168- target = "target2"
169- contexts = {
170- ctx = "target:ctx"
171- }
172- }
173- ```
174-
175- The named context ` ctx ` represents a Dockerfile stage, which copies the files
176- from its context (` . ` ). Other stages in the Dockerfile can now reference the
177- ` ctx ` named context and, for example, mount its files with ` --mount=from=ctx ` .
178-
179- ``` dockerfile {title=Dockerfile}
180- FROM scratch AS ctx
181- COPY --link . .
182-
183- FROM golang:alpine AS target1
184- WORKDIR /work
185- RUN --mount=from=ctx \
186- go build -o /out/client ./cmd/client \
187-
188- FROM golang:alpine AS target2
189- WORKDIR /work
190- RUN --mount=from=ctx \
191- go build -o /out/server ./cmd/server
192- ```
0 commit comments