2017-09-13 03:16:23 +02:00
|
|
|
---
|
2017-10-01 18:57:52 +02:00
|
|
|
title: Docker
|
2017-09-13 03:16:23 +02:00
|
|
|
---
|
|
|
|
|
|
|
|
Since [v0.31.0](https://github.com/goreleaser/goreleaser/releases/tag/v0.31.0),
|
2017-10-01 18:57:52 +02:00
|
|
|
GoReleaser supports building and pushing Docker images.
|
2017-09-13 03:16:23 +02:00
|
|
|
|
|
|
|
## How it works
|
|
|
|
|
2017-10-01 18:57:52 +02:00
|
|
|
You can declare multiple Docker images. They will be matched against
|
2017-09-13 03:16:23 +02:00
|
|
|
the binaries generated by your `builds` section.
|
|
|
|
|
2017-10-01 18:57:52 +02:00
|
|
|
If you have only one `build` setup,
|
|
|
|
the configuration is as easy as adding the
|
|
|
|
name of your image to your `.goreleaser.yml` file:
|
2017-09-13 03:16:23 +02:00
|
|
|
|
2020-05-10 23:59:21 +02:00
|
|
|
The docker image declaration supports templating. Learn more about the [name template engine](/customization/templates).
|
2018-10-20 15:26:16 +02:00
|
|
|
|
2017-09-13 03:16:23 +02:00
|
|
|
```yaml
|
|
|
|
dockers:
|
2018-10-20 15:26:16 +02:00
|
|
|
- image_templates:
|
|
|
|
- user/repo
|
2017-09-13 03:16:23 +02:00
|
|
|
```
|
|
|
|
|
2017-10-01 18:57:52 +02:00
|
|
|
You also need to create a `Dockerfile` in your project's root folder:
|
2017-09-13 03:16:23 +02:00
|
|
|
|
|
|
|
```dockerfile
|
|
|
|
FROM scratch
|
|
|
|
COPY mybin /
|
|
|
|
ENTRYPOINT ["/mybin"]
|
|
|
|
```
|
|
|
|
|
2017-10-01 18:57:52 +02:00
|
|
|
This configuration will build and push a Docker image named `user/repo:tagname`.
|
2017-09-13 03:16:23 +02:00
|
|
|
|
2020-05-10 21:57:11 +02:00
|
|
|
!!! info
|
2020-06-10 15:43:38 +02:00
|
|
|
Note that we are not building any go files in the docker
|
2020-05-10 21:57:11 +02:00
|
|
|
build phase, we are merely copying the binary to a `scratch` image and
|
|
|
|
setting up the entrypoint.
|
2018-06-06 15:44:52 +02:00
|
|
|
|
2017-09-13 03:16:23 +02:00
|
|
|
## Customization
|
|
|
|
|
2017-10-01 18:57:52 +02:00
|
|
|
Of course, you can customize a lot of things:
|
2017-09-13 03:16:23 +02:00
|
|
|
|
|
|
|
```yaml
|
|
|
|
# .goreleaser.yml
|
|
|
|
dockers:
|
2017-10-01 18:57:52 +02:00
|
|
|
# You can have multiple Docker images.
|
2017-09-13 03:16:23 +02:00
|
|
|
-
|
2017-10-01 18:57:52 +02:00
|
|
|
# GOOS of the built binary that should be used.
|
2017-09-13 03:16:23 +02:00
|
|
|
goos: linux
|
2019-12-27 16:55:03 +02:00
|
|
|
|
2017-10-01 18:57:52 +02:00
|
|
|
# GOARCH of the built binary that should be used.
|
2017-09-13 03:16:23 +02:00
|
|
|
goarch: amd64
|
2019-12-27 16:55:03 +02:00
|
|
|
|
2017-10-01 18:57:52 +02:00
|
|
|
# GOARM of the built binary that should be used.
|
2017-09-13 03:16:23 +02:00
|
|
|
goarm: ''
|
2019-12-27 16:55:03 +02:00
|
|
|
|
2019-01-17 22:22:35 +02:00
|
|
|
# Name templates of the built binaries that should be used.
|
2019-01-11 20:27:39 +02:00
|
|
|
binaries:
|
|
|
|
- mybinary
|
2019-12-27 16:55:03 +02:00
|
|
|
|
|
|
|
# Build IDs to gather the binaries from.
|
|
|
|
builds:
|
|
|
|
- mybuild
|
|
|
|
|
2018-10-20 15:26:16 +02:00
|
|
|
# Templates of the Docker image names.
|
|
|
|
image_templates:
|
|
|
|
- "myuser/myimage:latest"
|
|
|
|
- "myuser/myimage:{{ .Tag }}"
|
|
|
|
- "myuser/myimage:{{ .Tag }}-{{ .Env.GO_VERSION }}"
|
|
|
|
- "myuser/myimage:v{{ .Major }}"
|
|
|
|
- "gcr.io/myuser/myimage:latest"
|
2019-12-27 16:55:03 +02:00
|
|
|
|
2018-03-24 23:42:21 +02:00
|
|
|
# Skips the docker push. Could be useful if you also do draft releases.
|
2019-03-06 18:17:53 +02:00
|
|
|
# If set to auto, the release will not be pushed to the docker repository
|
|
|
|
# in case there is an indicator for prerelease in the tag e.g. v1.0.0-rc1
|
2018-03-24 23:42:21 +02:00
|
|
|
# Defaults to false.
|
|
|
|
skip_push: false
|
2019-12-27 16:55:03 +02:00
|
|
|
|
2017-10-01 18:57:52 +02:00
|
|
|
# Path to the Dockerfile (from the project root).
|
2018-12-16 15:11:01 +02:00
|
|
|
dockerfile: Dockerfile
|
2019-12-27 16:55:03 +02:00
|
|
|
|
2018-10-03 15:11:40 +02:00
|
|
|
# Template of the docker build flags.
|
|
|
|
build_flag_templates:
|
2020-01-26 17:06:21 +02:00
|
|
|
- "--pull"
|
|
|
|
- "--label=org.opencontainers.image.created={{.Date}}"
|
|
|
|
- "--label=org.opencontainers.image.name={{.ProjectName}}"
|
|
|
|
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
|
|
|
|
- "--label=org.opencontainers.image.version={{.Version}}"
|
2019-07-16 23:10:45 +02:00
|
|
|
- "--build-arg=FOO={{.Env.Bar}}"
|
2019-12-27 16:55:03 +02:00
|
|
|
|
2017-10-01 18:57:52 +02:00
|
|
|
# If your Dockerfile copies files other than the binary itself,
|
2017-09-26 00:10:04 +02:00
|
|
|
# you should list them here as well.
|
2018-12-16 15:11:01 +02:00
|
|
|
# Note that goreleaser will create the same structure inside the temporary
|
|
|
|
# folder, so if you add `foo/bar.json` here, on your Dockerfile you can
|
|
|
|
# `COPY foo/bar.json /whatever.json`.
|
|
|
|
# Also note that the paths here are relative to the folder in which
|
|
|
|
# goreleaser is being run.
|
|
|
|
# This field does not support wildcards, you can add an entire folder here
|
|
|
|
# and use wildcards when you `COPY`/`ADD` in your Dockerfile.
|
2017-09-26 00:10:04 +02:00
|
|
|
extra_files:
|
|
|
|
- config.yml
|
2017-09-13 03:16:23 +02:00
|
|
|
```
|
|
|
|
|
2020-05-10 23:59:21 +02:00
|
|
|
!!! tip
|
|
|
|
Learn more about the [name template engine](/customization/templates).
|
2018-07-09 08:57:46 +02:00
|
|
|
|
2017-10-01 18:57:52 +02:00
|
|
|
These settings should allow you to generate multiple Docker images,
|
|
|
|
for example, using multiple `FROM` statements,
|
|
|
|
as well as generate one image for each binary in your project.
|
2017-12-06 01:13:16 +02:00
|
|
|
|
2018-10-05 18:00:25 +02:00
|
|
|
## Generic Image Names
|
|
|
|
|
|
|
|
Some users might want to keep their image name as generic as possible.
|
|
|
|
That can be accomplished simply by adding template language in the definition:
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
# .goreleaser.yml
|
2018-10-12 05:08:39 +02:00
|
|
|
project: foo
|
2018-10-05 18:00:25 +02:00
|
|
|
dockers:
|
|
|
|
-
|
2019-01-11 20:27:39 +02:00
|
|
|
binaries:
|
|
|
|
- mybinary
|
2018-10-20 15:26:16 +02:00
|
|
|
image_templates:
|
|
|
|
- "myuser/{{.ProjectName}}"
|
2018-10-05 18:00:25 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
This will build and public the following images:
|
|
|
|
|
2018-10-12 05:08:39 +02:00
|
|
|
- `myuser/foo`
|
|
|
|
|
2020-05-10 23:59:21 +02:00
|
|
|
!!! tip
|
|
|
|
Learn more about the [name template engine](/customization/templates).
|
2018-10-05 18:00:25 +02:00
|
|
|
|
2018-01-19 04:05:09 +02:00
|
|
|
## Keeping docker images updated for current major
|
|
|
|
|
|
|
|
Some users might want to when version to push docker tags `:v1`, `:v1.6`,
|
|
|
|
`:v1.6.4` and `:latest` when `v1.6.4` (for example) is built. That can be
|
2018-10-20 15:26:16 +02:00
|
|
|
accomplished by using multiple `image_templates`:
|
2018-01-19 04:05:09 +02:00
|
|
|
|
|
|
|
```yaml
|
|
|
|
# .goreleaser.yml
|
|
|
|
dockers:
|
|
|
|
-
|
2019-01-11 20:27:39 +02:00
|
|
|
binaries:
|
|
|
|
- mybinary
|
2018-10-20 15:26:16 +02:00
|
|
|
image_templates:
|
|
|
|
- "myuser/myimage:{{ .Tag }}"
|
|
|
|
- "myuser/myimage:v{{ .Major }}"
|
|
|
|
- "myuser/myimage:v{{ .Major }}.{{ .Minor }}"
|
|
|
|
- "myuser/myimage:latest"
|
2018-01-19 04:05:09 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
This will build and publish the following images:
|
|
|
|
|
2018-06-06 15:44:52 +02:00
|
|
|
- `myuser/myimage:v1.6.4`
|
|
|
|
- `myuser/myimage:v1`
|
|
|
|
- `myuser/myimage:v1.6`
|
|
|
|
- `myuser/myimage:latest`
|
2018-01-19 04:05:09 +02:00
|
|
|
|
2018-02-19 01:37:59 +02:00
|
|
|
With these settings you can hopefully push several different docker images
|
|
|
|
with multiple tags.
|
2018-10-03 15:11:40 +02:00
|
|
|
|
2020-05-10 23:59:21 +02:00
|
|
|
!!! tip
|
|
|
|
Learn more about the [name template engine](/customization/templates).
|
2018-10-12 05:08:39 +02:00
|
|
|
|
2018-10-20 15:26:16 +02:00
|
|
|
## Publishing to multiple docker registries
|
|
|
|
|
|
|
|
Some users might want to push images to multiple docker registries. That can be
|
|
|
|
accomplished by using multiple `image_templates`:
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
# .goreleaser.yml
|
|
|
|
dockers:
|
|
|
|
-
|
2019-01-11 20:27:39 +02:00
|
|
|
binaries:
|
|
|
|
- mybinary
|
2018-10-20 15:26:16 +02:00
|
|
|
image_templates:
|
|
|
|
- "docker.io/myuser/myimage:{{ .Tag }}"
|
|
|
|
- "docker.io/myuser/myimage:latest"
|
|
|
|
- "gcr.io/myuser/myimage:{{ .Tag }}"
|
|
|
|
- "gcr.io/myuser/myimage:latest"
|
|
|
|
```
|
|
|
|
|
|
|
|
This will build and publish the following images to `docker.io` and `gcr.io`:
|
|
|
|
|
|
|
|
- `myuser/myimage:v1.6.4`
|
|
|
|
- `myuser/myimage:latest`
|
|
|
|
|
2018-10-03 15:11:40 +02:00
|
|
|
## Applying docker build flags
|
|
|
|
|
|
|
|
Build flags can be applied using `build_flag_templates`. The flags must be
|
|
|
|
valid docker build flags.
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
# .goreleaser.yml
|
|
|
|
dockers:
|
|
|
|
-
|
2019-01-11 20:27:39 +02:00
|
|
|
binaries:
|
|
|
|
- mybinary
|
2018-10-20 15:26:16 +02:00
|
|
|
image_templates:
|
2019-01-11 20:27:39 +02:00
|
|
|
- "myuser/myimage"
|
2018-10-03 15:11:40 +02:00
|
|
|
build_flag_templates:
|
2020-01-26 17:06:21 +02:00
|
|
|
- "--pull"
|
|
|
|
- "--label=org.opencontainers.image.created={{.Date}}"
|
|
|
|
- "--label=org.opencontainers.image.name={{.ProjectName}}"
|
|
|
|
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
|
|
|
|
- "--label=org.opencontainers.image.version={{.Version}}"
|
2018-10-03 15:11:40 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
This will execute the following command:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
docker build -t myuser/myimage . \
|
2020-01-26 17:06:21 +02:00
|
|
|
--pull \
|
|
|
|
--label=org.opencontainers.image.created=2020-01-19T15:58:07Z" \
|
|
|
|
--label=org.opencontainers.image.name=mybinary" \
|
|
|
|
--label=org.opencontainers.image.revision=da39a3ee5e6b4b0d3255bfef95601890afd80709" \
|
|
|
|
--label=org.opencontainers.image.version=1.6.4
|
2018-10-03 15:11:40 +02:00
|
|
|
```
|
2018-10-12 05:08:39 +02:00
|
|
|
|
2020-05-10 23:59:21 +02:00
|
|
|
!!! tip
|
|
|
|
Learn more about the [name template engine](/customization/templates).
|