github.com/goreleaser/goreleaser@v1.25.1/www/docs/customization/docker.md (about) 1 # Docker Images 2 3 GoReleaser can build and push Docker images. 4 Let's see how it works. 5 6 ## How it works 7 8 You can declare multiple Docker images. They will be matched against 9 the binaries generated by your `builds` section and packages generated 10 by your `nfpms` section. 11 12 If you have only one `build` setup, 13 the configuration is as easy as adding the 14 name of your image to your `.goreleaser.yaml` file: 15 16 ```yaml 17 dockers: 18 - image_templates: 19 - user/repo 20 ``` 21 22 !!! tip 23 24 The `image_templates` attribute supports templating. Learn more about the [name template engine](/customization/templates/). 25 26 You also need to create a `Dockerfile` in your project's root directory: 27 28 ```dockerfile 29 FROM scratch 30 ENTRYPOINT ["/mybin"] 31 COPY mybin / 32 ``` 33 34 This configuration will build and push a Docker image named `user/repo:tagname`. 35 36 !!! warning 37 38 Note that we are not building any go files in the Docker 39 build phase, we are merely copying the binary to a `scratch` image and 40 setting up the `entrypoint`. 41 42 ## Customization 43 44 Of course, you can customize a lot of things: 45 46 ```yaml 47 # .goreleaser.yaml 48 dockers: 49 # You can have multiple Docker images. 50 - # 51 # ID of the image, needed if you want to filter by it later on (e.g. on custom publishers). 52 id: myimg 53 54 # GOOS of the built binaries/packages that should be used. 55 # Default: 'linux' 56 goos: linux 57 58 # GOARCH of the built binaries/packages that should be used. 59 # Default: 'amd64' 60 goarch: amd64 61 62 # GOARM of the built binaries/packages that should be used. 63 # Default: '6' 64 goarm: "" 65 66 # GOAMD64 of the built binaries/packages that should be used. 67 # Default: 'v1' 68 goamd64: "v2" 69 70 # IDs to filter the binaries/packages. 71 # 72 # Make sure to only include the IDs of binaries you want to `COPY` in your 73 # Dockerfile. 74 # 75 # If you include IDs that don't exist or are not available for the current 76 # architecture being built, the build of the image will be skipped. 77 ids: 78 - mybuild 79 - mynfpm 80 81 # Templates of the Docker image names. 82 # 83 # Templates: allowed 84 image_templates: 85 - "myuser/myimage:latest" 86 - "myuser/myimage:{{ .Tag }}" 87 - "myuser/myimage:{{ .Tag }}-{{ .Env.FOOBAR }}" 88 - "myuser/myimage:v{{ .Major }}" 89 - "gcr.io/myuser/myimage:latest" 90 91 # Skips the docker build. 92 # Could be useful if you want to skip building the windows docker image on 93 # linux, for example. 94 # 95 # This option is only available on GoReleaser Pro. 96 # Since: v1.14 (pro) 97 # Templates: allowed 98 skip_build: false 99 100 # Skips the docker push. 101 # Could be useful if you also do draft releases. 102 # 103 # If set to auto, the release will not be pushed to the Docker repository 104 # in case there is an indicator of a prerelease in the tag, e.g. v1.0.0-rc1. 105 # 106 # Templates: allowed (since v1.19) 107 skip_push: false 108 109 # Path to the Dockerfile (from the project root). 110 # 111 # Default: 'Dockerfile' 112 # Templates: allowed 113 dockerfile: "{{ .Env.DOCKERFILE }}" 114 115 # Use this instead of `dockerfile` if the contents of your Dockerfile are 116 # supposed to go through the template engine as well. 117 # 118 # `dockerfile` is ignored when this is set. 119 # 120 # This feature is only available in GoReleaser Pro. 121 # Since: v1.20 (pro) 122 # Templates: allowed 123 templated_dockerfile: "{{.Env.DOCKERFILE }}" 124 125 # Set the "backend" for the Docker pipe. 126 # 127 # Valid options are: docker, buildx, podman. 128 # 129 # Podman is a GoReleaser Pro feature and is only available on Linux. 130 # 131 # Default: 'docker' 132 use: docker 133 134 # Docker build flags. 135 # 136 # Templates: allowed 137 build_flag_templates: 138 - "--pull" 139 - "--label=org.opencontainers.image.created={{.Date}}" 140 - "--label=org.opencontainers.image.title={{.ProjectName}}" 141 - "--label=org.opencontainers.image.revision={{.FullCommit}}" 142 - "--label=org.opencontainers.image.version={{.Version}}" 143 - "--build-arg=FOO={{.Env.Bar}}" 144 - "--platform=linux/arm64" 145 146 # Extra flags to be passed down to the push command. 147 push_flags: 148 - --tls-verify=false 149 150 # If your Dockerfile copies files other than binaries and packages, 151 # you should list them here as well. 152 # Note that GoReleaser will create the same structure inside a temporary 153 # directory, so if you add `foo/bar.json` here, on your Dockerfile you can 154 # `COPY foo/bar.json /whatever.json`. 155 # Also note that the paths here are relative to the directory in which 156 # GoReleaser is being run (usually the repository root directory). 157 # This field does not support wildcards, you can add an entire directory here 158 # and use wildcards when you `COPY`/`ADD` in your Dockerfile. 159 extra_files: 160 - config.yml 161 162 # Additional templated extra files to add to the Docker image. 163 # Those files will have their contents pass through the template engine, 164 # and its results will be added to the build context the same way as the 165 # extra_files field above. 166 # 167 # This feature is only available in GoReleaser Pro. 168 # Since: v1.17 (pro) 169 # Templates: allowed 170 templated_extra_files: 171 - src: LICENSE.tpl 172 dst: LICENSE.txt 173 mode: 0644 174 ``` 175 176 !!! warning 177 178 Note that you will have to manually login into the Docker registries you 179 want to push to — GoReleaser does not login by itself. 180 181 !!! tip 182 183 Learn more about the [name template engine](/customization/templates/). 184 185 !!! tip 186 187 You can also create multi-platform images using the [docker_manifests](/customization/docker_manifest/) config. 188 189 These settings should allow you to generate multiple Docker images, 190 for example, using multiple `FROM` statements, 191 as well as generate one image for each binary in your project or one image with multiple binaries, as well as 192 install the generated packages instead of copying the binary and configs manually. 193 194 ## Generic Image Names 195 196 Some users might want to keep their image name as generic as possible. 197 That can be accomplished simply by adding template language in the definition: 198 199 ```yaml 200 # .goreleaser.yaml 201 project_name: foo 202 dockers: 203 - image_templates: 204 - "myuser/{{.ProjectName}}" 205 ``` 206 207 This will build and publish the following images: 208 209 - `myuser/foo` 210 211 !!! tip 212 213 Learn more about the [name template engine](/customization/templates/). 214 215 ## Keeping docker images updated for current major 216 217 Some users might want to push docker tags `:v1`, `:v1.6`, 218 `:v1.6.4` and `:latest` when `v1.6.4` (for example) is built. That can be 219 accomplished by using multiple `image_templates`: 220 221 ```yaml 222 # .goreleaser.yaml 223 dockers: 224 - image_templates: 225 - "myuser/myimage:{{ .Tag }}" 226 - "myuser/myimage:v{{ .Major }}" 227 - "myuser/myimage:v{{ .Major }}.{{ .Minor }}" 228 - "myuser/myimage:latest" 229 ``` 230 231 This will build and publish the following images: 232 233 - `myuser/myimage:v1.6.4` 234 - `myuser/myimage:v1` 235 - `myuser/myimage:v1.6` 236 - `myuser/myimage:latest` 237 238 With these settings you can hopefully push several Docker images 239 with multiple tags. 240 241 !!! tip 242 243 Learn more about the [name template engine](/customization/templates/). 244 245 ## Publishing to multiple docker registries 246 247 Some users might want to push images to multiple docker registries. That can be 248 accomplished by using multiple `image_templates`: 249 250 ```yaml 251 # .goreleaser.yaml 252 dockers: 253 - image_templates: 254 - "docker.io/myuser/myimage:{{ .Tag }}" 255 - "docker.io/myuser/myimage:latest" 256 - "gcr.io/myuser/myimage:{{ .Tag }}" 257 - "gcr.io/myuser/myimage:latest" 258 ``` 259 260 This will build and publish the following images to `docker.io` and `gcr.io`: 261 262 - `myuser/myimage:v1.6.4` 263 - `myuser/myimage:latest` 264 - `gcr.io/myuser/myimage:v1.6.4` 265 - `gcr.io/myuser/myimage:latest` 266 267 ## Applying Docker build flags 268 269 Build flags can be applied using `build_flag_templates`. 270 The flags must be valid Docker build flags. 271 272 ```yaml 273 # .goreleaser.yaml 274 dockers: 275 - image_templates: 276 - "myuser/myimage" 277 build_flag_templates: 278 - "--pull" 279 - "--label=org.opencontainers.image.created={{.Date}}" 280 - "--label=org.opencontainers.image.title={{.ProjectName}}" 281 - "--label=org.opencontainers.image.revision={{.FullCommit}}" 282 - "--label=org.opencontainers.image.version={{.Version}}" 283 ``` 284 285 This will execute the following command: 286 287 ```bash 288 docker build -t myuser/myimage . \ 289 --pull \ 290 --label=org.opencontainers.image.created=2020-01-19T15:58:07Z \ 291 --label=org.opencontainers.image.title=mybinary \ 292 --label=org.opencontainers.image.revision=da39a3ee5e6b4b0d3255bfef95601890afd80709 \ 293 --label=org.opencontainers.image.version=1.6.4 294 ``` 295 296 !!! tip 297 298 Learn more about the [name template engine](/customization/templates/). 299 300 ## Use a specific builder with Docker buildx 301 302 If `buildx` is enabled, the `default` context builder will be used when building 303 the image. This builder is always available and backed by BuildKit in the 304 Docker engine. If you want to use a different builder, you can specify it using 305 the `build_flag_templates` field: 306 307 ```yaml 308 # .goreleaser.yaml 309 dockers: 310 - image_templates: 311 - "myuser/myimage" 312 use: buildx 313 build_flag_templates: 314 - "--builder=mybuilder" 315 ``` 316 317 !!! tip 318 319 Learn more about the [buildx builder instances](https://docs.docker.com/buildx/working-with-buildx/#work-with-builder-instances). 320 321 ## Podman 322 323 !!! success "GoReleaser Pro" 324 325 The podman backend is a [GoReleaser Pro feature](/pro/). 326 327 You can use [`podman`](https://podman.io) instead of `docker` by setting `use` to `podman` on your config: 328 329 ```yaml 330 # .goreleaser.yaml 331 dockers: 332 - image_templates: 333 - "myuser/myimage" 334 use: podman 335 ``` 336 337 Note that GoReleaser will not install Podman for you, nor change any of its 338 configuration. 339 340 If you want to use it rootless, make sure to follow 341 [this guide](https://github.com/containers/podman/blob/main/docs/tutorials/rootless_tutorial.md).