github.com/GoogleContainerTools/skaffold/v2@v2.13.2/codelab/03_buildpacks-runimage-override/tutorial.md (about) 1 # Using custom run image for CNB builder 2 3 ## Introduction 4 5 ### What is CNB? 6 [Cloud Native Buildpacks](https://buildpacks.io/) (CNB) enable building 7 a container image from source code without the need for a Dockerfile. 8 Skaffold supports building with CNB, requiring only 9 a local Docker daemon. 10 11 CNB uses two images when building an application image: 12 - A _run image_ serves as the base image for the resulting application image. 13 - A _build image_ acts as the host for performing the build. 14 15 ### What you'll learn 16 17 - how to annotate an arbitrary image and use it as a CNB 18 [run image](https://buildpacks.io/docs/concepts/components/stack/). 19 - how to integrate the custom run image with a sample Buildpacks application using Skaffold's *artifact dependencies* feature. 20 21 ___ 22 23 **Time to complete**: <walkthrough-tutorial-duration duration=10></walkthrough-tutorial-duration> 24 25 Click the **Start** button to move to the next step. 26 27 ## First steps 28 29 ### Start a Minikube cluster 30 31 We'll use `minikube` as our local kubernetes cluster of choice. 32 33 Run: 34 ```bash 35 minikube start 36 ``` 37 38 ## Write a custom CNB run image 39 40 CNB run and build images require some additional metadata to identify the _Stack ID_ and user/group accounts to be used. A [_stack_](https://buildpacks.io/docs/concepts/components/stack/) is a specification or contract. For example, the `io.buildpacks.stacks.bionic` stack defines that it provides the same packages as installed on Ubuntu 18.04. 41 42 We add a base artifact with a single <walkthrough-editor-open-file filePath="base/Dockerfile">Dockerfile</walkthrough-editor-open-file> that defines the required metadata, and reference this as an <walkthrough-editor-select-line filePath="skaffold.yaml" startLine="4" startCharacterOffset="4" endLine="6" endCharacterOffset="0">artifact</walkthrough-editor-select-line> called `base` in our `skaffold.yaml`. 43 44 Next we'll use this artifact as the run image for a sample Buildpacks app. 45 46 <walkthrough-footnote> 47 We will use the `gcr.io/buildpacks/builder:v1` builder image which supports the Stack ID `google`. So that's what we added to the Dockerfile. 48 </walkthrough-footnote> 49 50 ## Use it in a sample Buildpacks app 51 52 We use a simple <walkthrough-editor-open-file filePath="app/main.go">Go application</walkthrough-editor-open-file> and reference it as an <walkthrough-editor-select-line filePath="skaffold.yaml" startLine="6" startCharacterOffset="4" endLine="8" endCharacterOffset="0">artifact</walkthrough-editor-select-line> called `app` in our `skaffold.yaml`. 53 54 To use the `base` artifact as the custom run image we: 55 - add an <walkthrough-editor-select-line filePath="skaffold.yaml" startLine="13" startCharacterOffset="4" endLine="15" endCharacterOffset="0">artifact dependency</walkthrough-editor-select-line> for `app` artifact on the `base` artifact. 56 - set the <walkthrough-editor-select-line filePath="skaffold.yaml" startLine="10" startCharacterOffset="6" endLine="11" endCharacterOffset="0">runImage</walkthrough-editor-select-line> property of the `app` artifact to be the `base` artifact. 57 58 ## Run it! 59 60 Run this command and Skaffold should take care of building the artifacts in order and deploying the provided <walkthrough-editor-open-file filePath="k8s/web.yaml">manifest</walkthrough-editor-open-file>. 61 62 ```bash 63 skaffold dev --port-forward 64 ``` 65 66 Once the image has been built and deployed click on the <walkthrough-web-preview-icon></walkthrough-web-preview-icon> icon and select `Preview on port 8080`. This should redirect to the running service and show the output: 67 68 ``` 69 Hello, World! 70 ``` 71 72 The Go app reads <walkthrough-editor-open-file filePath="base/hello.txt">hello.txt</walkthrough-editor-open-file> that's provided by the base artifact. Lets change the text from <walkthrough-editor-select-line filePath="base/hello.txt" startLine="0" startCharacterOffset="0" endLine="1" endCharacterOffset="0">Hello, World!</walkthrough-editor-select-line> to `Hello, Buildpacks!`. This should trigger a rebuild of the `base` artifact which in turn triggers a rebuild and redeploy for the `app` artifact. Once that completes click on the <walkthrough-web-preview-icon></walkthrough-web-preview-icon> icon again and select `Preview on port 8080`. This should redirect to the running service and show the output: 73 74 ``` 75 Hello, Buildpacks! 76 ``` 77 78 ## Congratulations 79 80 <walkthrough-conclusion-trophy></walkthrough-conclusion-trophy> 81 82 All done! 83 84 You now know how to use Buildpacks with custom run images and use Skaffold to tie the loop together. 85