github.com/droot/goreleaser@v0.66.2-0.20180420030140-c2db5fb17157/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      # Skips the docker push. Could be useful if you also do draft releases.
    52      # Defaults to false.
    53      skip_push: false
    54      # Path to the Dockerfile (from the project root).
    55      dockerfile: Dockerfile
    56      # Template of the docker tag. Defaults to `{{ .Version }}`. Other allowed
    57      # fields are `.Tag`, `.Major`, `.Minor` and `.Patch` and
    58      # `.Env.VARIABLE_NAME`.
    59      tag_templates:
    60      - "{{ .Tag }}"
    61      - "{{ .Tag }}-{{ .Env.GO_VERSION }}"
    62      - "v{{ .Major }}"
    63      - latest
    64      # If your Dockerfile copies files other than the binary itself,
    65      # you should list them here as well.
    66      extra_files:
    67      - config.yml
    68  ```
    69  
    70  These settings should allow you to generate multiple Docker images,
    71  for example, using multiple `FROM` statements,
    72  as well as generate one image for each binary in your project.
    73  
    74  ## Passing environment variables to tag_template
    75  
    76  You can do that by using `{{ .Env.VARIABLE_NAME }}` in the template, for
    77  example:
    78  
    79  ```yaml
    80  dockers:
    81    -
    82      tag_template: "{{ .Tag }}-{{ .Env.GOVERSION_NR }}"
    83  ```
    84  
    85  Then you can run:
    86  
    87  ```console
    88  GOVERSION_NR=$(go version | awk '{print $3}') goreleaser
    89  ```
    90  
    91  ## Keeping docker images updated for current major
    92  
    93  Some users might want to when version to push docker tags `:v1`, `:v1.6`,
    94  `:v1.6.4` and `:latest` when `v1.6.4` (for example) is built. That can be
    95  accomplished by using multiple `tag_templates`:
    96  
    97  ```yaml
    98  # .goreleaser.yml
    99  dockers:
   100    -
   101      binary: mybinary
   102      image: myuser/myimage
   103      tag_templates:
   104      - "{{ .Tag }}"
   105      - "v{{ .Major }}"
   106      - "v{{ .Major }}.{{ .Minor }}"
   107      - latest
   108  ```
   109  
   110  This will build and publish the following images:
   111  
   112  * myuser/myimage:v1.6.4
   113  * myuser/myimage:v1
   114  * myuser/myimage:v1.6
   115  * myuser/myimage:latest
   116  
   117  With these settings you can hopefully push several different docker images
   118  with multiple tags.