From 5ed427a4a073a1fce8f36e666daa514c16c0b544 Mon Sep 17 00:00:00 2001 From: Niall Sheridan Date: Sun, 7 Jul 2019 20:44:04 +0100 Subject: Statically link cashierd with C libs --- Dockerfile | 2 +- Makefile | 16 +++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index 33f1b34..b9ab104 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,7 @@ COPY go.mod . COPY go.sum . RUN go mod download COPY . . -RUN CGO_ENABLED=0 GOOS=linux make install-cashierd +RUN CGO_ENABLED=1 GOOS=linux make install-cashierd FROM gcr.io/distroless/static LABEL maintainer="nsheridan@gmail.com" diff --git a/Makefile b/Makefile index db969b8..278ed8d 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,10 @@ CASHIER_CMD := ./cmd/cashier CASHIERD_CMD := ./cmd/cashierd SRC_FILES = $(shell find * -type f -name '*.go' -not -path 'vendor/*' -not -name 'a_*-packr.go') -VERSION_PKG := "github.com/nsheridan/cashier/lib.Version" +VERSION_PKG := github.com/nsheridan/cashier/lib.Version VERSION := $(shell git describe --tags --always --dirty) +STATIC_LINKER_FLAGS ?= -linkmode external -extldflags -static -w GOOS ?= $(shell go env GOOS) GOARCH ?= $(shell go env GOARCH) CGO_ENABLED ?= $(shell go env CGO_ENABLED) @@ -22,15 +23,20 @@ lint: dep @[ -z "`git status --porcelain`" ] || (echo "unexpected files: `git status --porcelain`" && exit 1) build: cashier cashierd +install: install-cashierd install-cashier +cashier: cashier-bin +cashierd: cashierd-bin generate: go generate ./... -%-cmd: - CGO_ENABLED=$(CGO_ENABLED) GOARCH=$(GOARCH) GOOS=$(GOOS) go build -ldflags="-X $(VERSION_PKG)=$(VERSION)" -o $* ./cmd/$* - +%-bin: + CGO_ENABLED=$(CGO_ENABLED) GOARCH=$(GOARCH) GOOS=$(GOOS) go build -ldflags="-X $(VERSION_PKG)=$(VERSION) $(STATIC_LINKER_FLAGS)" ./cmd/$* install-%: generate - CGO_ENABLED=$(CGO_ENABLED) GOARCH=$(GOARCH) GOOS=$(GOOS) go install -ldflags="-X $(VERSION_PKG)=$(VERSION)" ./cmd/$* + CGO_ENABLED=$(CGO_ENABLED) GOARCH=$(GOARCH) GOOS=$(GOOS) go install -ldflags="-X $(VERSION_PKG)=$(VERSION) $(STATIC_LINKER_FLAGS)" ./cmd/$* + +docker-image: + docker build -f Dockerfile . clean: rm -f cashier cashierd -- cgit v1.2.3