Skip to content

Commit

Permalink
build: use buildkit cache for faster docker builds
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredallard committed Sep 26, 2020
1 parent 5497b8b commit 0fee46b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
17 changes: 12 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down
9 changes: 4 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0fee46b

Please sign in to comment.