Skip to content

Commit f0e91c5

Browse files
authored
Merge pull request #353 from nanotaboada/feature/dockerignore
chore: add .dockerignore to optimize build context
2 parents 386f17c + 461832d commit f0e91c5

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed

.dockerignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
.DS_Store
2+
.git/
3+
.github/
4+
.pytest_cache/
5+
.venv/
6+
.vscode/
7+
htmlcov/
8+
postman-collections/
9+
.codacy.yml
10+
.coverage
11+
.coveragerc
12+
.flake8
13+
.gitignore
14+
.pylintrc
15+
CODE_OF_CONDUCT.md
16+
codecov.yml
17+
commitlint.config.mjs
18+
CONTRIBUTING.md
19+
coverage.xml
20+
LICENSE
21+
/tests/

Dockerfile

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,54 @@
1-
# - Stage 1 --------------------------------------------------------------------
1+
# - Stage 1: Build dependencies into wheels ------------------------------------
22

33
FROM python:3.12-slim-bookworm AS build
44

55
WORKDIR /app
66

7-
# Install build tools needed to compile some Python packages
7+
# Install system build tools needed to compile Python packages with native
8+
# extensions
89
RUN apt-get update && apt-get install -y --no-install-recommends \
910
build-essential gcc && \
1011
rm -rf /var/lib/apt/lists/*
1112

12-
# Copy and build all required packages (with dependencies) into wheels
13+
# Pre-build all third-party dependencies into wheel files. This enables faster,
14+
# more reliable installation later without relying on network access
1315
COPY requirements.txt .
14-
RUN pip wheel --no-cache -r requirements.txt -w /app/wheelhouse
16+
RUN pip wheel --no-cache-dir --wheel-dir=/app/wheelhouse -r requirements.txt
1517

16-
# Copy full app source (not strictly needed in build stage unless building static assets)
17-
COPY . .
18-
19-
# - Stage 2 --------------------------------------------------------------------
18+
# - Stage 2: Runtime image ----------------------------------------------------
2019

2120
FROM python:3.12-slim-bookworm AS runtime
2221

2322
WORKDIR /app
2423

25-
# Only bring in requirements and prebuilt wheels from build stage
24+
# Install dependencies from prebuilt wheels (no network access)
25+
# This improves build speed and avoids dependency drift
2626
COPY requirements.txt .
2727
COPY --from=build /app/wheelhouse /app/wheelhouse
28-
29-
# Install app dependencies from local wheelhouse
3028
RUN pip install --no-cache-dir --no-index --find-links /app/wheelhouse -r requirements.txt
3129

32-
# Copy only the necessary runtime source files
30+
# Copy only runtime-relevant application code (excluding tests and tooling)
3331
COPY models ./models
3432
COPY routes ./routes
3533
COPY schemas ./schemas
3634
COPY services ./services
3735
COPY data ./data
3836
COPY main.py .
3937

40-
# Add non-root user for security hardening
38+
# Copy README and assets needed for GHCR package page metadata
39+
COPY README.md ./
40+
COPY assets ./assets
41+
42+
# Add a non-root user for better container security
4143
RUN adduser --disabled-password --gecos '' fastapi && \
4244
chown -R fastapi:fastapi /app
4345
USER fastapi
4446

45-
# Prevent Python from buffering stdout/stderr
47+
# Ensure logs and errors appear in Docker logs immediately
4648
ENV PYTHONUNBUFFERED=1
4749

48-
# Expose FastAPI port
50+
# Expose FastAPI default port
4951
EXPOSE 9000
5052

51-
# Start the FastAPI app with Uvicorn
53+
# Start the FastAPI application using Uvicorn ASGI server
5254
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "9000"]

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ This project includes a multi-stage `Dockerfile` for local development and produ
4848
### Build the image
4949

5050
```bash
51-
docker build -t fastapi-app .
51+
docker build -t python-samples-fastapi-restful .
5252
```
5353

5454
### Run the container
5555

5656
```bash
57-
docker run -p 9000:9000 fastapi-app
57+
docker run -p 9000:9000 python-samples-fastapi-restful:latest
5858
```
5959

6060
## Credits

0 commit comments

Comments
 (0)