Update Dockerfile

Fix my absolute shit-show of a Docker refactor.
This commit is contained in:
Katos
2024-10-12 15:53:40 +01:00
committed by GitHub
parent 55acfa54f5
commit 67c77a7d91

View File

@@ -1,44 +1,91 @@
# Node dependencies # Node dependencies stage
FROM node:18-alpine AS frontend-dependencies FROM --platform=$TARGETPLATFORM node:18-alpine AS frontend-dependencies
WORKDIR /app WORKDIR /app
# Install pnpm globally (caching layer)
RUN npm install -g pnpm RUN npm install -g pnpm
# Copy package.json and lockfile to leverage caching
COPY frontend/package.json frontend/pnpm-lock.yaml ./ COPY frontend/package.json frontend/pnpm-lock.yaml ./
RUN pnpm install --frozen-lockfile --shamefully-hoist RUN pnpm install --frozen-lockfile --shamefully-hoist
# Build Nuxt # Build Nuxt (frontend) stage
FROM node:18-alpine AS frontend-builder FROM --platform=$TARGETPLATFORM node:18-alpine AS frontend-builder
WORKDIR /app WORKDIR /app
COPY frontend ./
# Install pnpm globally again (it can reuse the cache if not changed)
RUN npm install -g pnpm
# Copy over source files and node_modules from dependencies stage
COPY frontend .
COPY --from=frontend-dependencies /app/node_modules ./node_modules COPY --from=frontend-dependencies /app/node_modules ./node_modules
RUN pnpm build RUN pnpm build
FROM golang:alpine AS builder-dependencies # Go dependencies stage
FROM --platform=$TARGETPLATFORM golang:alpine AS builder-dependencies
WORKDIR /go/src/app WORKDIR /go/src/app
# Copy go.mod and go.sum for better caching
COPY ./backend/go.mod ./backend/go.sum ./ COPY ./backend/go.mod ./backend/go.sum ./
RUN go mod download RUN go mod download
# Build API # Build API stage
FROM golang:alpine AS builder FROM --platform=$TARGETPLATFORM golang:alpine AS builder
ARG BUILD_TIME ARG BUILD_TIME
ARG COMMIT ARG COMMIT
ARG VERSION ARG VERSION
RUN apk update && apk upgrade && apk add --no-cache git build-base gcc g++
# Install necessary build tools
RUN apk update && \
apk upgrade && \
apk add --no-cache git build-base gcc g++
WORKDIR /go/src/app WORKDIR /go/src/app
# Copy Go modules (from dependencies stage) and source code
COPY --from=builder-dependencies /go/pkg/mod /go/pkg/mod
COPY ./backend . COPY ./backend .
# Clear old public files and copy new ones from frontend build
RUN rm -rf ./app/api/public RUN rm -rf ./app/api/public
COPY --from=frontend-builder /app/.output/public ./app/api/static/public COPY --from=frontend-builder /app/.output/public ./app/api/static/public
COPY --from=builder-dependencies /go/pkg/mod /go/pkg/mod
# Use cache for Go build artifacts
RUN --mount=type=cache,target=/root/.cache/go-build \ RUN --mount=type=cache,target=/root/.cache/go-build \
CGO_ENABLED=0 GOOS=linux go build \ CGO_ENABLED=0 GOOS=linux go build \
-ldflags "-s -w -X main.commit=$COMMIT -X main.buildTime=$BUILD_TIME -X main.version=$VERSION" \ -ldflags "-s -w -X main.commit=$COMMIT -X main.buildTime=$BUILD_TIME -X main.version=$VERSION" \
-o /go/bin/api ./app/api/*.go -o /go/bin/api \
-v ./app/api/*.go
# Production Stage # Production stage
FROM alpine:latest FROM --platform=$TARGETPLATFORM alpine:latest
WORKDIR /app ENV HBOX_MODE=production
COPY --from=builder /go/bin/api /app/api ENV HBOX_STORAGE_DATA=/data/
ENV HBOX_STORAGE_SQLITE_URL=/data/homebox.db?_pragma=busy_timeout=2000&_pragma=journal_mode=WAL&_fk=1
# Install necessary runtime dependencies
RUN apk --no-cache add ca-certificates wget RUN apk --no-cache add ca-certificates wget
ENTRYPOINT ["/app/api"] # Create application directory and copy over built Go binary
CMD ["/data/config.yml"] RUN mkdir /app
COPY --from=builder /go/bin/api /app
RUN chmod +x /app/api
# Labels and configuration for the final image
LABEL Name=homebox Version=0.0.1
LABEL org.opencontainers.image.source="https://github.com/sysadminsmedia/homebox"
# Expose necessary ports
EXPOSE 7745
WORKDIR /app
# Healthcheck configuration
HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
CMD [ "wget", "--no-verbose", "--tries=1", "-O", "-", "http://localhost:7745/api/v1/status" ]
# Persist volume
VOLUME [ "/data" ]
# Entrypoint and CMD
ENTRYPOINT [ "/app/api" ]
CMD [ "/data/config.yml" ]