Skip to content

Commit 8c6f8b8

Browse files
author
codewitching
committed
use multi-stage docker build for test and runtime
1 parent 4d8c91f commit 8c6f8b8

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ jobs:
3636
run: |
3737
docker build -t flask-ci-app .
3838
39-
- name: Run tests inside Docker
39+
- name: Run tests inside Docker (test stage)
4040
run: |
41-
docker run --rm flask-ci-app pytest
41+
docker build --target test -t flask-ci-test .
42+
docker run --rm flask-ci-test
43+
44+
- name: Build production Docker image
45+
run: |
46+
docker build --target runtime -t flask-ci-app .

Dockerfile

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,34 @@
1-
FROM python:3.12-slim
1+
# ================================
2+
# Stage 1: Test Stage
3+
# ================================
4+
FROM python:3.12-slim AS test-stage
25

36
WORKDIR /app
7+
COPY requirements.txt requirements-dev.txt ./
8+
9+
RUN pip install --no-cache-dir -r requirements.txt \
10+
&& pip install --no-cache-dir -r requirements-dev.txt
411

512
COPY . .
13+
ENV PYTHONPATH=/app
14+
CMD ["pytest"]
615

7-
RUN pip install --no-cache-dir -r requirements.txt \
8-
&& pip install pytest
16+
# ================================
17+
# Stage 2: Production Stage
18+
# ================================
19+
FROM python:3.12-slim AS runtime
20+
21+
WORKDIR /app
22+
23+
COPY requirements.txt ./
24+
25+
RUN pip install --no-cache-dir -r requirements.txt
26+
27+
COPY app ./app
28+
COPY app.py ./
929

1030
ENV PYTHONPATH=/app
1131

12-
CMD ["python", "app.py"]
32+
EXPOSE 5000
33+
34+
CMD ["python", "app.py"]

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytest

0 commit comments

Comments
 (0)