Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# syntax=docker/dockerfile:1.7

FROM golang:1.24.13-alpine AS builder

WORKDIR /src

# Copy dependency metadata first to preserve the module-cache layer.
COPY go.mod ./
RUN go mod download

# Copy application source after dependencies.
COPY *.go ./

RUN mkdir -p /out/data && \
CGO_ENABLED=0 GOOS=linux go test ./... && \
CGO_ENABLED=0 GOOS=linux go build \
-trimpath \
-ldflags="-s -w" \
-o /out/quicknotes \
.

# Build a static healthcheck executable because distroless has no shell,
# curl, wget, or package manager.
RUN <<'BUILD_HEALTHCHECK'
cat > /tmp/healthcheck.go <<'GO'
package main

import (
"net/http"
"os"
"time"
)

func main() {
client := http.Client{
Timeout: 2 * time.Second,
}

response, err := client.Get("http://127.0.0.1:8080/health")
if err != nil {
os.Exit(1)
}
defer response.Body.Close()

if response.StatusCode != http.StatusOK {
os.Exit(1)
}
}
GO

CGO_ENABLED=0 GOOS=linux go build \
-trimpath \
-ldflags="-s -w" \
-o /out/healthcheck \
/tmp/healthcheck.go
BUILD_HEALTHCHECK

FROM gcr.io/distroless/static-debian12:nonroot AS runtime

WORKDIR /

COPY --from=builder --chown=65532:65532 /out/quicknotes /quicknotes
COPY --from=builder --chown=65532:65532 /out/healthcheck /healthcheck
COPY --from=builder --chown=65532:65532 /out/data /data
COPY --chown=65532:65532 seed.json /seed.json

ENV ADDR=:8080 \
DATA_PATH=/data/notes.json \
SEED_PATH=/seed.json

EXPOSE 8080

USER 65532:65532

ENTRYPOINT ["/quicknotes"]
32 changes: 32 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
services:
quicknotes:
image: quicknotes:lab6
build:
context: ./app
dockerfile: Dockerfile
ports:
- "8080:8080"
environment:
ADDR: ":8080"
DATA_PATH: "/data/notes.json"
SEED_PATH: "/seed.json"
volumes:
- quicknotes-data:/data
healthcheck:
test: ["CMD", "/healthcheck"]
interval: 5s
timeout: 3s
retries: 5
start_period: 3s
restart: unless-stopped
user: "65532:65532"
cap_drop:
- ALL
read_only: true
tmpfs:
- /tmp:rw,noexec,nosuid,nodev,size=16m
security_opt:
- no-new-privileges:true

volumes:
quicknotes-data:
Loading