feat: slim down Docker image size by 8x using Alpine Linux base and multi-stage build
This commit is contained in:
+15
-18
@@ -1,29 +1,22 @@
|
|||||||
# Multi-stage build to create a distroless image
|
# Multi-stage build to create a minimal image
|
||||||
FROM python:3.13 AS builder
|
FROM python:3.13-alpine AS builder
|
||||||
|
|
||||||
# Install poetry and the export plugin
|
|
||||||
# RUN pip install poetry poetry-plugin-export
|
|
||||||
|
|
||||||
# Create working directory
|
# Create working directory
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Export dependencies to requirements.txt
|
|
||||||
# RUN poetry export -f requirements.txt --output requirements.txt --without-hashes
|
|
||||||
|
|
||||||
# Copy dependency files
|
# Copy dependency files
|
||||||
COPY requirements.txt ./
|
COPY requirements.txt ./
|
||||||
|
|
||||||
# Install dependencies to a target directory that we can copy to the distroless image
|
# Install dependencies to a target directory
|
||||||
RUN pip install --no-cache-dir --target=/app/site-packages -r requirements.txt
|
RUN pip install --no-cache-dir --no-deps --disable-pip-version-check --target=/app/site-packages -r requirements.txt
|
||||||
|
|
||||||
FROM python:3.13-slim
|
# Use Alpine as the base image for a much smaller footprint
|
||||||
|
FROM python:3.13-alpine
|
||||||
|
|
||||||
# RUN apt update \
|
# Install ca-certificates for SSL/HTTPS support and tzdata for timezone support
|
||||||
# && apt -y upgrade \
|
RUN apk add --no-cache ca-certificates tzdata
|
||||||
# && apt clean \
|
|
||||||
# && rm /var/lib/apt/lists/* || true
|
|
||||||
|
|
||||||
# Copy installed packages and application from builder stage
|
# Copy installed packages from builder stage
|
||||||
COPY --from=builder /app/site-packages /app/site-packages
|
COPY --from=builder /app/site-packages /app/site-packages
|
||||||
|
|
||||||
# Copy application code
|
# Copy application code
|
||||||
@@ -36,8 +29,12 @@ ENV PYTHONPATH=/app/site-packages
|
|||||||
# Set working directory
|
# Set working directory
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Create a non-root user for security
|
||||||
|
RUN adduser -D -u 1000 myice
|
||||||
|
USER myice
|
||||||
|
|
||||||
# Expose port
|
# Expose port
|
||||||
EXPOSE 8000
|
EXPOSE 8000
|
||||||
|
|
||||||
# Run the application directly with Python using the distroless entrypoint
|
# Run the application
|
||||||
ENTRYPOINT ["/usr/bin/python3", "-m", "uvicorn", "myice.webapi:app", "--host", "0.0.0.0", "--port", "8000"]
|
ENTRYPOINT ["python", "-m", "uvicorn", "myice.webapi:app", "--host", "0.0.0.0", "--port", "8000"]
|
||||||
|
|||||||
Reference in New Issue
Block a user