dockerfile: 2-stage
This commit is contained in:
parent
f1c41a074d
commit
d2a7f17752
32
Dockerfile
32
Dockerfile
@ -1,8 +1,9 @@
|
|||||||
FROM python:3.9-slim
|
# Build stage
|
||||||
|
FROM python:3.9-slim AS builder
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Install system dependencies
|
# Install build dependencies
|
||||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||||
build-essential \
|
build-essential \
|
||||||
&& apt-get clean \
|
&& apt-get clean \
|
||||||
@ -11,17 +12,30 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
|
|||||||
# Copy requirements file
|
# Copy requirements file
|
||||||
COPY requirements.txt .
|
COPY requirements.txt .
|
||||||
|
|
||||||
# Install Python dependencies
|
# Install dependencies into a virtual environment
|
||||||
RUN pip install --no-cache-dir -r requirements.txt
|
RUN python -m venv /opt/venv
|
||||||
|
ENV PATH="/opt/venv/bin:$PATH"
|
||||||
|
RUN pip install --no-cache-dir --upgrade pip && \
|
||||||
|
pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
# Copy application code
|
# Final stage
|
||||||
COPY . .
|
FROM python:3.9-slim
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Copy only the necessary files from the builder stage
|
||||||
|
COPY --from=builder /opt/venv /opt/venv
|
||||||
|
|
||||||
|
# Make sure we use the virtual environment
|
||||||
|
ENV PATH="/opt/venv/bin:$PATH"
|
||||||
|
ENV PYTHONUNBUFFERED=1
|
||||||
|
|
||||||
|
# Copy only the application code needed to run the service
|
||||||
|
COPY src/ ./src/
|
||||||
|
COPY main.py .
|
||||||
|
|
||||||
# Expose the port the app runs on
|
# Expose the port the app runs on
|
||||||
EXPOSE 8000
|
EXPOSE 8000
|
||||||
|
|
||||||
# Set environment variables
|
|
||||||
ENV PYTHONUNBUFFERED=1
|
|
||||||
|
|
||||||
# Command to run the application
|
# Command to run the application
|
||||||
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
|
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
|
||||||
Loading…
x
Reference in New Issue
Block a user