Skip to content

Commit f85116a

Browse files
Merge pull request #181 from python-discord/feat/158/multi-version
Install Multiple Python Versions in the Image
2 parents 3c10b34 + 70d0e1a commit f85116a

File tree

10 files changed

+135
-77
lines changed

10 files changed

+135
-77
lines changed

.dockerignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
!snekbox/
1010
!tests/
1111
!LICENSE
12-
!NOTICE
12+
!LICENSE-THIRD-PARTY
1313
!pyproject.toml
1414
!README.md

.github/CONTRIBUTING.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ Updating NsJail mainly involves two steps:
6262

6363
Other things to look out for are breaking changes to NsJail's config format, its command-line interface, or its logging format. Additionally, dependencies may have to be adjusted in the Dockerfile to get a new version to build or run.
6464

65+
## Adding and Updating Python Interpreters
66+
67+
Python interpreters are built using pyenv via the `scripts/build_python.sh` helper script. This script accepts a pyenv version specifier (`pyenv install --list`) and builds the interpreter in a version-specific directory under `/lang/python`. In the image, each minor version of a Python interpreter should have its own build stage and the resulting `/lang/python` directory can be copied from that stage into the `base` stage.
68+
69+
When updating a patch version (e.g. 3.11.3 to 3.11.4), edit the existing build stage in the image for the minor version (3.11); do not add a new build stage. To have access to a new version, pyenv likely needs to be updated. To do so, change the tag in the `git clone` command in the image, but only for the build stage that needs access to the new version. Updating pyenv for all build stages will just cause unnecessary build cache invalidations.
70+
71+
To change the default interpreter used by NsJail, update the target of the `/lang/python/default` symlink created in the `base` stage.
72+
6573
[readme]: ../README.md
6674
[Dockerfile]: ../Dockerfile
6775
[Compose v2]: https://docs.docker.com/compose/compose-v2/

Dockerfile

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,73 @@
1-
# syntax=docker/dockerfile:1
2-
FROM python:3.11-slim-buster as builder
1+
# syntax=docker/dockerfile:1.4
2+
FROM buildpack-deps:buster as builder-nsjail
33

44
WORKDIR /nsjail
55

