1+ # Use the Ubuntu 24.04 base image for the Python build stage
2+ FROM ubuntu:24.04 AS python-build
3+
4+ # Set environment variables
5+ ENV PYTHON_VERSION=3.13.2
6+
7+ # Install build dependencies
8+ RUN apt-get update && apt-get install -y \
9+ build-essential \
10+ zlib1g-dev \
11+ libncurses5-dev \
12+ libgdbm-dev \
13+ libnss3-dev \
14+ libssl-dev \
15+ libreadline-dev \
16+ libffi-dev \
17+ curl \
18+ libbz2-dev \
19+ libsqlite3-dev \
20+ wget \
21+ xz-utils \
22+ tk-dev \
23+ liblzma-dev \
24+ libgdbm-compat-dev
25+
26+ # Copy only the necessary files and directories for configuration and build
27+ COPY . /usr/src/python/
28+
29+ # Configure and build Python
30+ WORKDIR /usr/src/python
31+ RUN ./configure --enable-optimizations && make -j$(nproc) && make altinstall
32+
33+ # Create a virtual environment using the custom Python
34+ RUN /usr/local/bin/python3.13 -m venv /opt/venv
35+
36+ # Set the correct permissions for the virtual environment
37+ RUN chmod -R 755 /opt/venv
38+
39+ # Set the PATH environment variable to include the virtual environment
40+ ENV PATH="/opt/venv/bin:${PATH}"
41+
42+ # Use the Ubuntu 24.04 base image for the final stage
43+ FROM ubuntu:24.04
44+
45+ # Set environment variables
46+ ENV PYTHON_VERSION=3.13.2
47+
48+ # Install runtime dependencies
49+ RUN apt-get update && apt-get install -y --no-install-recommends \
50+ libbz2-dev \
51+ libsqlite3-dev \
52+ xz-utils \
53+ tk-dev \
54+ liblzma-dev \
55+ libgdbm-compat-dev && \
56+ apt-get autoremove -y && \
57+ apt-get clean && \
58+ rm -rf /var/lib/apt/lists/*
59+
60+ # Copy the Python installation and virtual environment from the build stage
61+ COPY --from=python-build /usr/local /usr/local
62+ COPY --from=python-build /opt/venv /opt/venv
63+
64+ # Set the PATH environment variable to include the virtual environment
65+ ENV PATH="/opt/venv/bin:${PATH}"
66+
67+ # Install app dependencies
68+ COPY requirements.txt /tmp/requirements.txt
69+ RUN /opt/venv/bin/pip install -r /tmp/requirements.txt
70+
71+ # Copy the application code
72+ COPY . /app
73+
74+ # Set the working directory
75+ WORKDIR /app
76+
77+ # Set the default command to run when the container starts
78+ CMD ["bash" ]
79+
80+ # Build the Docker image
81+ # docker build -t custom-python-venv:latest .
82+
83+ # Run the Docker container
84+ # docker run -it custom-python-venv:latest
85+
86+ # Run the Docker container with debugging
87+ # docker build --no-cache --progress=plain -t custom-python-venv:latest .
0 commit comments