From e7e82a5ec71b1c4db85871b9c5d4bfd33f10bcbb Mon Sep 17 00:00:00 2001 From: Joakim Karlsson <5434736+roffe@users.noreply.github.com> Date: Mon, 13 Aug 2018 16:18:39 +0200 Subject: [PATCH] use multistage build Dockerfile (#142) * use multistage build dockerfile * add .git to dockeringore * simplify makefile --- .dockerignore | 5 +++++ .drone.yml | 5 ----- Dockerfile | 11 +++++++---- Makefile | 12 ++++++++---- 4 files changed, 20 insertions(+), 13 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..265dcf72 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +.git +bin +docs +ci +deploy diff --git a/.drone.yml b/.drone.yml index d08af365..20c9f171 100755 --- a/.drone.yml +++ b/.drone.yml @@ -16,11 +16,6 @@ pipeline: commands: - make bench - build: - image: golang:1.10 - commands: - - make build-linux - docker: when: event: push diff --git a/Dockerfile b/Dockerfile index 20b4f89c..f56b0e26 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,10 @@ -FROM alpine:3.8 +FROM golang:1.10-alpine3.8 as build +RUN apk add --no-cache make +WORKDIR /go/src/github.com/uswitch/kiam +ADD . . +RUN make +FROM alpine:3.8 RUN apk --no-cache add iptables - -COPY bin/kiam-linux-amd64 /kiam - +COPY --from=build /go/src/github.com/uswitch/kiam/bin/kiam-linux-amd64 /kiam CMD [] diff --git a/Makefile b/Makefile index 8cfe6409..4354e435 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,17 @@ +NAME?=kiam ARCH=amd64 BIN = bin/kiam BIN_LINUX = $(BIN)-linux-$(ARCH) BIN_DARWIN = $(BIN)-darwin-$(ARCH) - +GIT_BRANCH?=$(shell git rev-parse --abbrev-ref HEAD) +IMG_NAMESPACE?=quay.io/uswitch +IMG_TAG?=$(GIT_BRANCH) +REGISTRY?=$(IMG_NAMESPACE)/$(NAME) SOURCES := $(shell find . -iname '*.go') proto/service.pb.go .PHONY: test clean all coverage -all: proto/service.pb.go build-darwin build-linux +all: build-linux build-darwin: $(SOURCES) GOARCH=$(ARCH) GOOS=darwin go build -o $(BIN_DARWIN) cmd/kiam/*.go @@ -31,8 +35,8 @@ coverage: $(SOURCES) coverage.txt bench: $(SOURCES) go test -run=XX -bench=. github.com/uswitch/kiam/pkg/... -docker: Dockerfile $(BIN_LINUX) - docker image build -t quay.io/uswitch/kiam:devel . +docker: Dockerfile + docker image build -t "$(REGISTRY):$(IMG_TAG)" . clean: rm -rf bin/