zee-solution/Dockerfile
ulflow_phattt2901 f8957e0d95
Some checks failed
CI Pipeline / Security Scan (push) Successful in 5m1s
CI Pipeline / Lint (push) Failing after 6m9s
CI Pipeline / Test (push) Has been skipped
CI Pipeline / Build (push) Has been skipped
CI Pipeline / Notification (push) Successful in 1s
chore:update docker
2025-06-06 19:11:07 +07:00

98 lines
2.5 KiB
Docker

# Dockerfile
# Build arguments
ARG GO_VERSION=1.24.3
ARG ALPINE_VERSION=3.19
ARG APP_USER=appuser
ARG APP_GROUP=appgroup
# --- Builder Stage ---
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine AS builder
# Install build dependencies
RUN apk add --no-cache git make gcc libc-dev
# Set working directory
WORKDIR /build
# Enable Go modules and set build environment variables
ENV GO111MODULE=on \
CGO_ENABLED=0 \
GOOS=linux \
GOARCH=amd64
# Copy dependency files first for better layer caching
COPY go.mod go.sum ./
# Download dependencies using cache mount for efficiency
RUN --mount=type=cache,target=/go/pkg/mod \
go mod download
# Copy source code
COPY . .
# Create the configs directory in the build context (if it exists)
RUN if test -d "configs"; then mkdir -p /build/configs && cp -r configs/* /build/configs/; fi
# Build the application with optimizations and version info
RUN --mount=type=cache,target=/root/.cache/go-build \
go build -ldflags="-w -s -X 'main.Version=$(git describe --tags --always 2>/dev/null || echo 'dev')'" \
-o /build/bin/app ./src/cmd/api # Update path to your main package
# --- Final Stage ---
FROM alpine:${ALPINE_VERSION}
# Re-declare ARG to use in this stage
ARG APP_USER
ARG APP_GROUP
ARG APP_HOME=/app
# Set runtime environment variables
ENV TZ=Asia/Ho_Chi_Minh \
APP_USER=${APP_USER} \
APP_GROUP=${APP_GROUP} \
APP_HOME=${APP_HOME} \
APP_ENV=production
# Install runtime dependencies
RUN apk add --no-cache \
ca-certificates \
tzdata \
tini \
&& rm -rf /var/cache/apk/*
# Create app user and group
RUN addgroup -S ${APP_GROUP} && \
adduser -S -G ${APP_GROUP} -h ${APP_HOME} -D ${APP_USER}
# Create necessary application directories
RUN mkdir -p ${APP_HOME}/configs ${APP_HOME}/logs ${APP_HOME}/storage
# Switch to app directory
WORKDIR ${APP_HOME}
# Copy binary and configs from builder
COPY --from=builder --chown=${APP_USER}:${APP_GROUP} /build/bin/app .
COPY --from=builder --chown=${APP_USER}:${APP_GROUP} /build/configs ./configs/
# Set file permissions
RUN chmod +x ./app && \
chown -R ${APP_USER}:${APP_GROUP} ${APP_HOME}
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:3000/health || exit 1
# Switch to non-root user
USER ${APP_USER}
# Default command
CMD ["./app"]
# Expose port
EXPOSE 3000
# Set environment variable for production
ENV APP_ENV=production
# Command to run the application
ENTRYPOINT ["/sbin/tini", "--"]