@@ -58,51 +58,25 @@ RUN --mount=type=cache,target=/root/.npm \
5858 npm ci --production
5959
6060# ============================================================================
61- # Stage 4: Runtime - Ubuntu 22.04 with only runtime dependencies
61+ # Stage 4: Build Python 3.11.11 from source
6262# ============================================================================
63- FROM ubuntu:22.04 AS runtime
64-
65- # Accept version as build argument (passed from npm_package_version)
66- ARG SANDBOX_VERSION=unknown
63+ FROM ubuntu:22.04 AS python-builder
6764
6865# Prevent interactive prompts during package installation
6966ENV DEBIAN_FRONTEND=noninteractive
7067
71- # Set the sandbox version as an environment variable for version checking
72- ENV SANDBOX_VERSION=${SANDBOX_VERSION}
73-
74- # Install essential runtime packages with cache mounts
68+ # Install build dependencies for Python
7569RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
7670 --mount=type=cache,target=/var/lib/apt,sharing=locked \
7771 rm -f /etc/apt/apt.conf.d/docker-clean && \
7872 echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' >/etc/apt/apt.conf.d/keep-cache && \
7973 apt-get update && apt-get install -y --no-install-recommends \
80- curl \
81- wget \
82- ca-certificates \
83- procps \
84- git \
85- unzip \
86- zip \
87- jq \
88- file \
89- # Build dependencies for Python
90- build-essential \
91- ccache \
92- libssl-dev \
93- zlib1g-dev \
94- libbz2-dev \
95- libreadline-dev \
96- libsqlite3-dev \
97- libncursesw5-dev \
98- xz-utils \
99- tk-dev \
100- libxml2-dev \
101- libxmlsec1-dev \
102- libffi-dev \
103- liblzma-dev
104-
105- # Download and build Python 3.11.11 from source with ccache
74+ build-essential ccache wget ca-certificates \
75+ libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev \
76+ libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev \
77+ libffi-dev liblzma-dev
78+
79+ # Download and build Python 3.11.11 with ccache
10680RUN --mount=type=cache,target=/tmp/python-cache \
10781 --mount=type=cache,target=/root/.ccache \
10882 export PATH="/usr/lib/ccache:$PATH" && \
@@ -113,68 +87,77 @@ RUN --mount=type=cache,target=/tmp/python-cache \
11387 cp /tmp/python-cache/Python-3.11.11.tar.xz . && \
11488 tar -xf Python-3.11.11.tar.xz && \
11589 cd Python-3.11.11 && \
116- ./configure --enable-optimizations --with-lto -- prefix=/usr/local && \
90+ ./configure --prefix=/usr/local && \
11791 make -j$(nproc) && \
11892 make altinstall && \
11993 ccache --show-stats && \
12094 cd / && \
12195 rm -rf /tmp/Python-3.11.11*
12296
123- # Set Python 3.11 as default python3 and install pip
97+ # ============================================================================
98+ # Stage 5: Runtime - Ubuntu 22.04 with only runtime dependencies
99+ # ============================================================================
100+ FROM ubuntu:22.04 AS runtime
101+
102+ # Accept version as build argument (passed from npm_package_version)
103+ ARG SANDBOX_VERSION=unknown
104+
105+ # Prevent interactive prompts during package installation
106+ ENV DEBIAN_FRONTEND=noninteractive
107+
108+ # Set the sandbox version as an environment variable for version checking
109+ ENV SANDBOX_VERSION=${SANDBOX_VERSION}
110+
111+ # Install runtime packages and Python runtime libraries
112+ RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
113+ --mount=type=cache,target=/var/lib/apt,sharing=locked \
114+ rm -f /etc/apt/apt.conf.d/docker-clean && \
115+ echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' >/etc/apt/apt.conf.d/keep-cache && \
116+ apt-get update && apt-get install -y --no-install-recommends \
117+ ca-certificates curl wget procps git unzip zip jq file binutils \
118+ libssl3 zlib1g libbz2-1.0 libreadline8 libsqlite3-0 \
119+ libncursesw6 libtinfo6 libxml2 libxmlsec1 libffi8 liblzma5 libtk8.6 && \
120+ update-ca-certificates
121+
122+ # Copy compiled Python from python-builder stage
123+ COPY --from=python-builder /usr/local/bin/python3.11 /usr/local/bin/python3.11
124+ COPY --from=python-builder /usr/local/lib/python3.11 /usr/local/lib/python3.11
125+
126+ # Set Python 3.11 as default python3, install pip, and clean up
124127RUN update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.11 1 && \
125128 /usr/local/bin/python3.11 -m ensurepip && \
126- /usr/local/bin/python3.11 -m pip install --upgrade pip
129+ /usr/local/bin/python3.11 -m pip install --no-cache-dir --upgrade pip && \
130+ # Remove unnecessary Python files to reduce image size
131+ find /usr/local/lib/python3.11 -type d -name "test" -exec rm -rf {} + 2>/dev/null || true && \
132+ find /usr/local/lib/python3.11 -type d -name "tests" -exec rm -rf {} + 2>/dev/null || true && \
133+ find /usr/local/lib/python3.11 -name "*.pyc" -delete && \
134+ find /usr/local/lib/python3.11 -name "*.pyo" -delete && \
135+ find /usr/local/lib/python3.11 -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true && \
136+ # Strip debug symbols from Python binary
137+ strip /usr/local/bin/python3.11 || true
138+
139+ # Install Python packages and clean up
140+ RUN --mount=type=cache,target=/root/.cache/pip \
141+ pip3 install --no-cache-dir matplotlib numpy pandas ipython && \
142+ # Remove test files and caches from installed packages
143+ find /usr/local/lib/python3.11/site-packages -type d -name "test" -exec rm -rf {} + 2>/dev/null || true && \
144+ find /usr/local/lib/python3.11/site-packages -type d -name "tests" -exec rm -rf {} + 2>/dev/null || true && \
145+ find /usr/local/lib/python3.11/site-packages -name "*.pyc" -delete && \
146+ find /usr/local/lib/python3.11/site-packages -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true && \
147+ # Strip debug symbols from .so files
148+ find /usr/local/lib/python3.11/site-packages -name "*.so" -exec strip {} \; 2>/dev/null || true && \
149+ # Remove binutils after stripping
150+ apt-get remove -y binutils && apt-get autoremove -y
127151
128152# Install Node.js 20 LTS using official NodeSource setup script
129- RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
130- && apt-get install -y nodejs \
131- && rm -rf /var/lib/apt/lists/*
153+ RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
154+ --mount=type=cache,target=/var/lib/apt,sharing=locked \
155+ curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
156+ && apt-get install -y nodejs
132157
133158# Install Bun runtime from official image
134159COPY --from=oven/bun:1 /usr/local/bin/bun /usr/local/bin/bun
135160
136- # Install essential Python packages with cache mount
137- RUN --mount=type=cache,target=/root/.cache/pip \
138- pip3 install \
139- matplotlib \
140- numpy \
141- pandas \
142- ipython
143-
144- # Mark runtime libraries as manually installed to prevent autoremove
145- RUN apt-mark manual \
146- libssl3 \
147- zlib1g \
148- libbz2-1.0 \
149- libreadline8 \
150- libsqlite3-0 \
151- libncursesw6 \
152- libtinfo6 \
153- libxml2 \
154- libxmlsec1 \
155- libffi8 \
156- liblzma5 \
157- libtk8.6
158-
159- # Remove build dependencies to reduce image size (Python and packages are already compiled)
160- RUN apt-get remove -y \
161- build-essential \
162- ccache \
163- libssl-dev \
164- zlib1g-dev \
165- libbz2-dev \
166- libreadline-dev \
167- libsqlite3-dev \
168- libncursesw5-dev \
169- xz-utils \
170- tk-dev \
171- libxml2-dev \
172- libxmlsec1-dev \
173- libffi-dev \
174- liblzma-dev && \
175- apt-get autoremove -y && \
176- apt-get clean
177-
178161# Set up runtime container server directory
179162WORKDIR /container-server
180163
0 commit comments