github.com/joselitofilho/goreleaser@v0.155.1-0.20210123221854-e4891856c593/www/docs/customization/docker.md (about) 1 --- 2 title: Docker 3 --- 4 5 Since [v0.31.0](https://github.com/goreleaser/goreleaser/releases/tag/v0.31.0), 6 GoReleaser supports building and pushing Docker images. 7 8 ## How it works 9 10 You can declare multiple Docker images. They will be matched against 11 the binaries generated by your `builds` section and packages generated 12 by your `nfpms` section. 13 14 If you have only one `build` setup, 15 the configuration is as easy as adding the 16 name of your image to your `.goreleaser.yml` file: 17 18 ```yaml 19 dockers: 20 - image_templates: 21 - user/repo 22 ``` 23 24 !!! tip 25 The `image_templates` attribute supports templating. Learn more about the [name template engine](/customization/templates/). 26 27 You also need to create a `Dockerfile` in your project's root folder: 28 29 ```dockerfile 30 FROM scratch 31 ENTRYPOINT ["/mybin"] 32 COPY mybin / 33 ``` 34 35 This configuration will build and push a Docker image named `user/repo:tagname`. 36 37 !!! warning 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.yml 48 dockers: 49 # You can have multiple Docker images. 50 - 51 # GOOS of the built binaries/packages that should be used. 52 goos: linux 53 54 # GOARCH of the built binaries/packages that should be used. 55 goarch: amd64 56 57 # GOARM of the built binaries/packages that should be used. 58 goarm: '' 59 60 # IDs to filter the binaries/packages. 61 ids: 62 - mybuild 63 - mynfpm 64 65 # Templates of the Docker image names. 66 image_templates: 67 - "myuser/myimage:latest" 68 - "myuser/myimage:{{ .Tag }}" 69 - "myuser/myimage:{{ .Tag }}-{{ .Env.GO_VERSION }}" 70 - "myuser/myimage:v{{ .Major }}" 71 - "gcr.io/myuser/myimage:latest" 72 73 # Skips the docker push. Could be useful if you also do draft releases. 74 # If set to auto, the release will not be pushed to the docker repository 75 # in case there is an indicator for prerelease in the tag e.g. v1.0.0-rc1 76 # Defaults to false. 77 skip_push: false 78 79 # Path to the Dockerfile (from the project root). 80 dockerfile: Dockerfile 81 82 # Whether to use `docker buildx build` instead of `docker build`. 83 # You probably want to set it to true when using flags like `--platform`. 84 # If true, will also add `--load` to the build flags. 85 # Defaults to false. 86 use_buildx: true 87 88 # Template of the docker build flags. 89 build_flag_templates: 90 - "--pull" 91 - "--label=org.opencontainers.image.created={{.Date}}" 92 - "--label=org.opencontainers.image.title={{.ProjectName}}" 93 - "--label=org.opencontainers.image.revision={{.FullCommit}}" 94 - "--label=org.opencontainers.image.version={{.Version}}" 95 - "--build-arg=FOO={{.Env.Bar}}" 96 - "--platform=linux/arm64" 97 98 # If your Dockerfile copies files other than binaries and packages, 99 # you should list them here as well. 100 # Note that GoReleaser will create the same structure inside a temporary 101 # folder, so if you add `foo/bar.json` here, on your Dockerfile you can 102 # `COPY foo/bar.json /whatever.json`. 103 # Also note that the paths here are relative to the folder in which 104 # GoReleaser is being run (usually the repository root folder). 105 # This field does not support wildcards, you can add an entire folder here 106 # and use wildcards when you `COPY`/`ADD` in your Dockerfile. 107 extra_files: 108 - config.yml 109 ``` 110 111 !!! tip 112 Learn more about the [name template engine](/customization/templates/). 113 114 !!! tip 115 You can also create multi-platform images using the [docker_manifests](/customization/docker_manifest/) config. 116 117 These settings should allow you to generate multiple Docker images, 118 for example, using multiple `FROM` statements, 119 as well as generate one image for each binary in your project or one image with multiple binaries, as well as 120 install the generated packages instead of copying the binary and configs manually. 121 122 ## Generic Image Names 123 124 Some users might want to keep their image name as generic as possible. 125 That can be accomplished simply by adding template language in the definition: 126 127 ```yaml 128 # .goreleaser.yml 129 project: foo 130 dockers: 131 - 132 image_templates: 133 - "myuser/{{.ProjectName}}" 134 ``` 135 136 This will build and public the following images: 137 138 - `myuser/foo` 139 140 !!! tip 141 Learn more about the [name template engine](/customization/templates/). 142 143 ## Keeping docker images updated for current major 144 145 Some users might want to when version to push docker tags `:v1`, `:v1.6`, 146 `:v1.6.4` and `:latest` when `v1.6.4` (for example) is built. That can be 147 accomplished by using multiple `image_templates`: 148 149 ```yaml 150 # .goreleaser.yml 151 dockers: 152 - 153 image_templates: 154 - "myuser/myimage:{{ .Tag }}" 155 - "myuser/myimage:v{{ .Major }}" 156 - "myuser/myimage:v{{ .Major }}.{{ .Minor }}" 157 - "myuser/myimage:latest" 158 ``` 159 160 This will build and publish the following images: 161 162 - `myuser/myimage:v1.6.4` 163 - `myuser/myimage:v1` 164 - `myuser/myimage:v1.6` 165 - `myuser/myimage:latest` 166 167 With these settings you can hopefully push several Docker images 168 with multiple tags. 169 170 !!! tip 171 Learn more about the [name template engine](/customization/templates/). 172 173 ## Publishing to multiple docker registries 174 175 Some users might want to push images to multiple docker registries. That can be 176 accomplished by using multiple `image_templates`: 177 178 ```yaml 179 # .goreleaser.yml 180 dockers: 181 - 182 image_templates: 183 - "docker.io/myuser/myimage:{{ .Tag }}" 184 - "docker.io/myuser/myimage:latest" 185 - "gcr.io/myuser/myimage:{{ .Tag }}" 186 - "gcr.io/myuser/myimage:latest" 187 ``` 188 189 This will build and publish the following images to `docker.io` and `gcr.io`: 190 191 - `myuser/myimage:v1.6.4` 192 - `myuser/myimage:latest` 193 - `gcr.io/myuser/myimage:v1.6.4` 194 - `gcr.io/myuser/myimage:latest` 195 196 ## Applying docker build flags 197 198 Build flags can be applied using `build_flag_templates`. The flags must be 199 valid docker build flags. 200 201 ```yaml 202 # .goreleaser.yml 203 dockers: 204 - 205 image_templates: 206 - "myuser/myimage" 207 build_flag_templates: 208 - "--pull" 209 - "--label=org.opencontainers.image.created={{.Date}}" 210 - "--label=org.opencontainers.image.title={{.ProjectName}}" 211 - "--label=org.opencontainers.image.revision={{.FullCommit}}" 212 - "--label=org.opencontainers.image.version={{.Version}}" 213 ``` 214 215 This will execute the following command: 216 217 ```bash 218 docker build -t myuser/myimage . \ 219 --pull \ 220 --label=org.opencontainers.image.created=2020-01-19T15:58:07Z \ 221 --label=org.opencontainers.image.title=mybinary \ 222 --label=org.opencontainers.image.revision=da39a3ee5e6b4b0d3255bfef95601890afd80709 \ 223 --label=org.opencontainers.image.version=1.6.4 224 ``` 225 226 !!! tip 227 Learn more about the [name template engine](/customization/templates/).