This commit is contained in:
Tomas Dvorak
2025-11-21 08:44:44 +01:00
parent c941313fd5
commit f5b6f83974
108 changed files with 8642 additions and 5871 deletions
+51 -10
View File
@@ -1,18 +1,26 @@
FROM golang:1.24.5-alpine AS builder
# Build stage
FROM golang:1.24.5-bullseye AS builder
ARG REMBG_ENABLED=true
WORKDIR /app
# Install build dependencies
RUN apk add --no-cache gcc musl-dev git
# Copy go mod and sum files
COPY go.mod go.sum ./
RUN --mount=type=cache,target=/var/lib/apt/lists \
--mount=type=cache,target=/var/cache/apt \
apt-get update && apt-get install -y --no-install-recommends \
git \
build-essential \
&& if [ "$REMBG_ENABLED" = "true" ]; then apt-get install -y --no-install-recommends python3 python3-pip python3-dev; fi \
&& rm -rf /var/lib/apt/lists/*
# Configure Go proxy with fallback and download dependencies with retry
ENV GOPROXY=https://proxy.golang.org,direct
ENV GOPRIVATE=
ENV GOSUMDB=sum.golang.org
# Copy go mod and sum files
COPY go.mod go.sum ./
# Download all dependencies with retry logic and cache mount
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
@@ -22,6 +30,11 @@ RUN --mount=type=cache,target=/go/pkg/mod \
done && \
go mod verify
# Install Python dependencies for rembg (before copying full source for better cacheability)
COPY scripts/requirements-rembg.txt .
RUN --mount=type=cache,target=/root/.cache/pip \
if [ "$REMBG_ENABLED" = "true" ]; then pip3 install -r requirements-rembg.txt; else echo "REMBG disabled, skipping pip install"; fi
# Copy the source code
COPY . .
@@ -31,20 +44,48 @@ RUN --mount=type=cache,target=/go/pkg/mod \
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
go build -ldflags="-w -s" -trimpath -o main .
# Use a smaller image for the final container
FROM alpine:latest
# Final stage
FROM debian:bullseye-slim
ARG REMBG_ENABLED=true
WORKDIR /app
# Copy the binary from builder
COPY --from=builder /app/main .
# Install runtime dependencies
RUN --mount=type=cache,target=/var/lib/apt/lists \
--mount=type=cache,target=/var/cache/apt \
apt-get update && apt-get install -y --no-install-recommends \
ca-certificates \
tzdata \
&& if [ "$REMBG_ENABLED" = "true" ]; then apt-get install -y --no-install-recommends python3 python3-pip python3-dev libgl1-mesa-glx libglib2.0-0; fi \
&& rm -rf /var/lib/apt/lists/*
# Copy static files and templates
# Create non-root user and directories
RUN addgroup --system app && adduser --system --ingroup app app \
&& mkdir -p /app/uploads /app/cache /app/static /app/templates \
&& chown -R app:app /app
# Install rembg and its dependencies
COPY --from=builder /app/requirements-rembg.txt .
RUN --mount=type=cache,target=/root/.cache/pip \
if [ "$REMBG_ENABLED" = "true" ]; then pip3 install -r requirements-rembg.txt; fi \
&& rm -f requirements-rembg.txt
# Copy the binary and other files
COPY --from=builder /app/main .
COPY --from=builder /app/static ./static
COPY --from=builder /app/templates ./templates
COPY --from=builder /app/scripts ./scripts
# Set environment and permissions
ENV GIN_MODE=debug
USER app
# Expose port
EXPOSE 8080
# Healthcheck
HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \
CMD wget -q -O - http://127.0.0.1:8080/api/v1/health >/dev/null 2>&1 || exit 1
# Command to run the executable
CMD ["./main"]