Skip to content

Commit 0f5a7d4

Browse files
committed
docs: named contexts
Signed-off-by: CrazyMax <[email protected]>
1 parent 6a6e8c7 commit 0f5a7d4

File tree

3 files changed

+100
-75
lines changed

3 files changed

+100
-75
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ ___
2222
* [Secrets](docs/advanced/secrets.md)
2323
* [Isolated builders](docs/advanced/isolated-builders.md)
2424
* [Push to multi-registries](docs/advanced/push-multi-registries.md)
25-
* [Copy between registries](docs/advanced/copy-between-registries.md)
25+
* [Copy between registries](docs/advanced/copy-between-registries.md)
2626
* [Cache](docs/advanced/cache.md)
2727
* [Local registry](docs/advanced/local-registry.md)
2828
* [Export image to Docker](docs/advanced/export-docker.md)
2929
* [Share built image between jobs](docs/advanced/share-image-jobs.md)
3030
* [Test your image before pushing it](docs/advanced/test-before-push.md)
31+
* [Named contexts](docs/advanced/named-contexts.md)
3132
* [Handle tags and labels](docs/advanced/tags-labels.md)
3233
* [Update DockerHub repo description](docs/advanced/dockerhub-desc.md)
3334
* [Customizing](#customizing)
@@ -176,6 +177,7 @@ jobs:
176177
* [Export image to Docker](docs/advanced/export-docker.md)
177178
* [Share built image between jobs](docs/advanced/share-image-jobs.md)
178179
* [Test your image before pushing it](docs/advanced/test-before-push.md)
180+
* [Named contexts](docs/advanced/named-contexts.md)
179181
* [Handle tags and labels](docs/advanced/tags-labels.md)
180182
* [Update DockerHub repo description](docs/advanced/dockerhub-desc.md)
181183

docs/advanced/export-docker.md

+2-74
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Export image to Docker
22

3-
You may want your build result to be available in the Docker client through `docker images` to be able to use it
4-
in another step of your workflow:
3+
You may want your build result to be available in the Docker client through
4+
`docker images` to be able to use it in another step of your workflow:
55

66
```yaml
77
name: ci
@@ -33,75 +33,3 @@ jobs:
3333
run: |
3434
docker image inspect myimage:latest
3535
```
36-
37-
38-
## Usage of the built image in other build steps
39-
40-
[By default](https://github.com/docker/setup-buildx-action#customizing), `docker/setup-buildx-action@v2`
41-
uses `docker-container` as a build driver, so the docker images are not available in the builder container.
42-
To use them, you may use [build contexts](https://github.com/docker/buildx/blob/master/docs/reference/buildx_build.md#-additional-build-contexts---build-context):
43-
44-
```yaml
45-
name: ci
46-
47-
on:
48-
push:
49-
branches:
50-
- 'main'
51-
52-
jobs:
53-
docker:
54-
runs-on: ubuntu-latest
55-
steps:
56-
-
57-
name: Checkout
58-
uses: actions/checkout@v3
59-
-
60-
name: Set up Docker Buildx
61-
uses: docker/setup-buildx-action@v2
62-
-
63-
name: Build base image
64-
uses: docker/build-push-action@v3
65-
with:
66-
context: base
67-
load: true
68-
tags: my-base-image:latest
69-
-
70-
name: Build image from my-base-image:latest
71-
uses: docker/build-push-action@v3
72-
with:
73-
context: .
74-
build-contexts: |
75-
base-image=docker-image://my-base-image:latest
76-
tags: myimage:latest
77-
```
78-
79-
Where `base-image`is the name of the base image (or stage name if specified) in your Dockerfile:
80-
```Dockerfile
81-
FROM base-image
82-
```
83-
84-
### Bake alternative
85-
86-
You may also want to use [bake](https://docs.docker.com/build/bake/build-contexts/#using-a-result-of-one-target-as-a-base-image-in-another-target)
87-
and build the base image and the target image in one build step:
88-
```terraform
89-
# docker-bake.hcl
90-
target "base" {
91-
dockerfile = "baseapp.Dockerfile"
92-
}
93-
94-
target "app" {
95-
contexts = {
96-
baseapp = "target:base"
97-
}
98-
}
99-
```
100-
101-
```yaml
102-
-
103-
name: Build
104-
uses: docker/bake-action@v2
105-
with:
106-
target: app
107-
```

docs/advanced/named-contexts.md

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Named contexts
2+
3+
You can define [additional build contexts](https://docs.docker.com/engine/reference/commandline/buildx_build/#build-context)
4+
that can be accessed in your Dockerfile with `FROM name` or `--from=name`. When
5+
Dockerfile defines a stage with the same name it is overwritten.
6+
7+
This can be useful with GitHub Actions to reuse results from other builds or
8+
pin an image to a spcific tag in your workflow.
9+
10+
## Pin image to a specific tag
11+
12+
Replace `alpine:latest` with a pinned one:
13+
14+
```dockerfile
15+
# syntax=docker/dockerfile:1
16+
FROM alpine
17+
RUN echo "Hello World"
18+
```
19+
20+
```yaml
21+
name: ci
22+
23+
on:
24+
push:
25+
branches:
26+
- 'main'
27+
28+
jobs:
29+
docker:
30+
runs-on: ubuntu-latest
31+
steps:
32+
-
33+
name: Checkout
34+
uses: actions/checkout@v3
35+
-
36+
name: Set up Docker Buildx
37+
uses: docker/setup-buildx-action@v2
38+
-
39+
name: Build
40+
uses: docker/build-push-action@v3
41+
with:
42+
context: .
43+
build-contexts: |
44+
alpine=docker-image://alpine:3.16
45+
tags: myimage:latest
46+
```
47+
48+
## Usage of the built image in other build steps
49+
50+
By default, the [`setup-buildx` action](https://github.com/docker/setup-buildx-action#about)
51+
uses `docker-container` as a build driver, so built Docker images are not
52+
available in the builder container.
53+
54+
With named contexts you can reuse the built image:
55+
56+
```dockerfile
57+
# syntax=docker/dockerfile:1
58+
FROM alpine
59+
RUN echo "Hello World"
60+
```
61+
62+
```yaml
63+
name: ci
64+
65+
on:
66+
push:
67+
branches:
68+
- 'main'
69+
70+
jobs:
71+
docker:
72+
runs-on: ubuntu-latest
73+
steps:
74+
-
75+
name: Checkout
76+
uses: actions/checkout@v3
77+
-
78+
name: Set up Docker Buildx
79+
uses: docker/setup-buildx-action@v2
80+
-
81+
name: Build base image
82+
uses: docker/build-push-action@v3
83+
with:
84+
context: base
85+
load: true
86+
tags: my-base-image:latest
87+
-
88+
name: Build
89+
uses: docker/build-push-action@v3
90+
with:
91+
context: .
92+
build-contexts: |
93+
alpine=docker-image://my-base-image:latest
94+
tags: myimage:latest
95+
```

0 commit comments

Comments
 (0)