Skip to content

Commit ca7f4e2

Browse files
authored
Docker image with prebuilt pyodide (pyodide#787)
1 parent 7d4fbe9 commit ca7f4e2

File tree

7 files changed

+157
-11
lines changed

7 files changed

+157
-11
lines changed

.dockerignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
emsdk/emsdk/
22
cpython/installs/
33
cpython/build/
4-
packages/
4+
packages/*/build/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
on:
2+
workflow_dispatch:
3+
inputs:
4+
version:
5+
description: 'Version of the docker image to build.
6+
The same version of the pyodide-env image must already exist'
7+
required: true
8+
9+
jobs:
10+
build_image:
11+
runs-on: ubuntu-latest
12+
# This is the recommended use of github's docker build-push action
13+
# See https://github.com/marketplace/actions/build-and-push-docker-images#git-context
14+
steps:
15+
-
16+
name: Set up QEMU
17+
uses: docker/setup-qemu-action@v1
18+
-
19+
name: Set up Docker Buildx
20+
uses: docker/setup-buildx-action@v1
21+
-
22+
name: Login to DockerHub
23+
uses: docker/login-action@v1
24+
with:
25+
username: ${{ secrets.DOCKERHUB_USERNAME }}
26+
password: ${{ secrets.DOCKERHUB_TOKEN }}
27+
-
28+
name: Build and push
29+
id: docker_build
30+
uses: docker/build-push-action@v2
31+
with:
32+
file: ./Dockerfile-prebuilt
33+
push: true
34+
build-args: |
35+
VERSION=${{ github.event.inputs.version }}
36+
tags: "iodide/pyodide:${{ github.event.inputs.version }}"
37+
-
38+
name: Image digest
39+
run: echo ${{ steps.docker_build.outputs.digest }}

Dockerfile-prebuilt

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
ARG VERSION
2+
3+
FROM iodide/pyodide-env:${VERSION}
4+
5+
USER root
6+
7+
ENV EMSDK_NUM_CORES=4 EMCC_CORES=4 PYODIDE_JOBS=4
8+
9+
COPY . pyodide
10+
11+
# the rm at the end deletes the build results such that the resulting image can still be used for building pyodide
12+
# from source (including partial and customized builds). Due to the previous run of make, builds
13+
# executed with this image will be much faster than the ones executed with pyodide-env
14+
RUN cd pyodide && make && rm -r pyodide/build

docs/building_from_sources.md

+9-4
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,21 @@ make
4545
## Using Docker
4646

4747
We provide a Debian-based Docker image on Docker Hub with the dependencies
48-
already installed to make it easier to build Pyodide. Note that building from
49-
the Docker image is *very* slow on Mac, building on the host machine is
50-
preferred if at all possible.
48+
already installed to make it easier to build Pyodide. On top of that we provide a
49+
pre-built image which can be used for fast custom and partial builds of pyodide.
50+
Note that building from the non pre-built the Docker image is *very* slow on Mac,
51+
building on the host machine is preferred if at all possible.
5152

5253
1. Install Docker
5354

54-
2. From a git checkout of Pyodide, run `./run_docker`
55+
2. From a git checkout of Pyodide, run `./run_docker` or `./run_docker --pre-built`
5556

5657
3. Run `make` to build.
5758

59+
Note: You can control the resources allocated to the build by setting the env vars
60+
`EMSDK_NUM_CORE`, `EMCC_CORES` and `PYODIDE_JOBS` (the default for each is 4).
61+
62+
5863
If running ``make`` deterministically stops at one point in each subsequent try, increasing
5964
the maximum RAM usage available to the docker container might help [This is different
6065
from the physical RAM capacity inside the system]. Ideally, at least 3 GB of RAM

run_docker

+69-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,77 @@
1-
#!/bin/sh
1+
#!/usr/bin/env bash
2+
3+
set -eo pipefail
4+
5+
6+
function usage() {
7+
cat > /dev/stdout <<EOF
8+
Usage:
9+
run_docker [OPTIONS]
10+
11+
Optional flags:
12+
-h, --help Show this information and exit
13+
--pre-built Use the prebuilt pyodide image.
14+
This is ignored if the env var PYODIDE_DOCKER_IMAGE is set
15+
-p, --port <port> System port to which to forward.
16+
This is ignored if the env var PYODIDE_SYSTEM_PORT is set
17+
18+
Prerequisites:
19+
Docker has to be set up on your system
20+
EOF
21+
}
22+
23+
function error() {
24+
usage
25+
exit 255
26+
}
27+
28+
29+
PYODIDE_IMAGE_TAG="0.16.1"
30+
DEFAULT_PYODIDE_DOCKER_IMAGE="iodide/pyodide-env:${PYODIDE_IMAGE_TAG}"
31+
DEFAULT_PYODIDE_SYSTEM_PORT="8000"
32+
33+
while [[ $# -gt 0 ]]
34+
do
35+
key="$1"
36+
case $key in
37+
-h|--help)
38+
usage
39+
exit 0
40+
;;
41+
--pre-built)
42+
if [[ -n ${PYODIDE_DOCKER_IMAGE} ]]; then
43+
echo "WARNING: will use the env var PYODIDE_DOCKER_IMAGE=${PYODIDE_DOCKER_IMAGE},
44+
the flag --pre-built has no effect"
45+
fi
46+
DEFAULT_PYODIDE_DOCKER_IMAGE="iodide/pyodide:${PYODIDE_IMAGE_TAG}"
47+
shift
48+
;;
49+
-p|--port)
50+
if [ "$#" -lt 2 ]; then
51+
>&2 echo "port cannot be empty"
52+
error
53+
fi
54+
if [[ -n ${PYODIDE_SYSTEM_PORT} ]]; then
55+
echo "WARNING: will use the env var PYODIDE_SYSTEM_PORT=${PYODIDE_SYSTEM_PORT} instead of the provided port"
56+
fi
57+
DEFAULT_PYODIDE_SYSTEM_PORT=$2
58+
shift 2
59+
;;
60+
*)
61+
>&2 echo "Unknown option $1"
62+
error
63+
;;
64+
esac
65+
done
266

367
PYODIDE_DOCKER_PORT=${PYODIDE_DOCKER_PORT:-"8000"}
4-
PYODIDE_SYSTEM_PORT=${PYODIDE_SYSTEM_PORT:-"8000"}
5-
PYODIDE_DOCKER_IMAGE=${PYODIDE_DOCKER_IMAGE:-"iodide/pyodide-env:0.16.1"}
68+
PYODIDE_SYSTEM_PORT=${PYODIDE_SYSTEM_PORT:-${DEFAULT_PYODIDE_SYSTEM_PORT}}
69+
PYODIDE_DOCKER_IMAGE=${PYODIDE_DOCKER_IMAGE:-${DEFAULT_PYODIDE_DOCKER_IMAGE}}
670

771
exec docker run \
8-
-p $PYODIDE_SYSTEM_PORT:$PYODIDE_DOCKER_PORT \
72+
-p "$PYODIDE_SYSTEM_PORT":"$PYODIDE_DOCKER_PORT" \
973
-it --rm \
1074
-v $PWD:/src \
1175
--user root -e NB_UID=$UID -e NB_GID=$GID \
12-
${PYODIDE_DOCKER_IMAGE} \
76+
"${PYODIDE_DOCKER_IMAGE}" \
1377
/bin/bash

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ addopts =
44
-r sxX
55

66
[bumpversion]
7-
current_version = 0.15.0
7+
current_version = 0.16.1
88
commit = True
99
tag = True
1010
tag_name = {new_version}

test/pyodide_build/test_run_docker.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from pathlib import Path
2+
import subprocess
3+
4+
BASE_DIR = Path(__file__).parents[2]
5+
6+
7+
def test_run_docker_script():
8+
res = subprocess.run(
9+
["bash", str(BASE_DIR / "run_docker"), "--help"],
10+
check=False,
11+
stdout=subprocess.PIPE,
12+
stderr=subprocess.PIPE,
13+
)
14+
15+
assert "Usage:\n run_docker" in res.stdout.decode("utf-8")
16+
17+
res = subprocess.run(
18+
["bash", str(BASE_DIR / "run_docker"), "--invalid-param"],
19+
check=False,
20+
stdout=subprocess.PIPE,
21+
stderr=subprocess.PIPE,
22+
)
23+
assert res.returncode > 0
24+
assert "Unknown option --invalid-param" in res.stderr.decode("utf-8")

0 commit comments

Comments
 (0)