apskel-pos-backend/Dockerfile

107 lines
2.4 KiB
Docker
Raw Normal View History

2024-06-04 14:25:57 +07:00
# Build Stage
2025-07-18 20:15:12 +07:00
FROM golang:1.21-alpine AS build
2023-10-08 15:59:42 +07:00
2025-07-18 20:10:29 +07:00
# Install necessary packages including CA certificates
RUN apk --no-cache add ca-certificates tzdata git curl
2023-10-08 15:59:42 +07:00
WORKDIR /src
2025-07-18 20:10:29 +07:00
# Copy go mod files first for better caching
COPY go.mod go.sum ./
# Download dependencies
RUN go mod download
# Copy source code
COPY . .
# Build the application
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o /app cmd/server/main.go
# Development Stage
2025-07-18 20:15:12 +07:00
FROM golang:1.21-alpine AS development
2025-07-18 20:10:29 +07:00
# Install air for live reload and other dev tools
RUN go install github.com/cosmtrek/air@latest
# Install necessary packages
RUN apk --no-cache add ca-certificates tzdata git curl
WORKDIR /app
# Copy go mod files
COPY go.mod go.sum ./
RUN go mod download
# Copy source code
2023-10-08 15:59:42 +07:00
COPY . .
2025-07-18 20:10:29 +07:00
# Set timezone
ENV TZ=Asia/Jakarta
2023-10-08 15:59:42 +07:00
2025-07-18 20:10:29 +07:00
# Expose port
EXPOSE 3300
2023-10-08 15:59:42 +07:00
2025-07-18 20:10:29 +07:00
# Use air for live reload in development
CMD ["air", "-c", ".air.toml"]
2023-10-08 15:59:42 +07:00
2025-07-18 20:10:29 +07:00
# Migration Stage
FROM build AS migration
2023-10-08 15:59:42 +07:00
2025-07-18 20:10:29 +07:00
# Install migration tool
RUN go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
WORKDIR /app
# Copy migration files
COPY migrations ./migrations
COPY infra ./infra
# Set the entrypoint for migrations
ENTRYPOINT ["migrate"]
# Production Stage
FROM debian:bullseye-slim AS production
2025-08-10 20:41:34 +07:00
# Install minimal runtime dependencies + Chrome, Chromium, and wkhtmltopdf for PDF generation
2025-07-18 20:10:29 +07:00
RUN apt-get update && apt-get install -y \
ca-certificates \
tzdata \
curl \
2025-08-10 20:41:34 +07:00
fontconfig \
wget \
gnupg \
&& wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list \
&& apt-get update \
&& apt-get install -y google-chrome-stable chromium wkhtmltopdf \
2025-07-18 20:10:29 +07:00
&& rm -rf /var/lib/apt/lists/*
# Create non-root user for security
RUN groupadd -r appuser && useradd -r -g appuser appuser
# Copy the binary
2023-10-08 15:59:42 +07:00
COPY --from=build /app /app
2025-07-18 20:10:29 +07:00
# Copy configuration files
COPY --from=build /src/infra /infra
# Change ownership to non-root user
RUN chown -R appuser:appuser /app /infra
2023-10-08 15:59:42 +07:00
2025-07-18 20:10:29 +07:00
# Set timezone
2023-10-08 15:59:42 +07:00
ENV TZ=Asia/Jakarta
2025-07-18 20:10:29 +07:00
# Expose port
EXPOSE 3300
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
CMD curl -f http://localhost:3300/health || exit 1
# Switch to non-root user
USER appuser
# Set the entrypoint
2023-10-08 15:59:42 +07:00
ENTRYPOINT ["/app"]