From 0fee46b0df23337474e8b08635970665c8f5aa51 Mon Sep 17 00:00:00 2001 From: Jared Allard Date: Fri, 25 Sep 2020 23:05:07 -0700 Subject: [PATCH] build: use buildkit cache for faster docker builds --- Dockerfile | 17 ++++++++++++----- Makefile | 9 ++++----- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/Dockerfile b/Dockerfile index 3756bb4..8790e8f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,18 +1,25 @@ # syntax=docker/dockerfile:1.0-experimental FROM golang:1.15-alpine as builder ARG VERSION +ENV GOCACHE "/go-build-cache" WORKDIR /src +# Why: Pinning versions in a docker container isn't +# really worth the effort. +# hadolint ignore=DL3018 RUN apk add --no-cache make bash git libc-dev gcc -# Add go.mod and go.sum first to maximize caching -COPY ./go.mod ./go.sum ./ -RUN go mod download - +# Copy all files into our docker container. COPY . . +# --mount here allows us to cache the packages even if +# it's invalidated by go.mod,go.sum +RUN --mount=type=cache,target=/go/pkg make dep + # Build our application -RUN make build APP_VERSION=${VERSION} +# --mount here allows us to save go build cache across builds +# but also needed to use the package cache above +RUN --mount=type=cache,target=/go-build-cache --mount=type=cache,target=/go/pkg make build APP_VERSION=${VERSION} FROM alpine:3.12 ENTRYPOINT ["/usr/bin/localizer"] diff --git a/Makefile b/Makefile index ec9e766..d613d67 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ GO ?= go SHELL := /usr/bin/env bash GOOS := $(shell go env GOOS) GOARCH := $(shell go env GOARCH) -PKG := $(GO) mod download +PKG := $(GO) mod download -x # TODO(jaredallard): infer from Git tag APP_VERSION := $(shell git describe --match 'v[0-9]*' --tags --abbrev=0 --always HEAD) LDFLAGS := -w -s @@ -12,7 +12,6 @@ GOPROXY := https://proxy.golang.org GO_EXTRA_FLAGS := -v TAGS := BINDIR := $(CURDIR)/bin -BIN_NAME := bootstraper PKGDIR := github.com/jaredallard/localizer CGO_ENABLED := 1 BENCH_FLAGS := "-bench=Bench $(BENCH_FLAGS)" @@ -61,9 +60,9 @@ gobuild: mkdir -p $(BINDIR) GOPROXY=$(GOPROXY) CGO_ENABLED=$(CGO_ENABLED) $(GO) build -o $(BINDIR)/ -ldflags "$(LDFLAGS)" $(GO_EXTRA_FLAGS) $(PKGDIR)/... -.PHONY: docker-build-push -docker-build-push: - @$(LOG) info "Building and push docker image" +.PHONY: docker-build +docker-build: + @$(LOG) info "Building docker image" DOCKER_BUILDKIT=1 docker build -t "jaredallard/localizer:latest" . .PHONY: fmt