-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dockerfile
72 lines (63 loc) · 2.08 KB
/
Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# based on https://medium.com/@chemidy/create-the-smallest-and-secured-golang-docker-image-based-on-scratch-4752223b7324
############################
# STEP 1 build executable binary
############################
FROM --platform=$BUILDPLATFORM golang:1.20-alpine AS builder
ARG USER=appuser
ARG UID=10001
ARG GO_FILES=.
ARG GO_MAIN=./main/main.go
LABEL maintainer="Lukas Paluch <[email protected]>"
# Install git.
# Git is required for fetching the dependencies.
RUN apk update && apk add --no-cache git
# Ca-certificates is required to call HTTPS endpoints.
RUN apk update && apk add --no-cache ca-certificates && update-ca-certificates
# Set the Current Working Directory inside the container
WORKDIR /app
# See https://stackoverflow.com/a/55757473/12429735RUN
RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
"${USER}"
# Fetch dependencies.
COPY ${GO_FILES}/go.mod .
COPY ${GO_FILES}/go.sum .
RUN go mod download
# Using go get.
RUN go get -v all
# copy code files
COPY ${GO_FILES} .
# Build the binary.
# TARGETOS and TARGETARCH are builtin vars
ARG TARGETOS
ARG TARGETARCH
ARG CGO_ENABLED=0
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} CGO_ENABLED=${CGO_ENABLED} go build -ldflags="-w -s" -o /app/main.bin ${GO_MAIN}
# create tmp folder for use in scratch
RUN mkdir /my_tmp
RUN chown -R ${UID}:${UID} /my_tmp
############################
# STEP 2 build a small image
############################
FROM scratch
ARG UID=10001
ARG PROJECT_NAME=sero
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
# Import the user and group files from the builder.
COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /etc/group /etc/group
# create tmp directory
COPY --from=builder --chown=${UID}:${UID} /my_tmp /tmp
# Copy static executable.
COPY --from=builder --chown=${UID}:${UID} /app/main.bin /main
ENV UID=$UID
# Use an unprivileged user.
USER ${UID}:${UID}
LABEL org.opencontainers.image.source=https://github.com/fluktuid/sero
# Run the binary.
ENTRYPOINT ["/main"]