|
| 1 | +--- |
| 2 | +title: Build arguments |
| 3 | +description: Introduction to configurable builds, using build args |
| 4 | +keywords: > |
| 5 | + build, buildkit, buildx, guide, tutorial, build arguments, arg |
| 6 | +--- |
| 7 | + |
| 8 | +{% include_relative nav.html selected="5" %} |
| 9 | + |
| 10 | +Build arguments is a great way to add flexibility to your builds. You can pass |
| 11 | +build arguments at build-time, and you can set a default value that the builder |
| 12 | +uses as a fallback. |
| 13 | + |
| 14 | +## Change runtime versions |
| 15 | + |
| 16 | +A practical use case for build arguments is to specify runtime versions for |
| 17 | +build stages. Your image uses the `golang:{{site.example_go_version}}-alpine` |
| 18 | +image as a base image. |
| 19 | +But what if someone wanted to use a different version of Go for building the |
| 20 | +application? They could update the version number inside the Dockerfile, but |
| 21 | +that’s inconvenient, it makes switching between versions more tedious than it |
| 22 | +has to be. Build arguments make life easier: |
| 23 | + |
| 24 | +```diff |
| 25 | + # syntax=docker/dockerfile:1 |
| 26 | +- FROM golang:{{site.example_go_version}}-alpine AS base |
| 27 | ++ ARG GO_VERSION={{site.example_go_version}} |
| 28 | ++ FROM golang:${GO_VERSION}-alpine AS base |
| 29 | + WORKDIR /src |
| 30 | + RUN --mount=type=cache,target=/go/pkg/mod/ \ |
| 31 | + --mount=type=bind,source=go.sum,target=go.sum \ |
| 32 | + --mount=type=bind,source=go.mod,target=go.mod \ |
| 33 | + go mod download -x |
| 34 | + |
| 35 | + FROM base AS build-client |
| 36 | + RUN --mount=type=cache,target=/go/pkg/mod/ \ |
| 37 | + --mount=type=bind,target=. \ |
| 38 | + go build -o /bin/client ./cmd/client |
| 39 | + |
| 40 | + FROM base AS build-server |
| 41 | + RUN --mount=type=cache,target=/go/pkg/mod/ \ |
| 42 | + --mount=type=bind,target=. \ |
| 43 | + go build -o /bin/server ./cmd/server |
| 44 | + |
| 45 | + FROM scratch AS client |
| 46 | + COPY --from=build /bin/client /bin/ |
| 47 | + ENTRYPOINT [ "/bin/client" ] |
| 48 | + |
| 49 | + FROM scratch AS server |
| 50 | + COPY --from=build /bin/server /bin/ |
| 51 | + ENTRYPOINT [ "/bin/server" ] |
| 52 | +``` |
| 53 | + |
| 54 | +The `ARG` keyword is interpolated in the image name in the `FROM` instruction. |
| 55 | +The default value of the `GO_VERSION` build argument is set to `{{site.example_go_version}}`. |
| 56 | +If the build doesn't receive a `GO_VERSION` build argument, the `FROM` instruction |
| 57 | +resolves to `golang:{{site.example_go_version}}-alpine`. |
| 58 | + |
| 59 | +Try setting a different version of Go to use for building, using the |
| 60 | +`--build-arg` flag for the build command: |
| 61 | + |
| 62 | +```console |
| 63 | +$ docker build --build-arg="GO_VERSION=1.19" . |
| 64 | +``` |
| 65 | + |
| 66 | +Running this command results in a build using the `golang:1.19-alpine` image. |
| 67 | + |
| 68 | +## Inject values |
| 69 | + |
| 70 | +You can also make use of build arguments to modify values in the source code of |
| 71 | +your program, at build time. This is useful for dynamically injecting |
| 72 | +information, avoiding hard-coded values. With Go, consuming external values at |
| 73 | +build time is done using linker flags, or `-ldflags`. |
| 74 | + |
| 75 | +The server part of the application contains a conditional statement to print the |
| 76 | +app version, if a version is specified: |
| 77 | + |
| 78 | +```go |
| 79 | +// cmd/server/main.go |
| 80 | +var version string |
| 81 | + |
| 82 | +func main() { |
| 83 | + if version != "" { |
| 84 | + log.Printf("Version: %s", version) |
| 85 | + } |
| 86 | +``` |
| 87 | +
|
| 88 | +You could declare the version string value directly in the code. But, updating |
| 89 | +the version to line up with the release version of the application would require |
| 90 | +updating the code ahead of every release. That would be both tedious and |
| 91 | +error-prone. A better solution is to pass the version string as a build |
| 92 | +argument, and inject the build argument into the code. |
| 93 | +
|
| 94 | +The following example adds an `APP_VERSION` build argument to the `build-server` |
| 95 | +stage. The Go compiler uses the value of the build argument to set the value of |
| 96 | +a variable in the code. |
| 97 | +
|
| 98 | +```diff |
| 99 | + # syntax=docker/dockerfile:1 |
| 100 | + ARG GO_VERSION={{site.example_go_version}} |
| 101 | + FROM golang:${GO_VERSION}-alpine AS base |
| 102 | + WORKDIR /src |
| 103 | + RUN --mount=type=cache,target=/go/pkg/mod/ \ |
| 104 | + --mount=type=bind,source=go.sum,target=go.sum \ |
| 105 | + --mount=type=bind,source=go.mod,target=go.mod \ |
| 106 | + go mod download -x |
| 107 | + |
| 108 | + FROM base AS build-client |
| 109 | + RUN --mount=type=cache,target=/go/pkg/mod/ \ |
| 110 | + --mount=type=bind,target=. \ |
| 111 | + go build -o /bin/client ./cmd/client |
| 112 | + |
| 113 | + FROM base AS build-server |
| 114 | ++ ARG APP_VERSION="v0.0.0+unknown" |
| 115 | + RUN --mount=type=cache,target=/go/pkg/mod/ \ |
| 116 | + --mount=type=bind,target=. \ |
| 117 | +- go build -o /bin/server ./cmd/server |
| 118 | ++ go build -ldflags "-X main.version=$APP_VERSION" -o /bin/server ./cmd/server |
| 119 | + |
| 120 | + FROM scratch AS client |
| 121 | + COPY --from=build-client /bin/client /bin/ |
| 122 | + ENTRYPOINT [ "/bin/client" ] |
| 123 | + |
| 124 | + FROM scratch AS server |
| 125 | + COPY --from=build-server /bin/server /bin/ |
| 126 | + ENTRYPOINT [ "/bin/server" ] |
| 127 | +``` |
| 128 | +
|
| 129 | +Now the version of the server when building the binary, without having to update |
| 130 | +the source code. To verify this, you can the `server` target and start a |
| 131 | +container with `docker run`. The server outputs `v0.0.1` as the version on |
| 132 | +startup. |
| 133 | +
|
| 134 | +```console |
| 135 | +$ docker build --target=server --build-arg="APP_VERSION=v0.0.1" --tag=buildme-server . |
| 136 | +$ docker run buildme-server |
| 137 | +2023/04/06 08:54:27 Version: v0.0.1 |
| 138 | +2023/04/06 08:54:27 Starting server... |
| 139 | +2023/04/06 08:54:27 Listening on HTTP port 3000 |
| 140 | +``` |
| 141 | +
|
| 142 | +## Summary |
| 143 | +
|
| 144 | +This section showed how you can use build arguments to make builds more |
| 145 | +configurable, and inject values at build-time. |
| 146 | +
|
| 147 | +Related information: |
| 148 | +
|
| 149 | +- [`ARG` Dockerfile reference](../../engine/reference/builder.md#arg) |
| 150 | +
|
| 151 | +## Next steps |
| 152 | +
|
| 153 | +The next section of this guide shows how you can use Docker builds to create not |
| 154 | +only container images, but executable binaries as well. |
| 155 | +
|
| 156 | +[Export binaries](export.md){: .button .primary-btn } |
0 commit comments