dockergodrone.io

Where to put the compiled Go binary?


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.


Solution

  • 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.