-
Notifications
You must be signed in to change notification settings - Fork 2
/
Containerfile
74 lines (63 loc) · 1.79 KB
/
Containerfile
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
73
74
############################
# STEP 1 prepare binary and base data
############################
FROM docker.io/library/golang:1.22.1-alpine3.19 AS builder
ARG USER=appuser
ARG UID=10001
ARG GO_FILES=.
ARG GO_MAIN=main.go
# Install git.
# Git is required for fetching the dependencies.
RUN apk update && apk add --no-cache git
# 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
# get go 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 ${GO_MAIN}
# create tmp folder for use in scratch
RUN mkdir /my_tmp
RUN chown -R ${USER}:${USER} /my_tmp
############################
# STEP 2 build a small image
############################
FROM scratch
ARG USER=appuser
ARG UID=10001
LABEL maintainer="schlauerlauer <github.com/schlauerlauer>"
# Import the user and group files from the builder.
COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /etc/group /etc/group
# copy license
COPY LICENSE /LICENSE
# create tmp directory
COPY --from=builder /my_tmp /tmp
# Copy static executable.
COPY --from=builder /app/main /app
COPY config.yaml LICENSE /
ENV USER=$USER
# Use an unprivileged user.
USER ${UID}:${UID}
LABEL org.opencontainers.image.source=https://github.com/schlauerlauer/alertmanager-webhook-signal
# Run the binary.
EXPOSE 10000
CMD ["./app"]