I am going to advertise a not well-known parameter that can accelerate your docker builds significantly.
It is called BuildKit.
When BuildKit is enabled, it skips all unused stages and builds each line or layer in parallel to accelerate build times, while the traditional build command does all of these serially.
It was an experimental feature of Docker for a long time. But since 18.09 or higher versions, you can enable it either by setting
export DOCKER_BUILDKIT=1
or you can modify your** /etc/docker/daemon.json **to enable it by default as following
{ "features": { "buildkit": true } }
Assume that you have a Dockerfile as following.
FROM golang:1.13.5 AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-w -s" -o main .
FROM scratch
WORKDIR /app
COPY --from=builder /app/configs/ ./configs/
COPY --from=builder /app/main ./main
EXPOSE 3200
ENTRYPOINT ["/app/main"]
I have built this Dockerfile in both traditional and BuildKit way.
Standard Docker build time
docker build -t go-dockerbuild .
0.09s user 0.07s system 0% cpu 1:33.46 total
BuildKit build time
DOCKER_BUILDKIT=1 docker build -t go-dockerbuildkit .
0.28s user 0.22s system 0% cpu 1:00.01 total
It saved me 33 seconds which means %35 decrease in build time. It is just awesome . Especially in the world of microservices and continuous deployment , it is a significant improvement.
To sum up, BuildKit is one of the best practices of Docker now , it is highly recommended and performant.
#buildkit #docker