github.com/fitzix/goreleaser@v0.92.0/www/content/docker.md (about) 1 --- 2 title: Docker 3 series: customization 4 hideFromIndex: true 5 weight: 140 6 --- 7 8 Since [v0.31.0](https://github.com/goreleaser/goreleaser/releases/tag/v0.31.0), 9 GoReleaser supports building and pushing Docker images. 10 11 ## How it works 12 13 You can declare multiple Docker images. They will be matched against 14 the binaries generated by your `builds` section. 15 16 If you have only one `build` setup, 17 the configuration is as easy as adding the 18 name of your image to your `.goreleaser.yml` file: 19 20 The docker image declaration supports templating. Learn more about the [name template engine](/templates). 21 22 ```yaml 23 dockers: 24 - image_templates: 25 - user/repo 26 ``` 27 28 You also need to create a `Dockerfile` in your project's root folder: 29 30 ```dockerfile 31 FROM scratch 32 COPY mybin / 33 ENTRYPOINT ["/mybin"] 34 ``` 35 36 This configuration will build and push a Docker image named `user/repo:tagname`. 37 38 > **Attention**: Note that were 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 binary that should be used. 52 goos: linux 53 # GOARCH of the built binary that should be used. 54 goarch: amd64 55 # GOARM of the built binary that should be used. 56 goarm: '' 57 # Name of the built binary that should be used. 58 binary: mybinary 59 # Templates of the Docker image names. 60 image_templates: 61 - "myuser/myimage:latest" 62 - "myuser/myimage:{{ .Tag }}" 63 - "myuser/myimage:{{ .Tag }}-{{ .Env.GO_VERSION }}" 64 - "myuser/myimage:v{{ .Major }}" 65 - "gcr.io/myuser/myimage:latest" 66 # Skips the docker push. Could be useful if you also do draft releases. 67 # Defaults to false. 68 skip_push: false 69 # Path to the Dockerfile (from the project root). 70 dockerfile: Dockerfile 71 # Template of the docker build flags. 72 build_flag_templates: 73 - "--label=org.label-schema.schema-version=1.0" 74 - "--label=org.label-schema.version={{.Version}}" 75 - "--label=org.label-schema.name={{.ProjectName}}" 76 - "--build-arg=FOO={{.ENV.Bar}}" 77 # If your Dockerfile copies files other than the binary itself, 78 # you should list them here as well. 79 extra_files: 80 - config.yml 81 ``` 82 83 > Learn more about the [name template engine](/templates). 84 85 These settings should allow you to generate multiple Docker images, 86 for example, using multiple `FROM` statements, 87 as well as generate one image for each binary in your project. 88 89 ## Generic Image Names 90 91 Some users might want to keep their image name as generic as possible. 92 That can be accomplished simply by adding template language in the definition: 93 94 ```yaml 95 # .goreleaser.yml 96 project: foo 97 dockers: 98 - 99 binary: mybinary 100 image_templates: 101 - "myuser/{{.ProjectName}}" 102 ``` 103 104 This will build and public the following images: 105 106 - `myuser/foo` 107 108 > Learn more about the [name template engine](/templates). 109 110 ## Keeping docker images updated for current major 111 112 Some users might want to when version to push docker tags `:v1`, `:v1.6`, 113 `:v1.6.4` and `:latest` when `v1.6.4` (for example) is built. That can be 114 accomplished by using multiple `image_templates`: 115 116 ```yaml 117 # .goreleaser.yml 118 dockers: 119 - 120 binary: mybinary 121 image_templates: 122 - "myuser/myimage:{{ .Tag }}" 123 - "myuser/myimage:v{{ .Major }}" 124 - "myuser/myimage:v{{ .Major }}.{{ .Minor }}" 125 - "myuser/myimage:latest" 126 ``` 127 128 This will build and publish the following images: 129 130 - `myuser/myimage:v1.6.4` 131 - `myuser/myimage:v1` 132 - `myuser/myimage:v1.6` 133 - `myuser/myimage:latest` 134 135 With these settings you can hopefully push several different docker images 136 with multiple tags. 137 138 > Learn more about the [name template engine](/templates). 139 140 ## Publishing to multiple docker registries 141 142 Some users might want to push images to multiple docker registries. That can be 143 accomplished by using multiple `image_templates`: 144 145 ```yaml 146 # .goreleaser.yml 147 dockers: 148 - 149 binary: mybinary 150 image_templates: 151 - "docker.io/myuser/myimage:{{ .Tag }}" 152 - "docker.io/myuser/myimage:latest" 153 - "gcr.io/myuser/myimage:{{ .Tag }}" 154 - "gcr.io/myuser/myimage:latest" 155 ``` 156 157 This will build and publish the following images to `docker.io` and `gcr.io`: 158 159 - `myuser/myimage:v1.6.4` 160 - `myuser/myimage:latest` 161 162 163 ## Applying docker build flags 164 165 Build flags can be applied using `build_flag_templates`. The flags must be 166 valid docker build flags. 167 168 ```yaml 169 # .goreleaser.yml 170 dockers: 171 - 172 binary: mybinary 173 image_templates: 174 - "myuser/myimage" 175 build_flag_templates: 176 - "--label=org.label-schema.schema-version=1.0" 177 - "--label=org.label-schema.version={{.Version}}" 178 - "--label=org.label-schema.name={{.ProjectName}}" 179 ``` 180 181 This will execute the following command: 182 183 ```bash 184 docker build -t myuser/myimage . \ 185 --label=org.label-schema.schema-version=1.0 \ 186 --label=org.label-schema.version=1.6.4 \ 187 --label=org.label-schema.name=mybinary" 188 ``` 189 190 > Learn more about the [name template engine](/templates).