Skip to content

Commit

Permalink
Merge pull request #1621 from ministryofjustice/scratch
Browse files Browse the repository at this point in the history
Use scratch for image bases
  • Loading branch information
hawx authored Nov 18, 2024
2 parents 95d4be5 + c990e7e commit 3bda7a9
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 134 deletions.
5 changes: 2 additions & 3 deletions cmd/mock-os-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
package main

import (
"cmp"
"log"
"net/http"
"os"

"github.com/ministryofjustice/opg-go-common/env"
)

func main() {
port := env.Get("PORT", "8080")
port := cmp.Or(os.Getenv("PORT"), "8080")

http.HandleFunc("/search/places/v1/postcode", func(w http.ResponseWriter, r *http.Request) {
postcode := r.URL.Query().Get("postcode")
Expand Down
19 changes: 7 additions & 12 deletions docker/mlpa/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ ARG TAG=v0.0.0

WORKDIR /app

RUN addgroup -S user && \
adduser -S -g user user

COPY --link go.mod go.sum ./
RUN go mod download

Expand All @@ -44,27 +47,19 @@ COPY --link internal ./internal

RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags="-X main.Tag=${TAG}" -o /go/bin/mlpab ./cmd/mlpa

FROM alpine:3.20.3 AS production
FROM scratch AS production

WORKDIR /go/bin

COPY --link web/robots.txt web/robots.txt
COPY --from=asset-env /app/web/static web/static
COPY --from=build-env /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=build-env /etc/passwd /etc/passwd
COPY --from=build-env /go/bin/mlpab mlpab
COPY --link web/robots.txt web/robots.txt
COPY --link web/template web/template
COPY --link lang lang


RUN addgroup -S user && \
adduser -S -g user user && \
chown -R user:user mlpab web/template web/static web/robots.txt

COPY scripts/docker_hardening/alpine_image_hardening.sh /harden.sh

RUN /harden.sh && rm /harden.sh

USER user

EXPOSE 8080

ENTRYPOINT ["./mlpab"]
20 changes: 10 additions & 10 deletions docker/mock-notify/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
FROM golang:1.23.3-alpine AS build-env

RUN apk --no-cache add openssl=3.3.2-r1

WORKDIR /app

COPY go.mod go.sum ./
RUN apk --no-cache add openssl=3.3.2-r1

COPY --link go.mod go.sum ./
RUN go mod download

COPY cmd/mock-notify ./cmd/mock-notify
COPY --link cmd/mock-notify ./cmd/mock-notify

RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -a -installsuffix cgo -o /go/bin/mock-notify ./cmd/mock-notify

RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -a -installsuffix cgo -o mock-notify ./cmd/mock-notify
FROM scratch AS production

RUN addgroup --system app && \
adduser --system --gecos app app && \
chown -R app:app /app
WORKDIR /go/bin

USER app
COPY --from=build-env /go/bin/mock-notify mock-notify

CMD [ "/app/mock-notify" ]
CMD [ "./mock-notify" ]
22 changes: 11 additions & 11 deletions docker/mock-os-api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
FROM golang:1.23.3-alpine AS build-env

RUN apk --no-cache add openssl=3.3.2-r1

WORKDIR /app

COPY go.mod go.sum ./
RUN apk --no-cache add openssl=3.3.2-r1

COPY --link go.mod go.sum ./
RUN go mod download

COPY cmd/mock-os-api ./cmd/mock-os-api
COPY docker/mock-os-api/data ./data
COPY --link cmd/mock-os-api ./cmd/mock-os-api

RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -a -installsuffix cgo -o /go/bin/mock-os-api ./cmd/mock-os-api

RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -a -installsuffix cgo -o mock-os-api ./cmd/mock-os-api
FROM scratch AS production

RUN addgroup --system app && \
adduser --system --gecos app app && \
chown -R app:app /app
WORKDIR /go/bin

USER app
COPY --from=build-env /go/bin/mock-os-api mock-os-api
COPY --link docker/mock-os-api/data data

CMD [ "/app/mock-os-api" ]
CMD [ "./mock-os-api" ]
98 changes: 0 additions & 98 deletions scripts/docker_hardening/alpine_image_hardening.sh

This file was deleted.

0 comments on commit 3bda7a9

Please sign in to comment.