Multi-stage container build for smaller image size

This commit is contained in:
Imbus 2024-02-12 18:56:26 +01:00
parent 1887836c6e
commit 5c1b810da1

View file

@ -2,26 +2,39 @@
# We use node:latest as the base image. # We use node:latest as the base image.
# Essentially we build the frontend SPA with vite and # Essentially we build the frontend SPA with vite and
# make it available in the public directory. # make it available in the public directory.
FROM docker.io/node:latest as client FROM docker.io/node:alpine as client
WORKDIR /build WORKDIR /build
ADD frontend /build ADD frontend /build
RUN npm install RUN npm install
RUN npm run build RUN npm run build
# Builds the server in an isolated stage # Builds the server in an isolated stage
FROM docker.io/golang:latest as go FROM docker.io/golang:alpine as go
RUN apk add gcompat
RUN apk add gcc
RUN apk add musl-dev
ADD backend /build ADD backend /build
WORKDIR /build WORKDIR /build
# Copy the frontend SPA build into public
COPY --from=client /build/dist /build/static
# Get the dependencies # Get the dependencies
RUN go mod download RUN go mod download
# RUN go build -o server # RUN go build -o server
RUN CGO_ENABLED=1 GOOS=linux go build -a -installsuffix cgo -o ./server ./cmd/ RUN CGO_ENABLED=1 GOOS=linux go build -a -installsuffix cgo -o ./server ./cmd/
# The final stage for building a minimal image
FROM docker.io/alpine:latest as runner
WORKDIR /app
# Copy the frontend SPA build into public
COPY --from=client /build/dist /app/static
# Copy the server binary
COPY --from=go /build/server /app/server
# Copy the migration scripts
COPY --from=go /build/migrations /app/migrations
# Expose port 8080 # Expose port 8080
EXPOSE 8080 EXPOSE 8080