Files
2026-04-14 07:38:08 +02:00

130 lines
4.4 KiB
Docker

FROM golang:1.25 AS gomail-builder
ARG TARGETARCH=amd64
ARG TARGETOS=linux
WORKDIR /gomail
RUN { \
echo 'package main'; \
echo ''; \
echo 'import ('; \
echo ' "fmt"'; \
echo ' "io"'; \
echo ' "net/mail"'; \
echo ' "net/smtp"'; \
echo ' "os"'; \
echo ' "strings"'; \
echo ')'; \
echo ''; \
echo 'func main() {'; \
echo ' readHeaders := strings.Contains(strings.Join(os.Args[1:], " "), "-t")'; \
echo ' recipients := []string{}'; \
echo ' for _, arg := range os.Args[1:] {'; \
echo ' if !strings.HasPrefix(arg, "-") {'; \
echo ' recipients = append(recipients, arg)'; \
echo ' }'; \
echo ' }'; \
echo ''; \
echo ' body, err := io.ReadAll(os.Stdin)'; \
echo ' if err != nil {'; \
echo ' fmt.Fprintln(os.Stderr, err)'; \
echo ' os.Exit(1)'; \
echo ' }'; \
echo ''; \
echo ' if readHeaders {'; \
echo ' msg, parseErr := mail.ReadMessage(strings.NewReader(string(body)))'; \
echo ' if parseErr != nil {'; \
echo ' fmt.Fprintln(os.Stderr, parseErr)'; \
echo ' os.Exit(1)'; \
echo ' }'; \
echo ' for _, hdr := range []string{"To", "Cc", "Bcc"} {'; \
echo ' if val := msg.Header.Get(hdr); val != "" {'; \
echo ' addrs, addrErr := mail.ParseAddressList(val)'; \
echo ' if addrErr != nil {'; \
echo ' fmt.Fprintln(os.Stderr, addrErr)'; \
echo ' os.Exit(1)'; \
echo ' }'; \
echo ' for _, addr := range addrs {'; \
echo ' recipients = append(recipients, addr.Address)'; \
echo ' }'; \
echo ' }'; \
echo ' }'; \
echo ' }'; \
echo ''; \
echo ' if len(recipients) == 0 {'; \
echo ' fmt.Fprintln(os.Stderr, "usage: sendmail [-t] recipient...")'; \
echo ' os.Exit(1)'; \
echo ' }'; \
echo ''; \
echo ' relay := os.Getenv("SMTP_RELAY")'; \
echo ' if relay == "" {'; \
echo ' relay = "172.17.0.2"'; \
echo ' }'; \
echo ''; \
echo ' port := os.Getenv("SMTP_PORT")'; \
echo ' if port == "" {'; \
echo ' port = "25"'; \
echo ' }'; \
echo ''; \
echo ' sender := os.Getenv("EMAIL_FROM")'; \
echo ' if sender == "" {'; \
echo ' fmt.Fprintln(os.Stderr, "[sendmail] EMAIL_FROM is not set, skipping")'; \
echo ' os.Exit(0)'; \
echo ' }'; \
echo ''; \
echo ' debug := os.Getenv("DEBUG") != ""'; \
echo ' if debug {'; \
echo ' fmt.Fprintln(os.Stderr, fmt.Sprintf("[sendmail] relay=%s port=%s sender=%s recipients=%v", relay, port, sender, recipients))'; \
echo ' fmt.Fprintln(os.Stderr, "[sendmail] body:")'; \
echo ' fmt.Fprintln(os.Stderr, string(body))'; \
echo ' }'; \
echo ''; \
echo ' if err = smtp.SendMail(relay+":"+port, nil, sender, recipients, body); err != nil {'; \
echo ' fmt.Fprintln(os.Stderr, err)'; \
echo ' os.Exit(1)'; \
echo ' }'; \
echo ' if debug {'; \
echo ' fmt.Fprintln(os.Stderr, "[sendmail] sent successfully")'; \
echo ' }'; \
echo '}'; \
} > main.go
RUN go mod init gomail && \
CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o /usr/local/bin/sendmail .
FROM golang:1.25 AS builder
WORKDIR /app
COPY ./go.mod ./go.sum /app/
RUN go mod download
COPY ./*.go /app/
RUN rm -f /app/gomail.go
ARG TARGETARCH=amd64
ARG TARGETOS=linux
ARG VERSION=dev
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags "-X main.version=${VERSION}" -a -installsuffix nocgo -o minitor .
FROM alpine:3.23
RUN mkdir /app
WORKDIR /app/
# Copy minitor in
COPY --from=builder /app/minitor .
# Copy sendmail (gomail) in
COPY --from=gomail-builder /usr/local/bin/sendmail /usr/local/bin/sendmail
# Add common checking tools
# hadolint ignore=DL3018
RUN apk --no-cache add bash=~5 curl=~8 jq=~1 bind-tools=~9 tzdata
# Add minitor user for running as non-root
RUN addgroup -S minitor && adduser -S minitor -G minitor
# Copy scripts
COPY ./scripts /app/scripts
COPY default_config.hcl /app/config.hcl
RUN chmod -R 755 /app/scripts
# Drop to non-root user
USER minitor
ENTRYPOINT [ "./minitor" ]