Skip to content

Commit 40c7421

Browse files
committed
improve docker build
Signed-off-by: Markus Blaschke <mblaschke82@gmail.com>
1 parent c045337 commit 40c7421

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

Dockerfile

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,38 @@
1-
FROM golang:1.18-alpine as build
1+
#############################################
2+
# Build
3+
#############################################
4+
FROM --platform=$BUILDPLATFORM golang:1.18-alpine as build
25

36
RUN apk upgrade --no-cache --force
47
RUN apk add --update build-base make git
58

69
WORKDIR /go/src/github.com/webdevops/azure-auditor
710

11+
# Dependencies
12+
COPY go.mod go.sum .
13+
RUN go mod download
14+
815
# Compile
9-
COPY ./ /go/src/github.com/webdevops/azure-auditor
10-
RUN make dependencies
11-
#RUN make test
12-
RUN make build
13-
RUN ./azure-auditor --help
16+
COPY . .
17+
RUN make test
18+
ARG TARGETOS TARGETARCH
19+
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} make build
1420

1521
#############################################
16-
# FINAL IMAGE
22+
# Test
1723
#############################################
18-
FROM gcr.io/distroless/static
24+
FROM gcr.io/distroless/static as test
1925
ENV LOG_JSON=1
2026
COPY --from=build /go/src/github.com/webdevops/azure-auditor/azure-auditor /
2127
COPY --from=build /go/src/github.com/webdevops/azure-auditor/templates/ /templates/
28+
RUN ["/azure-auditor", "--help"]
29+
30+
#############################################
31+
# Final
32+
#############################################
33+
FROM gcr.io/distroless/static
34+
ENV LOG_JSON=1
35+
COPY --from=test /azure-auditor /
36+
COPY --from=test /go/src/github.com/webdevops/azure-auditor/templates/ /templates/
2237
USER 1000:1000
2338
ENTRYPOINT ["/azure-auditor"]

Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PROJECT_NAME := $(shell basename $(CURDIR))
22
GIT_TAG := $(shell git describe --dirty --tags --always)
33
GIT_COMMIT := $(shell git rev-parse --short HEAD)
4-
LDFLAGS := -X "main.gitTag=$(GIT_TAG)" -X "main.gitCommit=$(GIT_COMMIT)" -linkmode external -extldflags "-static" -s -w
4+
LDFLAGS := -X "main.gitTag=$(GIT_TAG)" -X "main.gitCommit=$(GIT_COMMIT)" -extldflags "-static" -s -w
55

66
FIRST_GOPATH := $(firstword $(subst :, ,$(shell go env GOPATH)))
77
GOLANGCI_LINT_BIN := $(FIRST_GOPATH)/bin/golangci-lint
@@ -16,7 +16,7 @@ clean:
1616

1717
.PHONY: build
1818
build:
19-
CGO_ENABLED=0 go build -a -ldflags '$(LDFLAGS)' -o $(PROJECT_NAME) .
19+
GOOS=${GOOS} GOARCH=${GOARCH} CGO_ENABLED=0 go build -a -ldflags '$(LDFLAGS)' -o $(PROJECT_NAME) .
2020

2121
.PHONY: vendor
2222
vendor:
@@ -29,7 +29,8 @@ image: build
2929
docker build -t $(PROJECT_NAME):$(GIT_TAG) .
3030

3131
build-push-development:
32-
docker build -t webdevops/$(PROJECT_NAME):development . && docker push webdevops/$(PROJECT_NAME):development
32+
docker buildx create --use
33+
docker buildx build -t webdevops/$(PROJECT_NAME):development --platform linux/amd64,linux/arm,linux/arm64 --push .
3334

3435
.PHONY: test
3536
test:

0 commit comments

Comments
 (0)