66
RUN apt-get -y update \
7-
&& apt-get install -y \
8-
bison=2:3.3.* \
9-
flex=2.6.* \
10-
g++=4:8.3.* \
11-
gcc=4:8.3.* \
12-
git=1:2.20.* \
13-
libprotobuf-dev=3.6.* \
14-
libnl-route-3-dev=3.4.* \
15-
make=4.2.* \
16-
pkg-config=0.29-6 \
17-
protobuf-compiler=3.6.*
7+
&& apt-get install -y --no-install-recommends \
8+
bison\
9+
flex \
10+
libprotobuf-dev\
11+
libnl-route-3-dev \
12+
protobuf-compiler \
13+
&& rm -rf /var/lib/apt/lists/*
14+
1815
RUN git clone -b master --single-branch https://github.com/google/nsjail.git . \
1916
&& git checkout dccf911fd2659e7b08ce9507c25b2b38ec2c5800
2017
RUN make
2118

19+
# ------------------------------------------------------------------------------
20+
FROM buildpack-deps:buster as builder-py-base
21+
22+
ENV PYENV_ROOT=/pyenv \
23+
PYTHON_CONFIGURE_OPTS='--disable-test-modules --enable-optimizations \
24+
--with-lto --with-system-expat --without-ensurepip'
25+
26+
RUN apt-get -y update \
27+
&& apt-get install -y --no-install-recommends \
28+
libxmlsec1-dev \
29+
tk-dev \
30+
&& rm -rf /var/lib/apt/lists/*
31+
32+
COPY --link scripts/build_python.sh /
33+
34+
# ------------------------------------------------------------------------------
35+
FROM builder-py-base as builder-py-3_11
36+
RUN git clone -b v2.3.24 --depth 1 https://github.com/pyenv/pyenv.git $PYENV_ROOT \
37+
&& /build_python.sh 3.11.4
38+
39+
# ------------------------------------------------------------------------------
40+
FROM builder-py-base as builder-py-3_12
41+
RUN git clone -b v2.3.24 --depth 1 https://github.com/pyenv/pyenv.git $PYENV_ROOT \
42+
&& /build_python.sh 3.12.0rc1
43+
2244
# ------------------------------------------------------------------------------
2345
FROM python:3.11-slim-buster as base
2446

25-
# Everything will be a user install to allow snekbox's dependencies to be kept
26-
# separate from the packages exposed during eval.
27-
ENV PATH=/root/.local/bin:$PATH \
28-
PIP_DISABLE_PIP_VERSION_CHECK=1 \
29-
PIP_NO_CACHE_DIR=false \
30-
PIP_USER=1
47+
ENV PIP_DISABLE_PIP_VERSION_CHECK=1 \
48+
PIP_NO_CACHE_DIR=false
3149

3250
RUN apt-get -y update \
33-
&& apt-get install -y \
34-
gcc=4:8.3.* \
35-
git=1:2.20.* \
36-
libnl-route-3-200=3.4.* \
37-
libprotobuf17=3.6.* \
51+
&& apt-get install -y --no-install-recommends \
52+
gcc \
53+
git \
54+
libnl-route-3-200 \
55+
libprotobuf17 \
3856
&& rm -rf /var/lib/apt/lists/*
3957

40-
COPY --from=builder /nsjail/nsjail /usr/sbin/
41-
RUN chmod +x /usr/sbin/nsjail
58+
COPY --link --from=builder-nsjail /nsjail/nsjail /usr/sbin/
59+
COPY --link --from=builder-py-3_11 /lang/ /lang/
60+
COPY --link --from=builder-py-3_12 /lang/ /lang/
61+
62+
RUN chmod +x /usr/sbin/nsjail \
63+
&& ln -s /lang/python/3.11/ /lang/python/default
4264

4365
# ------------------------------------------------------------------------------
4466
FROM base as venv
4567

46-
COPY requirements/ /snekbox/requirements/
68+
COPY --link requirements/ /snekbox/requirements/
4769
WORKDIR /snekbox
4870

49-
# pip installs to the default user site since PIP_USER is set.
5071
RUN pip install -U -r requirements/requirements.pip
5172

5273
# This must come after the first pip command! From the docs:
@@ -58,11 +79,12 @@ ARG DEV
5879
RUN if [ -n "${DEV}" ]; \
5980
then \
6081
pip install -U -r requirements/coverage.pip \
61-
&& PYTHONUSERBASE=/snekbox/user_base pip install numpy~=1.19; \
82+
&& export PYTHONUSERBASE=/snekbox/user_base \
83+
&& /lang/python/default/bin/python -m pip install --user numpy~=1.19; \
6284
fi
6385

6486
# At the end to avoid re-installing dependencies when only a config changes.
65-
COPY config/ /snekbox/config/
87+
COPY --link config/ /snekbox/config/
6688

6789
ENTRYPOINT ["gunicorn"]
6890
CMD ["-c", "config/gunicorn.conf.py"]

NOTICE renamed to LICENSE-THIRD-PARTY

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,36 @@
1-
The Python code at snekbox/config_pb2.py was generated from config.proto in nsjail
2-
Copyright 2014 Google Inc. All Rights Reserved.
3-
Copyright 2016 Sergiusz Bazanski. All Rights Reserved.
4-
5-
-------------------------------------------------------------------------------
6-
1+
--------------------------------------------------------------------------------
2+
MIT License
3+
Applies to:
4+
- Copyright (c) 2014 Docker, Inc.
5+
- scripts/build_python.sh: find command for de-bloating Python install
6+
--------------------------------------------------------------------------------
7+
8+
Permission is hereby granted, free of charge, to any person obtaining
9+
a copy of this software and associated documentation files (the
10+
"Software"), to deal in the Software without restriction, including
11+
without limitation the rights to use, copy, modify, merge, publish,
12+
distribute, sublicense, and/or sell copies of the Software, and to
13+
permit persons to whom the Software is furnished to do so, subject to
14+
the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included
17+
in all copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26+
27+
--------------------------------------------------------------------------------
28+
Apache License, Version 2.0
29+
Applies to:
30+
- Copyright 2014 Google Inc. All Rights Reserved.
31+
Copyright 2016 Sergiusz Bazanski. All Rights Reserved.
32+
- snekbox/config_pb2.py: generated from config.proto in nsjail
33+
--------------------------------------------------------------------------------
734
Apache License
835
Version 2.0, January 2004
936
http://www.apache.org/licenses/

README.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77

88
Python sandbox runners for executing code in isolation aka snekbox.
99

10-
Supports a memory [virtual read/write file system](#virtual-file-system) within the sandbox,
11-
allowing text or binary files to be sent and returned.
10+
Supports a memory [virtual read/write file system](#virtual-file-system) within the sandbox, allowing text or binary files to be sent and returned.
1211

1312
A client sends Python code to a snekbox, the snekbox executes the code, and finally the results of the execution are returned to the client.
1413

@@ -100,22 +99,19 @@ Name | Description
10099

101100
## Third-party Packages
102101

103-
By default, the Python interpreter has no access to any packages besides the
104-
standard library. Even snekbox's own dependencies like Falcon and Gunicorn are
105-
not exposed.
102+
By default, the Python interpreter has no access to any packages besides the standard library. Even snekbox's own dependencies like Falcon and Gunicorn are not exposed.
106103

107104
To expose third-party Python packages during evaluation, install them to a custom user site:
108105

109106
```sh
110-
docker exec snekbox /bin/sh -c 'PYTHONUSERBASE=/snekbox/user_base pip install numpy'
107+
docker exec snekbox /bin/sh -c \
108+
'PYTHONUSERBASE=/snekbox/user_base /lang/python/default/bin/python -m pip install --user numpy'
111109
```
112110

113111
In the above command, `snekbox` is the name of the running container. The name may be different and can be checked with `docker ps`.
114112

115113
The packages will be installed to the user site within `/snekbox/user_base`. To persist the installed packages, a volume for the directory can be created with Docker. For an example, see [`docker-compose.yml`].
116114

117-
If `pip`, `setuptools`, or `wheel` are dependencies or need to be exposed, then use the `--ignore-installed` option with pip. However, note that this will also re-install packages present in the custom user site, effectively making caching it futile. Current limitations of pip don't allow it to ignore packages extant outside the installation destination.
118-
119115
## Development Environment
120116

121117
See [CONTRIBUTING.md](.github/CONTRIBUTING.md).

config/snekbox.cfg

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ envar: "OPENBLAS_NUM_THREADS=5"
1414
envar: "MKL_NUM_THREADS=5"
1515
envar: "VECLIB_MAXIMUM_THREADS=5"
1616
envar: "NUMEXPR_NUM_THREADS=5"
17-
envar: "PYTHONPATH=/snekbox/user_base/lib/python3.11/site-packages"
17+
envar: "PYTHONDONTWRITEBYTECODE=true"
1818
envar: "PYTHONIOENCODING=utf-8:strict"
19+
envar: "PYTHONUNBUFFERED=true"
20+
envar: "PYTHONUSERBASE=/snekbox/user_base"
1921
envar: "HOME=home"
2022

2123
keep_caps: false
@@ -79,29 +81,8 @@ mount {
7981
}
8082

8183
mount {
82-
src: "/usr/local/lib"
83-
dst: "/usr/local/lib"
84-
is_bind: true
85-
rw: false
86-
}
87-
88-
mount {
89-
src: "/usr/local/bin/python"
90-
dst: "/usr/local/bin/python"
91-
is_bind: true
92-
rw: false
93-
}
94-
95-
mount {
96-
src: "/usr/local/bin/python3"
97-
dst: "/usr/local/bin/python3"
98-
is_bind: true
99-
rw: false
100-
}
101-
102-
mount {
103-
src: "/usr/local/bin/python3.11"
104-
dst: "/usr/local/bin/python3.11"
84+
src: "/lang"
85+
dst: "/lang"
10586
is_bind: true
10687
rw: false
10788
}
@@ -116,6 +97,6 @@ cgroup_pids_mount: "/sys/fs/cgroup/pids"
11697
iface_no_lo: true
11798

11899
exec_bin {
119-
path: "/usr/local/bin/python"
120-
arg: "-BSqu"
100+
path: "/lang/python/default/bin/python"
101+
arg: ""
121102
}

deployment.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ spec:
3030
- "/bin/sh"
3131
- "-c"
3232
- >-
33-
PYTHONUSERBASE=/snekbox/user_base
34-
pip install --user --upgrade
33+
find /lang/python -mindepth 1 -maxdepth 1 -type d -exec
34+
sh -c 'PYTHONUSERBASE=/snekbox/user_base &&
35+
{}/bin/python -m pip install --user -U
3536
anyio[trio]~=3.6
3637
arrow~=1.2
3738
attrs~=22.2
@@ -55,6 +56,7 @@ spec:
5556
typing-extensions~=4.4
5657
tzdata~=2022.7
5758
yarl~=1.8
59+
' \;
5860
volumes:
5961
- name: snekbox-user-base-volume
6062
hostPath:

scripts/build_python.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env bash
2+
set -euxo pipefail
3+
shopt -s inherit_errexit
4+
5+
py_version="${1}"
6+
7+
# Install Python interpreter under e.g. /lang/python/3.11/ (no patch version).
8+
"${PYENV_ROOT}/plugins/python-build/bin/python-build" \
9+
"${py_version}" \
10+
"/lang/python/${py_version%.*}"
11+
"/lang/python/${py_version%.*}/bin/python" -m pip install -U pip
12+
13+
# Clean up some unnecessary files to reduce image size bloat.
14+
find /lang/python/ -depth \
15+
\( \
16+
\( -type d -a \( \
17+
-name test -o -name tests -o -name idle_test \
18+
\) \) \
19+
-o \( -type f -a \( \
20+
-name '*.pyc' -o -name '*.pyo' -o -name 'libpython*.a' \
21+
\) \) \
22+
\) -exec rm -rf '{}' +

snekbox/nsjail.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,9 @@ def python3(
221221
*nsjail_args,
222222
"--",
223223
self.config.exec_bin.path,
224-
*self.config.exec_bin.arg,
225-
# Filter out empty strings at start of py_args
224+
# Filter out empty strings at start of Python args
226225
# (causes issues with python cli)
226+
*iter_lstrip(self.config.exec_bin.arg),
227227
*iter_lstrip(py_args),
228228
]
229229

tests/test_nsjail.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def test_subprocess_resource_unavailable(self):
7979
for _ in range({max_pids}):
8080
print(subprocess.Popen(
8181
[
82-
'/usr/local/bin/python3',
82+
'/lang/python/default/bin/python',
8383
'-c',
8484
'import time; time.sleep(1)'
8585
],
@@ -486,7 +486,7 @@ def test_py_args(self):
486486
for args, expected in cases:
487487
with self.subTest(args=args):
488488
result = self.nsjail.python3(py_args=args)
489-
idx = result.args.index("-BSqu")
489+
idx = result.args.index(self.nsjail.config.exec_bin.path)
490490
self.assertEqual(result.args[idx + 1 :], expected)
491491
self.assertEqual(result.returncode, 0)
492492

0 commit comments

Comments
 (0)