Files
MyClub/Dockerfile.dev
T
Tomas Dvorak 4ca3f4ff53 fix
2026-03-13 15:03:11 +01:00

93 lines
3.1 KiB
Docker

# Build stage
ARG GO_IMAGE_TAG=1.25.8-bookworm
FROM golang:${GO_IMAGE_TAG} AS builder
ARG REMBG_ENABLED=true
WORKDIR /app
# Install build 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 \
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 \
for i in 1 2 3 4 5; do \
go mod download && break || \
(echo "Attempt $i failed, retrying in 5s..." && sleep 5); \
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 --break-system-packages -r requirements-rembg.txt; else echo "REMBG disabled, skipping pip install"; fi
# Copy the source code
COPY . .
# Build the application with cache mount and optimizations
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
go build -ldflags="-w -s" -trimpath -o main .
# Final stage
FROM debian:bullseye-slim
ARG REMBG_ENABLED=true
WORKDIR /app
# 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/*
# 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 --break-system-packages -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"]