Skip to content

Commit ad1b8a3

Browse files
authored
Merge pull request #350 from nanotaboada/feature/wheelhouse
feat(docker): optimize multi-stage build using pre-built wheelhouse
2 parents f39576b + 890453a commit ad1b8a3

File tree

2 files changed

+59
-25
lines changed

2 files changed

+59
-25
lines changed

Dockerfile

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,52 @@
1-
# Stage 1: Build
2-
FROM python:3.12-slim-bookworm AS build
1+
# - Stage 1 --------------------------------------------------------------------
32

4-
WORKDIR /app
3+
FROM python:3.12-slim-bookworm AS build
54

6-
COPY requirements.txt .
7-
RUN pip install --no-cache-dir -r requirements.txt
5+
WORKDIR /app
86

9-
COPY . .
7+
# Install build tools needed to compile some Python packages
8+
RUN apt-get update && apt-get install -y --no-install-recommends \
9+
build-essential gcc && \
10+
rm -rf /var/lib/apt/lists/*
1011

11-
# Stage 2: Runtime
12-
FROM python:3.12-slim-bookworm AS runtime
12+
# Copy and build all required packages (with dependencies) into wheels
13+
COPY requirements.txt .
14+
RUN pip wheel --no-cache -r requirements.txt -w /app/wheelhouse
1315

14-
WORKDIR /app
16+
# Copy full app source (not strictly needed in build stage unless building static assets)
17+
COPY . .
1518

16-
COPY requirements.txt .
17-
RUN pip install --no-cache-dir -r requirements.txt
19+
# - Stage 2 --------------------------------------------------------------------
1820

19-
COPY models ./models
20-
COPY routes ./routes
21-
COPY schemas ./schemas
22-
COPY services ./services
23-
COPY data ./data
24-
COPY main.py .
21+
FROM python:3.12-slim-bookworm AS runtime
2522

26-
# Add non-root 'fastapi' user (optional for hardening)
27-
RUN adduser --disabled-password --gecos '' fastapi \
28-
&& chown -R fastapi:fastapi /app
29-
USER fastapi
23+
WORKDIR /app
3024

31-
EXPOSE 9000
32-
ENV PYTHONUNBUFFERED=1
25+
# Only bring in requirements and prebuilt wheels from build stage
26+
COPY requirements.txt .
27+
COPY --from=build /app/wheelhouse /app/wheelhouse
3328

34-
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "9000"]
29+
# Install app dependencies from local wheelhouse
30+
RUN pip install --no-cache-dir --no-index --find-links /app/wheelhouse -r requirements.txt
31+
32+
# Copy only the necessary runtime source files
33+
COPY models ./models
34+
COPY routes ./routes
35+
COPY schemas ./schemas
36+
COPY services ./services
37+
COPY data ./data
38+
COPY main.py .
39+
40+
# Add non-root user for security hardening
41+
RUN adduser --disabled-password --gecos '' fastapi && \
42+
chown -R fastapi:fastapi /app
43+
USER fastapi
44+
45+
# Prevent Python from buffering stdout/stderr
46+
ENV PYTHONUNBUFFERED=1
47+
48+
# Expose FastAPI port
49+
EXPOSE 9000
50+
51+
# Start the FastAPI app with Uvicorn
52+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "9000"]

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,30 @@ pip install -r requirements-test.txt
3333
uvicorn main:app --reload --port 9000
3434
```
3535

36-
## Documentation
36+
## Docs
3737

3838
```console
3939
http://localhost:9000/docs
4040
```
4141

4242
![API Documentation](assets/images/swagger.png)
4343

44+
## Docker
45+
46+
This project includes a multi-stage `Dockerfile` for local development and production builds.
47+
48+
### Build the image
49+
50+
```bash
51+
docker build -t fastapi-app .
52+
```
53+
54+
### Run the container
55+
56+
```bash
57+
docker run -p 9000:9000 fastapi-app
58+
```
59+
4460
## Credits
4561

4662
The solution has been coded using [Visual Studio Code](https://code.visualstudio.com/) with the official [Python](https://marketplace.visualstudio.com/items?itemName=ms-python.python) extension.

0 commit comments

Comments
 (0)