Skip to content

Commit 77adc63

Browse files
authored
Merge pull request cds-hooks#581 from buildpacks/runext/demo
Demo run image extension
2 parents 50c1d29 + 082ee11 commit 77adc63

File tree

9 files changed

+193
-79
lines changed

9 files changed

+193
-79
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
- name: Install pack
2727
uses: buildpacks/github-actions/[email protected]
2828
with:
29-
pack-version: '0.30.0-rc1'
29+
pack-version: '0.30.0-rc1' # FIXME: update to 0.30.0 when available
3030
- name: Test
3131
run: make test
3232
env:

content/docs/extension-guide/create-extension/_index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ This is a step-by-step tutorial for creating and using CNB image extensions.
2424
- [See a build that requires base image extension in order to succeed](/docs/extension-guide/create-extension/why-dockerfiles)
2525
- [Building blocks of an extension](/docs/extension-guide/create-extension/building-blocks-extension)
2626
- [Generating a build.Dockerfile for your application](/docs/extension-guide/create-extension/build-dockerfile)
27-
- [Generating a run.Dockerfile for your application](/docs/extension-guide/create-extension/run-dockerfile)
27+
- [Generating a run.Dockerfile for your application](/docs/extension-guide/create-extension/run-dockerfile-switch)
2828

2929
<!--+if false+-->
3030
---

