Skip to content

Commit e1a1035

Browse files
authored
Merge pull request #665 from baibaratsky/patch-1
docs: named contexts
2 parents e189a1c + 0f5a7d4 commit e1a1035

File tree

3 files changed

+100
-3
lines changed

3 files changed

+100
-3
lines changed

README.md

Lines changed: 3 additions & 1 deletion
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)
@@ -188,6 +189,7 @@ jobs:
188189
* [Export image to Docker](docs/advanced/export-docker.md)
189190
* [Share built image between jobs](docs/advanced/share-image-jobs.md)
190191
* [Test your image before pushing it](docs/advanced/test-before-push.md)
192+
* [Named contexts](docs/advanced/named-contexts.md)
191193
* [Handle tags and labels](docs/advanced/tags-labels.md)
192194
* [Update DockerHub repo description](docs/advanced/dockerhub-desc.md)
193195

docs/advanced/export-docker.md

Lines changed: 2 additions & 2 deletions
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

docs/advanced/named-contexts.md

Lines changed: 95 additions & 0 deletions
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)