Skip to content

Commit a28f2ef

Browse files
authored
Merge pull request #45 from rog-golang-buddies/dockerfile-fix
fix(Dockerfile): Go image update What does this commit do? ========================= * Use alpine Go base image to reduce size * Reuse make command to build binary Why are we doing this? ====================== * Shrink image size to increase build times * Reuse make to provide a single place add binary build updates
2 parents 5b3977d + 4d9a96a commit a28f2ef

File tree

5 files changed

+30
-8
lines changed

5 files changed

+30
-8
lines changed

.github/dependabot.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ updates:
55
schedule:
66
interval: weekly
77
open-pull-requests-limit: 5
8+
- package-ecosystem: "docker"
9+
directory: "/"
10+
schedule:
11+
interval: "weekly"

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,9 @@ bin/
1515
# Dependency directories (remove the comment below to include it)
1616
# vendor/
1717

18+
# Code editor personal settings
19+
.vscode/
20+
.idea/
21+
1822
# Other
1923
.DS_Store

Dockerfile

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1-
FROM golang:1.18 as build
2-
WORKDIR /go/src/app
1+
FROM golang:1.18-alpine as builder
2+
3+
RUN apk update && apk upgrade && \
4+
apk add --no-cache make bash
5+
6+
WORKDIR /src
37
COPY . .
4-
# Static build requires CGO_ENABLED=0
5-
RUN mkdir -p /go/bin && CGO_ENABLED=0 go build -ldflags="-w -s" -o /go/bin/app ./...
8+
9+
# Build executable
10+
RUN make build
611

712
# Using a distroless image from https://github.com/GoogleContainerTools/distroless
8-
# Image sourced from https://console.cloud.google.com/gcr/images/distroless/global/static
913
FROM gcr.io/distroless/static:nonroot
10-
COPY --from=build /go/bin/app /
11-
# numeric version of user nonroot:nonroot provided in image
14+
15+
# Copy executable from builder image
16+
COPY --from=builder /src/bin/app /
17+
18+
# Numeric version of user nonroot:nonroot provided in image
1219
USER 65532:65532
20+
21+
# Run the executable
1322
CMD ["/app"]

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
SHELL=/bin/bash -e -o pipefail
22
PWD = $(shell pwd)
3+
GO_BUILD= go build
4+
GOFLAGS= CGO_ENABLED=0
35

46
## help: Print this help message
57
.PHONY: help
@@ -37,4 +39,4 @@ fmt:
3739
## build: Build binary into bin/ directory
3840
.PHONY: build
3941
build:
40-
go build -ldflags="-w -s" -o bin/app ./...
42+
$(GOFLAGS) $(GO_BUILD) -a -v -ldflags="-w -s" -o bin/app cmd/main.go

sonar-project.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ sonar.organization=rog-golang-buddies
1010

1111
# Encoding of the source code. Default is default system encoding
1212
#sonar.sourceEncoding=UTF-8
13+
14+
# ensure that test files are identified and not used to check coverage
15+
sonar.test.inclusions=**/*_test.go

0 commit comments

Comments
 (0)