Skip to content

Commit e6bf9d4

Browse files
author
Irving Popovetsky
committed
phase 7
1 parent d446a82 commit e6bf9d4

3 files changed

Lines changed: 87 additions & 36 deletions

File tree

docker/Dockerfile

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,42 @@
1-
FROM python:3.7-alpine AS base
1+
FROM python:3.12-slim AS base
22

33
FROM base AS builder
44

5-
ENV PIP_DISABLE_PIP_VERSION_CHECK=on
6-
ENV PYTHONUNBUFFERED=1
5+
ENV PIP_DISABLE_PIP_VERSION_CHECK=on \
6+
PYTHONUNBUFFERED=1 \
7+
POETRY_NO_INTERACTION=1 \
8+
POETRY_VIRTUALENVS_IN_PROJECT=true \
9+
POETRY_VIRTUALENVS_CREATE=true \
10+
POETRY_CACHE_DIR=/tmp/poetry_cache
711

8-
RUN apk update
9-
RUN apk upgrade
10-
RUN apk add --no-cache build-base musl-dev python3-dev libffi-dev openssl-dev
12+
# Install build dependencies
13+
RUN apt-get update && apt-get install -y --no-install-recommends \
14+
build-essential \
15+
&& rm -rf /var/lib/apt/lists/*
1116

12-
RUN python -m venv /opt/venv
13-
# Make sure we use the virtualenv:
14-
ENV PATH="/opt/venv/bin:$PATH"
17+
WORKDIR /app
1518

16-
COPY poetry.lock pyproject.toml ./
19+
# Install poetry
20+
RUN pip install --no-cache-dir poetry
1721

18-
RUN pip install poetry
19-
RUN poetry config virtualenvs.create false
20-
RUN poetry install --only=main --compile --no-interaction --no-cache
22+
# Copy dependency files
23+
COPY pyproject.toml poetry.lock README.md ./
24+
COPY pybot ./pybot
2125

22-
# The `built-image` stage is the base for all remaining images
23-
# Pulls all of the built dependencies from the builder stage
24-
FROM base AS built-image
25-
ENV PIP_DISABLE_PIP_VERSION_CHECK=on
26-
ENV PYTHONUNBUFFERED=1
26+
# Install dependencies into .venv in project
27+
RUN poetry install --only=main --no-interaction --no-cache && \
28+
rm -rf $POETRY_CACHE_DIR
2729

28-
RUN apk update
29-
RUN apk upgrade
30-
RUN rm -rf /var/cache/apk/*
30+
# Production image
31+
FROM base AS prod
3132

32-
# copy installed deps from builder image
33-
COPY --from=builder /opt/venv /opt/venv
33+
ENV PIP_DISABLE_PIP_VERSION_CHECK=on \
34+
PYTHONUNBUFFERED=1 \
35+
PATH="/app/.venv/bin:$PATH"
3436

35-
# Make sure we use the virtualenv
36-
ENV PATH="/opt/venv/bin:$PATH"
37+
WORKDIR /app
3738

38-
# The `app` stage is used as the base for images that don't
39-
# need the development dependencies
40-
FROM built-image AS app
39+
# Copy application and virtual environment from builder
40+
COPY --from=builder /app ./
4141

42-
COPY . /src
43-
WORKDIR /src
44-
45-
# The `prod` stage creates an image that will run the application using a
46-
# production webserver and the `environments/production.py` configuration
47-
FROM app AS prod
48-
ENTRYPOINT ["python3", "-m", "pybot"]
42+
CMD ["python", "-m", "pybot"]

docker/Dockerfile.test.py312

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# docker/Dockerfile.test.py312
2+
# For running tests in Python 3.12 after upgrade
3+
4+
FROM python:3.12-slim
5+
6+
ENV PIP_DISABLE_PIP_VERSION_CHECK=on \
7+
PYTHONUNBUFFERED=1
8+
9+
WORKDIR /app
10+
11+
# Install system dependencies
12+
RUN apt-get update && apt-get install -y --no-install-recommends \
13+
build-essential \
14+
&& rm -rf /var/lib/apt/lists/*
15+
16+
# Copy dependency files
17+
COPY pyproject.toml poetry.lock README.md ./
18+
COPY pybot ./pybot
19+
20+
# Install poetry and dependencies in venv
21+
RUN pip install --upgrade pip poetry && \
22+
python -m venv /opt/venv && \
23+
/opt/venv/bin/pip install --upgrade pip && \
24+
/opt/venv/bin/pip install poetry && \
25+
/opt/venv/bin/poetry install --no-interaction
26+
27+
# Set PATH to use venv
28+
ENV PATH="/opt/venv/bin:$PATH"
29+
30+
# Copy test files
31+
COPY tests ./tests
32+
33+
# Default command: run tests with coverage
34+
CMD ["poetry", "run", "pytest", "-v", "--tb=short"]

docs/UPGRADE_PLAN.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1203,7 +1203,30 @@ target-version = ["py311", "py312"]
12031203

12041204
---
12051205

1206-
## Phase 7: Update Dockerfile (Day 5)
1206+
## Phase 7: Update Dockerfile (Day 5) ✅ COMPLETE
1207+
1208+
> **Status**: Completed January 4, 2026
1209+
> **Result**: Docker images updated to Python 3.12, all tests passing (57/57) in containerized environment
1210+
> **Production Image**: Python 3.12.12, all dependencies verified working
1211+
> **Test Image**: Python 3.12.12, 57/57 tests passing
1212+
1213+
### Docker Best Practices Applied
1214+
1215+
Based on Poetry documentation and Docker best practices research:
1216+
1217+
1. **`POETRY_VIRTUALENVS_IN_PROJECT=true`** - Creates `.venv` in project directory
1218+
2. **Multi-stage builds** - Separate builder and production stages for smaller images
1219+
3. **Minimal base image** - Using `python:3.12-slim` instead of full Python image
1220+
4. **Build cache optimization** - Copy dependencies before code for faster rebuilds
1221+
5. **Cache cleanup** - Remove Poetry cache to reduce image size
1222+
6. **Proper PATH configuration** - Set `PATH="/app/.venv/bin:$PATH"` in production
1223+
1224+
### Fixes Applied
1225+
1226+
- Fixed `cgi` module deprecation (removed in Python 3.13)
1227+
- Replaced `asynctest` with `unittest.mock.AsyncMock`
1228+
- Fixed test fixture `loop` parameter (deprecated in pytest-asyncio 1.0+)
1229+
- Proper venv management with Poetry in Docker
12071230

12081231
### 7.1 New docker/Dockerfile
12091232

0 commit comments

Comments
 (0)