Skip to content

Commit 34b71e0

Browse files
authored
Refactor Dockerfile for multi-stage build and add .dockerignore (cyclotruc#50)
1 parent dafe508 commit 34b71e0

File tree

2 files changed

+69
-5
lines changed

2 files changed

+69
-5
lines changed

.dockerignore

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Git
2+
.git
3+
.gitignore
4+
5+
# Python
6+
__pycache__
7+
*.pyc
8+
*.pyo
9+
*.pyd
10+
.Python
11+
env
12+
pip-log.txt
13+
pip-delete-this-directory.txt
14+
.tox
15+
.coverage
16+
.coverage.*
17+
.cache
18+
nosetests.xml
19+
coverage.xml
20+
*.cover
21+
*.log
22+
23+
# Virtual environment
24+
venv
25+
.env
26+
.venv
27+
ENV
28+
29+
# IDE
30+
.idea
31+
.vscode
32+
*.swp
33+
*.swo
34+
35+
# Project specific
36+
docs/
37+
tests/
38+
*.md
39+
LICENSE
40+
pytest.ini
41+
setup.py

Dockerfile

+28-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
1-
FROM python:3.12
1+
# Build stage
2+
FROM python:3.12-slim AS builder
3+
4+
WORKDIR /build
5+
6+
# Copy requirements first to leverage Docker cache
7+
COPY requirements.txt .
8+
9+
# Install build dependencies and Python packages
10+
RUN apt-get update \
11+
&& apt-get install -y --no-install-recommends gcc python3-dev \
12+
&& pip install --no-cache-dir --upgrade pip \
13+
&& pip install --no-cache-dir --timeout 1000 -r requirements.txt \
14+
&& rm -rf /var/lib/apt/lists/*
15+
16+
# Runtime stage
17+
FROM python:3.12-slim
18+
19+
# Set Python environment variables
20+
ENV PYTHONUNBUFFERED=1
21+
ENV PYTHONDONTWRITEBYTECODE=1
22+
23+
# Install git
24+
RUN apt-get update \
25+
&& apt-get install -y --no-install-recommends git \
26+
&& rm -rf /var/lib/apt/lists/*
227

328
WORKDIR /app
429

530
# Create a non-root user
631
RUN useradd -m -u 1000 appuser
732

33+
COPY --from=builder /usr/local/lib/python3.12/site-packages/ /usr/local/lib/python3.12/site-packages/
834
COPY src/ ./
9-
COPY requirements.txt ./
10-
11-
RUN pip install -r requirements.txt
1235

1336
# Change ownership of the application files
1437
RUN chown -R appuser:appuser /app
@@ -18,4 +41,4 @@ USER appuser
1841

1942
EXPOSE 8000
2043

21-
CMD ["uvicorn", "main:app", "--reload", "--host", "0.0.0.0"]
44+
CMD ["python", "-m", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

0 commit comments

Comments
 (0)