|
1 | 1 | --- |
2 | | -title: Build |
| 2 | +title: Image Building with DevSpace |
3 | 3 | sidebar_label: 1. Build |
4 | 4 | --- |
5 | 5 |
|
6 | 6 | import ConfigPartialBuildConfig from '../_partials/v2beta1/images/group_buildConfig.mdx' |
7 | 7 | import ConfigPartialOverwrites from '../_partials/v2beta1/images/group_overwrites.mdx' |
| 8 | +import ConfigPartialRebuildStrategy from '../_partials/v2beta1/images/rebuildStrategy.mdx' |
8 | 9 | import PreventBuildTip from './_partials/prevent_build.mdx' |
9 | 10 |
|
10 | 11 | DevSpace can build images using a variety of build engines. |
@@ -38,33 +39,200 @@ The example above defines 3 images: |
38 | 39 | - `api` with Dockerfile in `./Dockerfile` and build context in `./` (default values) |
39 | 40 | - `payments` with Dockerfile in `./payments/Dockerfile` and build context in `./payments/` |
40 | 41 | - `auth` with Dockerfile in `./auth/Dockerfile` and build context in `./auth/` |
41 | | - Additionally, the image `auth` also defines a few [`buildArgs`](#buildArgs) and a [build `target`](#target-for-multi-stage-builds) for multi-stage builds. |
| 42 | + Additionally, the image `auth` also defines a few [`buildArgs`](#buildargs) and a [build `target`](#target-for-multi-stage-builds) for multi-stage builds. |
42 | 43 |
|
43 | 44 |
|
44 | | -### 2. Use `build_images` in Pipeline |
45 | | -DevSpace builds images when the [`build_image` function](../functions) is called |
| 45 | +### 2. Call `build_images` in Pipeline |
| 46 | +DevSpace builds images when the [`build_image` function](../functions/index.mdx#build_images) is called within `pipelines` as shown in this example: |
| 47 | +```yaml title=devspace.yaml |
| 48 | +version: v2beta1 |
| 49 | +pipelines: |
| 50 | + # highlight-start |
| 51 | + build: |- |
| 52 | + build_images --all |
| 53 | + build-api: |- |
| 54 | + build_images api |
| 55 | + build-ordered: |- |
| 56 | + build_images auth |
| 57 | + build_images api payments |
| 58 | + # highlight-end |
| 59 | +
|
| 60 | +images: |
| 61 | + api: ... # see example above |
| 62 | + payments: ... # see example above |
| 63 | + auth: ... # see example above |
| 64 | +``` |
| 65 | + |
46 | 66 |
|
47 | 67 | ### 3. Run Pipeline |
| 68 | +Given the example above, you can now run: |
| 69 | +- `devspace build` to build all 3 images in Parallel |
| 70 | +- `devspace run-pipeline build-api` to build only the image named `api` |
| 71 | +- `devspace run-pipeline build-ordered` to |
| 72 | + 1. First build the image `auth` (blocking) |
| 73 | + 2. Then build the images `api` and `payments` in parallel |
48 | 74 |
|
49 | 75 |
|
50 | 76 | ## Build Configuration |
51 | 77 | DevSpace lets you customize the build process for each image using the following config fields: |
52 | 78 |
|
53 | 79 | <ConfigPartialBuildConfig/> |
54 | 80 |
|
| 81 | + |
55 | 82 | ### `buildArgs` |
| 83 | +Your Dockerfile can use the `ARG` instruction to define arguments that will be used in other build instructions as this example shows: |
| 84 | +```Dockerfile title=Dockerfile |
| 85 | +ARG JDK_VERSION=17 |
| 86 | +FROM maven:3-openjdk-${JDK_VERSION}-slim |
| 87 | +``` |
| 88 | +By default, this Dockerfile uses `maven:3-openjdk-7-slim` as base image but you can pass the build arg `JDK_VERSION` to change this dynamically. |
| 89 | + |
| 90 | +There are two options to provide build args in `devspace.yaml`: |
| 91 | + |
| 92 | +<details> |
| 93 | +<summary> |
| 94 | + |
| 95 | +#### 1. Setting `buildArgs` in the `images` section |
| 96 | + |
| 97 | +</summary> |
| 98 | + |
| 99 | +```yaml title=devspace.yaml |
| 100 | +version: v2beta1 |
| 101 | +images: |
| 102 | + auth: |
| 103 | + image: ghcr.io/loft-sh/devspace-example-auth |
| 104 | + # highlight-start |
| 105 | + buildArgs: |
| 106 | + JDK_VERSION: 8 |
| 107 | + ANOTHER_ARG: "some other value" |
| 108 | + # highlight-end |
| 109 | +``` |
| 110 | + |
| 111 | +</details> |
| 112 | + |
| 113 | +<details> |
| 114 | +<summary> |
| 115 | + |
| 116 | +#### 2. Calling `build_images` with the `--set` flag to dynamically add or override the `buildArgs` at runtime |
| 117 | + |
| 118 | +</summary> |
| 119 | + |
| 120 | +```yaml title=devspace.yaml |
| 121 | +version: v2beta1 |
| 122 | +pipelines: |
| 123 | + build: |- |
| 124 | + build_images --set buildArgs.JDK_VERSION=11 --set "buildArgs.SOME_ARG_NAME=some value" |
| 125 | +images: |
| 126 | + auth: |
| 127 | + image: ghcr.io/loft-sh/devspace-example-auth |
| 128 | +``` |
| 129 | + |
| 130 | +</details> |
| 131 | + |
| 132 | + |
56 | 133 |
|
57 | 134 | ### `target` For Multi-Stage Builds |
| 135 | +Some Dockerfiles contain multiple `FROM` instructions that define build stages optionally using the `AS <stage-name>` suffix as shown below: |
| 136 | +```Dockerfile title=Dockerfile |
| 137 | +# highlight-next-line |
| 138 | +FROM golang:1.17-bullseye AS builder |
| 139 | +ADD . /go/src/app |
| 140 | +RUN go build -o /go/bin/app |
| 141 | +
|
| 142 | +# highlight-next-line |
| 143 | +FROM gcr.io/distroless/base-debian11 |
| 144 | +COPY --from=builder /go/bin/app / |
| 145 | +CMD ["/app"] |
| 146 | +``` |
| 147 | +Without defining a build `target`, DevSpace (just like Docker and most other build engines) would build the entire image. However, for developerment, you may not want to use the fully built image because in this case it is a distroless container that only contains the final Go binary but no development tooling, not even `sh` to be able to open a terminal inside the container via `devspace enter`. |
58 | 148 |
|
| 149 | +If you want the image building process to only build the `builder` stage (line 1-4), then you have two options to pass a build `target`: |
59 | 150 |
|
60 | | -## Parallel vs Sequential Builds |
| 151 | +<details> |
| 152 | +<summary> |
61 | 153 |
|
| 154 | +#### 1. Setting `target` in the `images` section |
62 | 155 |
|
63 | | -## Rebuild Strategy |
| 156 | +</summary> |
64 | 157 |
|
| 158 | +```yaml title=devspace.yaml |
| 159 | +version: v2beta1 |
| 160 | +images: |
| 161 | + auth: |
| 162 | + image: ghcr.io/loft-sh/devspace-example-auth |
| 163 | + # highlight-next-line |
| 164 | + target: builder |
| 165 | +``` |
65 | 166 |
|
| 167 | +</details> |
66 | 168 |
|
67 | | -## Dockerfile Overwrites |
| 169 | +<details> |
| 170 | +<summary> |
| 171 | + |
| 172 | +#### 2. Calling `build_images` with the `--set` flag to dynamically add or override the `target` at runtime |
| 173 | + |
| 174 | +</summary> |
| 175 | + |
| 176 | +```yaml title=devspace.yaml |
| 177 | +version: v2beta1 |
| 178 | +pipelines: |
| 179 | + build: |- |
| 180 | + build_images auth --set target=builder |
| 181 | +images: |
| 182 | + auth: |
| 183 | + image: ghcr.io/loft-sh/devspace-example-auth |
| 184 | +``` |
| 185 | + |
| 186 | +</details> |
| 187 | + |
| 188 | + |
| 189 | +### Dockerfile Overwrites |
68 | 190 |
|
69 | 191 |
|
70 | 192 | <ConfigPartialOverwrites/> |
| 193 | + |
| 194 | + |
| 195 | +## Rebuild Strategy |
| 196 | +By default, DevSpace tries to skip building images as much as possible. Once any of your images is built once, DevSpace will only rebuild it if one of the conditions is true: |
| 197 | +- The `dockerfile` has changed |
| 198 | +- Any file within the build `context` folder has changed (while respecting `.dockerignore` rules) |
| 199 | +- The image configuration within the `devspace.yaml` has changed (including values set in the pipeline scripe, e.g. via `--set`) |
| 200 | +- The image was **not** been built before or the `.devspace/` folder has been deleted/manipulated |
| 201 | + |
| 202 | +You can explicitly override this behavior using the `rebuildStrategy` field: |
| 203 | + |
| 204 | +<ConfigPartialRebuildStrategy/> |
| 205 | + |
| 206 | +You can also set the `rebuildStrategy` during runtime using the `--set` flag: |
| 207 | +```yaml title=devspace.yaml |
| 208 | +version: v2beta1 |
| 209 | +pipelines: |
| 210 | + build: |- |
| 211 | + build_images auth --set rebuildStrategy=always |
| 212 | +``` |
| 213 | + |
| 214 | +:::tip `.devspace/` Folder |
| 215 | +DevSpace stores the information about each previously built image inside the `.devspace/` folder within your project. Do **not** commit this folder via `git`. It should always be listed in `.gitignore`. |
| 216 | +::: |
| 217 | + |
| 218 | + |
| 219 | +## `--skip-build` Flag |
| 220 | +If you call `devspace [dev/deploy/build/run-pipeline]` using the `--skip-build` flag, DevSpace will skip any `build_images` instructions defined in your pipeline script. |
| 221 | + |
| 222 | + |
| 223 | +## Parallel vs Sequential Builds |
| 224 | +When calling `build_images --all` or `build_images [image1] [image2]`, DevSpace builds all specified images in parallel. |
| 225 | + |
| 226 | +To execute image building sequentially, call `build_images` multiple times as shown below: |
| 227 | +```yaml title=devspace.yaml |
| 228 | +version: v2beta1 |
| 229 | +pipelines: |
| 230 | + build: |- |
| 231 | + build_images auth |
| 232 | + build_images api payments |
| 233 | +
|
| 234 | +images: |
| 235 | + api: ... # see example above |
| 236 | + payments: ... # see example above |
| 237 | + auth: ... # see example above |
| 238 | +``` |
0 commit comments