gitee.com/mirrors_opencollective/goreleaser@v0.45.0/docs/130-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. 12 13 If you have only one `build` setup, 14 the configuration is as easy as adding the 15 name of your image to your `.goreleaser.yml` file: 16 17 ```yaml 18 dockers: 19 - image: user/repo 20 ``` 21 22 You also need to create a `Dockerfile` in your project's root folder: 23 24 ```dockerfile 25 FROM scratch 26 COPY mybin / 27 ENTRYPOINT ["/mybin"] 28 ``` 29 30 This configuration will build and push a Docker image named `user/repo:tagname`. 31 32 ## Customization 33 34 Of course, you can customize a lot of things: 35 36 ```yaml 37 # .goreleaser.yml 38 dockers: 39 # You can have multiple Docker images. 40 - 41 # GOOS of the built binary that should be used. 42 goos: linux 43 # GOARCH of the built binary that should be used. 44 goarch: amd64 45 # GOARM of the built binary that should be used. 46 goarm: '' 47 # Name of the built binary that should be used. 48 binary: mybinary 49 # Docker image name. 50 image: myuser/myimage 51 # Path to the Dockerfile (from the project root). 52 dockerfile: Dockerfile 53 # Template of the docker tag. Defaults to `{{ .Version }}`. Other allowed 54 # fields are `.Tag`, `.Major`, `.Minor` and `.Patch` and 55 # `.Env.VARIABLE_NAME`. 56 tag_templates: 57 - "{{ .Tag }}" 58 - "{{ .Tag }}-{{ .Env.GO_VERSION }}" 59 - "v{{ .Major }}" 60 - latest 61 # If your Dockerfile copies files other than the binary itself, 62 # you should list them here as well. 63 extra_files: 64 - config.yml 65 ``` 66 67 These settings should allow you to generate multiple Docker images, 68 for example, using multiple `FROM` statements, 69 as well as generate one image for each binary in your project. 70 71 ## Passing environment variables to tag_template 72 73 You can do that by using `{{ .Env.VARIABLE_NAME }}` in the template, for 74 example: 75 76 ```yaml 77 dockers: 78 - 79 tag_template: "{{ .Tag }}-{{ .Env.GOVERSION_NR }}" 80 ``` 81 82 Then you can run: 83 84 ```console 85 GOVERSION_NR=$(go version | awk '{print $3}') goreleaser 86 ``` 87 88 ## Keeping docker images updated for current major 89 90 Some users might want to when version to push docker tags `:v1`, `:v1.6`, 91 `:v1.6.4` and `:latest` when `v1.6.4` (for example) is built. That can be 92 accomplished by using multiple `tag_templates`: 93 94 ```yaml 95 # .goreleaser.yml 96 dockers: 97 - 98 binary: mybinary 99 image: myuser/myimage 100 tag_templates: 101 - "{{ .Tag }}" 102 - "v{{ .Major }}" 103 - "v{{ .Major }}.{{ .Minor }}" 104 - latest 105 ``` 106 107 This will build and publish the following images: 108 109 * myuser/myimage:v1.6.4 110 * myuser/myimage:v1 111 * myuser/myimage:v1.6 112 * myuser/myimage:latest 113 114 Hope this feature serves you well! 115 116 More info: 117 118 * [#461](https://github.com/goreleaser/goreleaser/issues/461) 119 * [#505](https://github.com/goreleaser/goreleaser/issues/505)