content/docs/extension-guide/create-extension/build-dockerfile.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ aliases = [
88

99
<!-- test:suite=dockerfiles;weight=4 -->
1010

11+
Builder images can be kept lean if image extensions are used to dynamically install the needed dependencies
12+
for the current application.
13+
1114
### Examine `vim` extension
1215

1316
#### detect
@@ -28,7 +31,9 @@ cat $PWD/samples/extensions/vim/bin/generate
2831

2932
The extension generates a `build.Dockerfile` that installs `vim` on the builder image.
3033

31-
### Re-build the application image
34+
### Configure the `hello-extensions` buildpack to require `vim`
35+
36+
Set the `BP_REQUIRES` build-time environment variable to configure the `hello-extensions` buildpack to require `vim` (review the `./bin/detect` script to see why this works).
3237

3338
<!-- test:exec -->
3439
```
@@ -47,6 +52,7 @@ Note that `--network host` is necessary when publishing to a local registry.
4752
You should see:
4853

4954
```
55+
...
5056
[detector] ======== Results ========
5157
[detector] pass: samples/[email protected]
5258
[detector] pass: samples/[email protected]
@@ -55,12 +61,12 @@ You should see:
5561
[detector] samples/hello-extensions 0.0.1
5662
[detector] Running generate for extension samples/[email protected]
5763
...
58-
[extender] Found build Dockerfile for extension 'samples/vim'
59-
[extender] Applying the Dockerfile at /layers/generated/build/samples_vim/Dockerfile...
64+
[extender (build)] Found build Dockerfile for extension 'samples/vim'
65+
[extender (build)] Applying the Dockerfile at /layers/generated/build/samples_vim/Dockerfile...
6066
...
61-
[extender] Running build command
62-
[extender] ---> Hello Extensions Buildpack
63-
[extender] vim v1.8.0 (c) 1996 - 2018 by Steve Baker, Thomas Moore, Francesc Rocher, Florian Sesser, Kyosuke Tokoro
67+
[extender (build)] Running build command
68+
[extender (build)] ---> Hello Extensions Buildpack
69+
[extender (build)] VIM - Vi IMproved 9.0 (2022 Jun 28, compiled May 19 2023 16:28:36)
6470
...
6571
Successfully built image hello-extensions
6672
```
@@ -85,5 +91,4 @@ Let's take a look at how the `samples/curl` extension fixes the error by switchi
8591
<!--+ if false+-->
8692
---
8793

88-
<a href="/docs/extension-guide/create-extension/run-dockerfile" class="button bg-pink">Next Step</a>
89-
<!--+ end +-->
94+
<a href="/docs/extension-guide/create-extension/run-dockerfile-switch" class="button bg-pink">Next Step</a>

content/docs/extension-guide/create-extension/building-blocks-extension.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ aliases = [
1212

1313
<!-- test:exec -->
1414
```bash
15-
vim --help
15+
tree $PWD/samples/extensions/vim
1616
```
1717

18-
(That's right, we're using the very tool we will later be installing!) You should see something akin to the following:
18+
You should see something akin to the following:
1919

2020
```
2121
.
@@ -38,11 +38,6 @@ vim --help
3838
* Only a limited set of Dockerfile instructions is supported - consult
3939
the [spec](https://github.com/buildpacks/spec/blob/main/image_extension.md)
4040
for further details.
41-
* In the [initial implementation](/docs/features/dockerfiles#phased-approach), `run.Dockerfile` instructions are
42-
limited to a single `FROM` instruction (effectively, it is only possible to switch the run-time base image to a
43-
pre-created image i.e., no dynamic image modification is allowed). Consult
44-
the [spec](https://github.com/buildpacks/spec/blob/main/image_extension.md)
45-
for further details.
4641

4742
We'll take a closer look at the executables for the `vim` extension in the next step.
4843

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
+++
2+
title="Generating a run.Dockerfile that extends the runtime base image"
3+
weight=406
4+
+++
5+
6+
<!-- test:suite=dockerfiles;weight=6 -->
7+
8+
Run images can be kept lean if image extensions are used to dynamically install the needed dependencies
9+
for the current application.
10+
11+
### Examine `cowsay` extension
12+
13+
#### detect
14+
15+
<!-- test:exec -->
16+
```bash
17+
cat $PWD/samples/extensions/cowsay/bin/detect
18+
```
19+
20+
The extension always detects (because its exit code is `0`) and provides a dependency called `cowsay`.
21+
22+
#### generate
23+
24+
<!-- test:exec -->
25+
```bash
26+
cat $PWD/samples/extensions/cowsay/bin/generate
27+
```
28+
29+
The extension generates a `run.Dockerfile` that installs `cowsay` on the current run image.
30+
31+
### Configure the `hello-extensions` buildpack to require `cowsay`
32+
33+
Set the `BP_REQUIRES` build-time environment variable to configure the `hello-extensions` buildpack to require both `vim` and `curl` (review the `./bin/detect` script to see why this works).
34+
35+
<!-- test:exec -->
36+
```bash
37+
pack build hello-extensions \
38+
--builder localhost:5000/extensions-builder \
39+
--env BP_EXT_DEMO=1 \
40+
--env BP_REQUIRES=vim,curl,cowsay \
41+
--path $PWD/samples/apps/java-maven \
42+
--pull-policy always \
43+
--network host \
44+
--verbose
45+
```
46+
47+
Note that `--network host` is necessary when publishing to a local registry.
48+
49+
You should see:
50+
51+
```
52+
...
53+
[detector] ======== Results ========
54+
[detector] pass: samples/[email protected]
55+
[detector] pass: samples/[email protected]
56+
[detector] pass: samples/[email protected]
57+
[detector] pass: samples/[email protected]
58+
[detector] Resolving plan... (try #1)
59+
[detector] samples/vim 0.0.1
60+
[detector] samples/curl 0.0.1
61+
[detector] samples/cowsay 0.0.1
62+
[detector] samples/hello-extensions 0.0.1
63+
[detector] Running generate for extension samples/[email protected]
64+
...
65+
[detector] Running generate for extension samples/[email protected]
66+
...
67+
[detector] Running generate for extension samples/[email protected]
68+
...
69+
[detector] Found a run.Dockerfile from extension 'samples/curl' setting run image to 'localhost:5000/run-image-curl'
70+
...
71+
[extender (build)] Found build Dockerfile for extension 'samples/vim'
72+
[extender (build)] Applying Dockerfile at /layers/generated/build/samples_vim/Dockerfile...
73+
[extender (run)] Found run Dockerfile for extension 'samples/curl'
74+
[extender (run)] Found run Dockerfile for extension 'samples/cowsay'
75+
[extender (run)] Applying Dockerfile at /layers/generated/run/samples_curl/Dockerfile...
76+
...
77+
[extender (run)] Applying Dockerfile at /layers/generated/run/samples_cowsay/Dockerfile
78+
...
79+
[extender (build)] Running build command
80+
[extender (build)] ---> Hello Extensions Buildpack
81+
[extender (build)] VIM - Vi IMproved 9.0 (2022 Jun 28, compiled May 19 2023 16:28:36)
82+
...
83+
Successfully built image hello-extensions
84+
```
85+
86+
Note: build image extension and run image extension are done in parallel,
87+
so the log lines for those phases may print in a different order from that shown above.
88+
89+
### See the image run successfully
90+
91+
<!-- test:exec -->
92+
```bash
93+
docker run --rm --entrypoint cowsay hello-extensions
94+
```
95+
96+
You should see something akin to:
97+
98+
```
99+
________
100+
< MOOOO! >
101+
--------
102+
\ ^__^
103+
\ (oo)\_______
104+
(__)\ )\/\
105+
||----w |
106+
|| ||
107+
```

content/docs/extension-guide/create-extension/run-dockerfile.md renamed to content/docs/extension-guide/create-extension/run-dockerfile-switch.md

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
+++
2-
title="Generating a run.Dockerfile"
2+
title="Generating a run.Dockerfile that switches the runtime base image"
33
weight=405
44
aliases = [
55
"/docs/extension-author-guide/run-dockerfile/"
@@ -8,6 +8,10 @@ aliases = [
88

99
<!-- test:suite=dockerfiles;weight=5 -->
1010

11+
Platforms can have several run images available, each tailored to a specific language family - thus limiting the number
12+
of installed dependencies for each image to the minimum necessary to support the targeted language. Image extensions
13+
can be used to switch the run image to that most appropriate for the current application.
14+
1115
### Examine `curl` extension
1216

1317
#### detect
@@ -26,7 +30,7 @@ The extension always detects (because its exit code is `0`) and provides a depen
2630
cat $PWD/samples/extensions/curl/bin/generate
2731
```
2832

29-
The extension generates a `run.Dockerfile` that switches the run image to reference `run-image-curl`.
33+
The extension generates a `run.Dockerfile` that switches the run image to reference `localhost:5000/run-image-curl`.
3034

3135
### Build a run image for `curl` extension to use
3236

@@ -46,9 +50,13 @@ Build the run image:
4650
docker build \
4751
--file $PWD/samples/stacks/alpine/run/curl.Dockerfile \
4852
--tag localhost:5000/run-image-curl .
53+
54+
docker push localhost:5000/run-image-curl
4955
```
5056

51-
### Re-build the application image
57+
### Configure the `hello-extensions` buildpack to require `curl`
58+
59+
Set the `BP_REQUIRES` build-time environment variable to configure the `hello-extensions` buildpack to require both `vim` and `curl` (review the `./bin/detect` script to see why this works).
5260

5361
<!-- test:exec -->
5462
```bash
@@ -70,8 +78,11 @@ You should see:
7078
[detector] ======== Results ========
7179
[detector] pass: samples/[email protected]
7280
[detector] pass: samples/[email protected]
81+
[detector] pass: samples/[email protected]
7382
[detector] pass: samples/[email protected]
7483
[detector] Resolving plan... (try #1)
84+
[detector] skip: samples/[email protected] provides unused cowsay
85+
[detector] 3 of 4 buildpacks participating
7586
[detector] samples/vim 0.0.1
7687
[detector] samples/curl 0.0.1
7788
[detector] samples/hello-extensions 0.0.1
@@ -80,14 +91,14 @@ You should see:
8091
[detector] Running generate for extension samples/[email protected]
8192
...
8293
[detector] Checking for new run image
83-
[detector] Found a run.Dockerfile configuring image 'run-image-curl' from extension with id 'samples/curl'
94+
[detector] Found a run.Dockerfile from extension 'samples/curl' setting run image to 'localhost:5000/run-image-curl'
8495
...
85-
[extender] Found build Dockerfile for extension 'samples/vim'
86-
[extender] Applying the Dockerfile at /layers/generated/build/samples_vim/Dockerfile...
96+
[extender (build)] Found build Dockerfile for extension 'samples/vim'
97+
[extender (build)] Applying the Dockerfile at /layers/generated/build/samples_vim/Dockerfile...
8798
...
88-
[extender] Running build command
89-
[extender] ---> Hello Extensions Buildpack
90-
[extender] vim v1.8.0 (c) 1996 - 2018 by Steve Baker, Thomas Moore, Francesc Rocher, Florian Sesser, Kyosuke Tokoro
99+
[extender (build)] Running build command
100+
[extender (build)] ---> Hello Extensions Buildpack
101+
[extender (build)] VIM - Vi IMproved 9.0 (2022 Jun 28, compiled May 19 2023 16:28:36)
91102
...
92103
Successfully built image hello-extensions
93104
```
@@ -108,20 +119,31 @@ curl 7.85.0-DEV (x86_64-pc-linux-musl) ... more stuff here ...
108119
What happened: now that `hello-extensions` requires both `vim` and `curl` in its build plan, both extensions are
109120
included in the build and provide the needed dependencies for build and launch, respectively
110121
* The `vim` extension installs `vim` at build time, as before
111-
* The `curl` extension switches the run image to `run-image-curl`, which has `curl` installed
122+
* The `curl` extension switches the run image to `localhost:5000/run-image-curl`, which has `curl` installed
112123

113124
Now our `curl` process can succeed!
114125

115-
## What's next?
126+
### Next steps
116127

117-
The `vim` and `curl` examples are very simple, but we can unlock powerful new features with this functionality.
128+
Our `curl` process succeeded, but there is another process type defined on our image:
118129

119-
Platforms could have several run images available, each tailored to a specific language family, thus limiting the number
120-
of installed dependencies for each image to the minimum necessary to support the targeted language. Image extensions
121-
could be used to switch the run image to that most appropriate for the current application.
130+
```
131+
docker run --rm --entrypoint cowsay hello-extensions
132+
```
133+
134+
You should see:
135+
136+
```
137+
ERROR: failed to launch: path lookup: exec: "cowsay": executable file not found in $PATH
138+
```
139+
140+
Our run image, `localhost:5000/run-image-curl`, has `curl` installed, but it doesn't have `cowsay`.
141+
142+
In general, we may not always have a preconfigured run image available with all the needed dependencies for the current application.
143+
Luckily, we can also use image extensions to dynamically install runtime dependencies at build time. Let's look at that next.
122144

123-
Similarly, builder images could be kept lean if image extensions are used to dynamically install the needed dependencies
124-
for each application.
145+
<!--+ if false+-->
146+
---
125147

126-
In the future, both run image switching and run image modification will be supported, opening the door to other use
127-
cases. Consult the [RFC](https://github.com/buildpacks/rfcs/pull/173) for further information.
148+
<a href="/docs/extension-guide/create-extension/run-dockerfile-extend" class="button bg-pink">Next Step</a>
149+
<!--+ end +-->

content/docs/extension-guide/create-extension/setup-local-environment.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ cd <your preferred workspace directory>
5555
pack version
5656
```
5757

58-
The version should be at least `0.28.0`
58+
The version should be at least `0.30.0`
5959

6060
### Update pack configuration
6161

content/docs/extension-guide/create-extension/why-dockerfiles.md

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ Let's see a build that requires base image extension in order to succeed.
1919
cat $PWD/samples/buildpacks/hello-extensions/bin/detect
2020
```
2121

22-
The buildpack always detects (because its exit code is `0`) but doesn't require any dependencies (as the output build plan is empty).
22+
The buildpack opts-out of the build (exits with non-zero code) unless the `BP_EXT_DEMO` environment variable is set.
23+
24+
If the `BP_EXT_DEMO` environment variable is set, the buildpack detects (exits with code `0`), but doesn't require any dependencies through a build plan unless the `BP_REQUIRES` environment variable is set.
2325

2426
#### build
2527

@@ -28,7 +30,7 @@ The buildpack always detects (because its exit code is `0`) but doesn't require
2830
cat $PWD/samples/buildpacks/hello-extensions/bin/build
2931
```
3032

31-
The buildpack tries to use `tree` at build-time, and defines a launch process called `curl` that runs `curl --version` at runtime.
33+
The buildpack tries to use `vim` at build-time, and defines a launch process called `curl` that runs `curl --version` at runtime.
3234

3335
### Create a builder with extensions and publish it
3436

@@ -72,28 +74,33 @@ Note that `--network host` is necessary when publishing to a local registry.
7274
You should see:
7375

7476
```
77+
...
7578
[detector] ======== Results ========
76-
[detector] pass: samples/[email protected]
79+
[detector] pass: samples/[email protected]
80+
[detector] pass: samples/[email protected]
81+
[detector] pass: samples/[email protected]
7782
[detector] pass: samples/[email protected]
7883
[detector] Resolving plan... (try #1)
79-
[detector] skip: samples/[email protected] provides unused tree
80-
[detector] 1 of 2 buildpacks participating
84+
[detector] skip: samples/[email protected] provides unused vim
85+
[detector] skip: samples/[email protected] provides unused curl
86+
[detector] skip: samples/[email protected] provides unused cowsay
87+
[detector] 1 of 4 buildpacks participating
8188
[detector] samples/hello-extensions 0.0.1
8289
...
83-
[extender] Running build command
84-
[extender] ---> Hello Extensions Buildpack
85-
[extender] /cnb/buildpacks/samples_hello-extensions/0.0.1/bin/build: line 6: tree: command not found
86-
[extender] ERROR: failed to build: exit status 127
90+
[extender (build)] Running build command
91+
[extender (build)] ---> Hello Extensions Buildpack
92+
[extender (build)] /cnb/buildpacks/samples_hello-extensions/0.0.1/bin/build: line 6: vim: command not found
93+
[extender (build)] ERROR: failed to build: exit status 127
8794
```
8895

89-
What happened: our builder doesn't have `tree` installed, so the `hello-extensions` buildpack failed to build (as it
90-
tries to run `tree --version` in its `./bin/build` script).
96+
What happened: our builder doesn't have `vim` installed, so the `hello-extensions` buildpack failed to build (as it
97+
tries to run `vim --version` in its `./bin/build` script).
9198

92-
Even though there is a `samples/tree` extension that passed detection (`pass: samples/tree@0.0.1`), because
93-
the `hello-extensions` buildpack didn't require `tree` in the build plan, the extension was omitted from the detected
94-
group (`skip: samples/tree@0.0.1 provides unused tree`).
99+
Even though there is a `samples/vim` extension that passed detection (`pass: samples/vim@0.0.1`), because
100+
the `hello-extensions` buildpack didn't require `vim` in the build plan, the extension was omitted from the detected
101+
group (`skip: samples/vim@0.0.1 provides unused vim`).
95102

96-
Let's take a look at how the `samples/tree` extension installs `tree` on the builder image...
103+
Let's take a look at how the `samples/vim` extension installs `vim` on the builder image...
97104

98105
<!--+ if false+-->
99106
---

0 commit comments

Comments
 (0)