Files
minitor/Dockerfile
gyurix 753eaeab10
continuous-integration/drone/push Build is failing
Add debug logging to sendmail function for improved traceability
2026-04-12 15:18:59 +02:00

103 lines
3.1 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 ' "flag"'; \
echo ' "fmt"'; \
echo ' "io"'; \
echo ' "net/smtp"'; \
echo ' "os"'; \
echo ')'; \
echo ''; \
echo 'func main() {'; \
echo ' from := flag.String("f", "minitor@localhost", "envelope sender")'; \
echo ' flag.Parse()'; \
echo ' recipients := flag.Args()'; \
echo ''; \
echo ' if len(recipients) == 0 {'; \
echo ' fmt.Fprintln(os.Stderr, "usage: sendmail [-f sender] recipient...")'; \
echo ' os.Exit(1)'; \
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 ' 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("SMTP_SENDER")'; \
echo ' if sender == "" {'; \
echo ' sender = *from'; \
echo ' }'; \
echo ''; \
echo ' debug := os.Getenv("DEBUG") != ""'; \
echo ' if debug {'; \
echo ' fmt.Fprintf(os.Stderr, "[sendmail] relay=%s port=%s sender=%s recipients=%v\n", relay, port, sender, recipients)'; \
echo ' fmt.Fprintf(os.Stderr, "[sendmail] body:\n%s\n", 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" ]