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:
FROM golang:1.16-buster AS build
COPY go.mod ./
COPY go.sum ./
RUN go mod download
COPY *.go ./
RUN go build -o /docker-gs-ping
COPY --from=build /docker-gs-ping /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.
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.