Local build for Golang

Some application deployments require an executable to be built outside of the Docker build process, and then copied into the conatiner image as a distinct build step.

To include local builds for Golang projects in Velocity, you'll need a Dockerfile that copies the locally compiled binary into the Docker image, and you'll need your local build command handy.

Dockerfile structure

Your Dockerfile will need to include the following:

  • A COPY command that targets the locally compiled binary

FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY ./app .
CMD ["./app"]

Creating your local build run configuration

You'll need to select "No" in response to the question "Does your Dockerfile contain the applicaiton's build command."

Then, you'll need to enter an Application build command, like the one shown below along with an Executable path, as shown below.

Application build command

The application build command for Go projects should include the following:

  • The target OS (the cluster node OS)

  • The target Architecture i.e. amd64 / arm64 (the cluster node Arch)

  • Disable CGO

  • The full path to the resulting binary from the root of your project

  • The full path to the main module from the root of your project

  • Add -gcflags="all=-N -l"

  • Remove -ldflags="-w -s" (only -w and -s) if they exist

Example:

CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -gcflags="all=-N -l" -o path/from/project/root/app ./path/from/project/root/main.go

Path to executable

The path to executable is the path within the container image to the compiled binary. Notice that the following example aligns with the Dockerfile example above -- i.e. the path to run the executable in the container is as follows:

Example:

./app

Last updated