|
| 1 | ++++ |
| 2 | +title="Generating a run.Dockerfile" |
| 3 | +weight=405 |
| 4 | ++++ |
| 5 | + |
| 6 | +<!-- test:suite=dockerfiles;weight=5 --> |
| 7 | + |
| 8 | +### Examine `curl` extension |
| 9 | + |
| 10 | +#### detect |
| 11 | + |
| 12 | +<!-- test:exec --> |
| 13 | +```bash |
| 14 | +cat $PWD/samples/extensions/curl/bin/detect |
| 15 | +``` |
| 16 | + |
| 17 | +The extension always detects (because its exit code is `0`) and provides a dependency called `curl`. |
| 18 | + |
| 19 | +#### generate |
| 20 | + |
| 21 | +<!-- test:exec --> |
| 22 | +```bash |
| 23 | +cat $PWD/samples/extensions/curl/bin/generate |
| 24 | +``` |
| 25 | + |
| 26 | +The extension generates a `run.Dockerfile` that switches the run image to reference `run-image-curl`. |
| 27 | + |
| 28 | +### Build a run image for `curl` extension to use |
| 29 | + |
| 30 | +<!-- test:exec --> |
| 31 | +```bash |
| 32 | +cat $PWD/samples/stacks/alpine/run/curl.Dockerfile |
| 33 | +``` |
| 34 | + |
| 35 | +This is a simple Dockerfile that creates a CNB run image from the `curl` base image by adding the required CNB user configuration and `io.buildpacks.stack.id` label. |
| 36 | + |
| 37 | +The Dockerfile could come from anywhere, but we include it in the `stacks` directory for convenience. |
| 38 | + |
| 39 | +Build the run image: |
| 40 | + |
| 41 | +<!-- test:exec --> |
| 42 | +```bash |
| 43 | +docker build \ |
| 44 | + --file $PWD/samples/stacks/alpine/run/curl.Dockerfile \ |
| 45 | + --tag run-image-curl . |
| 46 | +``` |
| 47 | + |
| 48 | +### Re-create our builder with `hello-extensions` updated to require `curl` |
| 49 | + |
| 50 | +Edit `$PWD/samples/buildpacks/hello-extensions/bin/detect` to uncomment the second set of lines that output `[[requires]]` to the build plan: |
| 51 | + |
| 52 | +<!-- test:exec --> |
| 53 | +```bash |
| 54 | +sed -i "14,15s/#//" $PWD/samples/buildpacks/hello-extensions/bin/detect |
| 55 | +``` |
| 56 | + |
| 57 | +(On Mac, use `sed -i '' "14,15s/#//" $PWD/samples/buildpacks/hello-extensions/bin/detect`) |
| 58 | + |
| 59 | +Re-create the builder: |
| 60 | + |
| 61 | +<!-- test:exec --> |
| 62 | +```bash |
| 63 | +pack builder create localhost:5000/extensions-builder \ |
| 64 | + --config $PWD/samples/builders/alpine/builder.toml \ |
| 65 | + --publish |
| 66 | +``` |
| 67 | + |
| 68 | +### Re-build the application image |
| 69 | + |
| 70 | +<!-- test:exec --> |
| 71 | +```bash |
| 72 | +pack build hello-extensions \ |
| 73 | + --builder localhost:5000/extensions-builder \ |
| 74 | + --path $PWD/samples/apps/java-maven \ |
| 75 | + --pull-policy always \ |
| 76 | + --network host \ |
| 77 | + --verbose |
| 78 | +``` |
| 79 | + |
| 80 | +Note that `--network host` is necessary when publishing to a local registry. |
| 81 | + |
| 82 | +You should see: |
| 83 | + |
| 84 | +``` |
| 85 | +[detector] ======== Results ======== |
| 86 | +[detector] pass: samples/[email protected] |
| 87 | +[detector] pass: samples/[email protected] |
| 88 | +[detector] pass: samples/[email protected] |
| 89 | +[detector] Resolving plan... (try #1) |
| 90 | +[detector] samples/tree 0.0.1 |
| 91 | +[detector] samples/curl 0.0.1 |
| 92 | +[detector] samples/hello-extensions 0.0.1 |
| 93 | +[detector] Running generate for extension samples/[email protected] |
| 94 | +... |
| 95 | +[detector] Running generate for extension samples/[email protected] |
| 96 | +... |
| 97 | +[detector] Checking for new run image |
| 98 | +[detector] Found a run.Dockerfile configuring image 'run-image-curl' from extension with id 'samples/curl' |
| 99 | +... |
| 100 | +[extender] Found build Dockerfile for extension 'samples/tree' |
| 101 | +[extender] Applying the Dockerfile at /layers/generated/build/samples_tree/Dockerfile... |
| 102 | +... |
| 103 | +[extender] Running build command |
| 104 | +[extender] ---> Hello Extensions Buildpack |
| 105 | +[extender] tree v1.8.0 (c) 1996 - 2018 by Steve Baker, Thomas Moore, Francesc Rocher, Florian Sesser, Kyosuke Tokoro |
| 106 | +... |
| 107 | +Successfully built image hello-extensions |
| 108 | +``` |
| 109 | + |
| 110 | +### See the image run successfully |
| 111 | + |
| 112 | +<!-- test:exec --> |
| 113 | +```bash |
| 114 | +docker run --rm hello-extensions |
| 115 | +``` |
| 116 | + |
| 117 | +You should see something akin to: |
| 118 | + |
| 119 | +``` |
| 120 | +curl 7.85.0-DEV (x86_64-pc-linux-musl) ... more stuff here ... |
| 121 | +``` |
| 122 | + |
| 123 | +What happened: now that `hello-extensions` requires both `tree` and `curl` in its build plan, both extensions are |
| 124 | + included in the build and provide the needed dependencies for build and launch, respectively |
| 125 | +* The `tree` extension installs `tree` at build time, as before |
| 126 | +* The `curl` extension switches the run image to `run-image-curl`, which has `curl` installed |
| 127 | + |
| 128 | +Now our `curl` process can succeed! |
| 129 | + |
| 130 | +## What's next? |
| 131 | + |
| 132 | +The `tree` and `curl` examples are very simple, but we can unlock powerful new features with this functionality. |
| 133 | + |
| 134 | +Platforms could have several run images available, each tailored to a specific language family, thus limiting the number |
| 135 | +of installed dependencies for each image to the minimum necessary to support the targeted language. Image extensions |
| 136 | +could be used to switch the run image to that most appropriate for the current application. |
| 137 | + |
| 138 | +Similarly, builder images could be kept lean if image extensions are used to dynamically install the needed dependencies |
| 139 | +for each application. |
| 140 | + |
| 141 | +In the future, both run image switching and run image modification will be supported, opening the door to other use |
| 142 | +cases. Consult the [RFC](https://github.com/buildpacks/rfcs/pull/173) for further information. |
0 commit comments