1

I am trying to build my first Dockerfile for a Go application and use DroneCI to build pipeline.

The DroneCI configuration looks as follows:

kind: pipeline
type: docker
name: Build auto git tagger

steps:
  - name: test and build
    image: golang
    commands:
      - go mod download
      - go test ./test
      - go build ./cmd/git-tagger

  - name: Build docker image
    image: plugins/docker
    pull: if-not-exists
    settings:
      username: 
      password: 
      repo: 
      dockerfile: ./build/ci/Dockerfile
      registry: 
      auto_tag: true

trigger:
  branch:
    - master

I have followed the structure convention from https://github.com/golang-standards/project-layout:

enter image description here

The Dockerfile looks as follows so far:

FROM alpine:latest

RUN apk --no-cache add ca-certificates

WORKDIR /root/

The next step is, to copy the GO application binary into the container and here is the question, where to put the compiled binary? At the moment, it puts into the project folder.

softshipper
  • 29,621
  • 43
  • 158
  • 325
  • 4
    It normally doesn't matter. The key is that you know where it is, so you can execute it. – Flimzy Apr 23 '20 at 13:53
  • How can I specify the output folder for binary? – softshipper Apr 23 '20 at 14:01
  • 3
    [set GOBIN, or use the `-o` flag](https://golang.org/cmd/go/#hdr-Compile_and_install_packages_and_dependencies). The binary has to be in the `package` subtree (where Dockerfile is) for [COPY](https://docs.docker.com/engine/reference/builder/#copy) to work. – Peter Apr 23 '20 at 14:30

1 Answers1

1

You can specify output directory and file name wit go build -o flag. For example:

go build ./cmd/git-tagger -o ./build/package/foo

Then edit your Dockerfile:

  1. Load the binary you've got with COPY or ADD

  2. Execute it with ENTRYPOINT or CMD

P.S you specified Dockerfile path as ./build/ci/Dockerfile in your config. But it's in package dir on the screenshot.

Don't forget that repository you linked is just smb's personal opinion and Go doesn't really enforce you to any structure, it depends on your company style standards or on your preferences. So it's not extremely important where to put the binary.

Russiancold
  • 1,051
  • 1
  • 10
  • 25