Migrating from Dockerfile
Dockerfile looks like either of the examples in the official tutorial for writing a Dockerfile to containerize a Go application, you can easily migrate to use
Let's review the best practice multi-stage Dockerfile in that tutorial first:
# syntax=docker/dockerfile:1 ## ## Build ## FROM golang:1.16-buster AS build WORKDIR /app COPY go.mod ./ COPY go.sum ./ RUN go mod download COPY *.go ./ RUN go build -o /docker-gs-ping ## ## Deploy ## FROM gcr.io/distroless/base-debian10 WORKDIR / COPY --from=build /docker-gs-ping /docker-gs-ping EXPOSE 8080 USER nonroot:nonroot ENTRYPOINT ["/docker-gs-ping"]
- pulls the
COPYs your local source into the container environment (
go.sumfirst and running
go mod download, to cache dependencies in the container environment)
go buildon your source, inside the container, to produce an executable
COPYs the executable built in the previous step into a new image, on top of a minimal distroless base image.
The result is a Go application built on a minimal base image, with an optimally cached build sequence.
docker build on this
Dockerfile, don't forget to push that image to the registry so you can deploy it.
If your Go source is laid out as described in the tutorial, and you've installed and set up your environment, you can simply run
ko build ./ to build and push the container image to your registry.
You're done. You can delete your
Dockerfile and uninstall
ko takes advantage of your local Go build cache without needing to be told to, and it sets the
ENTRYPOINT and uses a nonroot distroless base image by default.
To build a multi-arch image, simply add
Compare this to the equivalent Docker instructions.