├── .dockerignore ├── .gitignore ├── Dockerfile ├── Makefile ├── README.md ├── entrypoint.sh ├── kernel.json ├── logo-32x32.png ├── logo-64x64.png ├── sample_connection_file.json └── start-kernel.sh /.dockerignore: -------------------------------------------------------------------------------- 1 | * 2 | !entrypoint.sh 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.bu 2 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ubuntu:16.04 2 | 3 | RUN apt-get update && apt-get install -y --no-install-recommends \ 4 | jq \ 5 | python3.5 \ 6 | python3-pip \ 7 | python3-setuptools \ 8 | netcat 9 | 10 | RUN pip3 install ipython ipykernel 11 | 12 | COPY entrypoint.sh entrypoint.sh 13 | 14 | # At runtime, mount the connection file to /tmp/connection_file.json 15 | ENTRYPOINT [ "./entrypoint.sh"] 16 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | IMAGE_NAME=python-kernel 2 | build: 3 | docker build --rm -t ${IMAGE_NAME} . 4 | 5 | install: 6 | jupyter kernelspec install $(PWD) 7 | 8 | test: 9 | ./start-kernel.sh $(PWD)/sample_connection_file.json 10 | 11 | run: 12 | jupyter console --kernel ipython-kernel-docker 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # iPython Kernel in Docker 2 | 3 | Basic example of connecting to an iPython kernel running within a Docker container. 4 | 5 | ## Prerequisites 6 | 7 | - [`jq`](https://stedolan.github.io/jq/) - needed to parse the [connection file](https://jupyter-client.readthedocs.io/en/stable/kernels.html#connection-files) 8 | 9 | ## 10 Second Tutorial 10 | 11 | Build the docker image. The docker container will launch a kernel running Python3 and listen on the ports defined in the connection file. 12 | 13 | ``` 14 | make build 15 | ``` 16 | 17 | Make Jupyter aware of our kernel 18 | 19 | ``` 20 | make install 21 | ``` 22 | 23 | Launch a Jupyter Console, which will create a connection file and a container, then attach to the kernel running inside of the container. 24 | 25 | ``` 26 | make run 27 | ``` 28 | 29 | ## Debugging and Testing 30 | 31 | To start the kernel yourself and attach using the `--existing` flag, use: 32 | 33 | ``` 34 | make test 35 | ``` 36 | 37 | And then in a separate terminal, from the project directory, run 38 | 39 | ``` 40 | jupyter console --existing $(pwd)/sample_connection_file.json 41 | ``` 42 | -------------------------------------------------------------------------------- /entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ $# -lt 1 ]; then 4 | connection_file=/tmp/connection-file.json 5 | else 6 | connection_file=$1 7 | fi 8 | 9 | cat $connection_file 10 | 11 | # Modify the damn connection file to use proper IP address now 12 | sed -i 's;127.0.0.1;0.0.0.0;' ${connection_file} 13 | 14 | python3 -m ipykernel_launcher --ip=0.0.0.0 -f $connection_file 15 | -------------------------------------------------------------------------------- /kernel.json: -------------------------------------------------------------------------------- 1 | { 2 | "argv": ["./start-kernel.sh", "{connection_file}"], 3 | "display_name": "Dockerized Python 3", 4 | "language": "python" 5 | } 6 | -------------------------------------------------------------------------------- /logo-32x32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tamera-lanham/ipython-kernel-docker/72babfa2c2b8125e6700d3cc02bd7d5ba087544c/logo-32x32.png -------------------------------------------------------------------------------- /logo-64x64.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tamera-lanham/ipython-kernel-docker/72babfa2c2b8125e6700d3cc02bd7d5ba087544c/logo-64x64.png -------------------------------------------------------------------------------- /sample_connection_file.json: -------------------------------------------------------------------------------- 1 | { 2 | "kernel_name": "", 3 | "hb_port": 51115, 4 | "ip": "0.0.0.0", 5 | "control_port": 51114, 6 | "stdin_port": 51113, 7 | "transport": "tcp", 8 | "signature_scheme": "hmac-sha256", 9 | "key": "19749810-8febfa748186a01da2f7b28c", 10 | "iopub_port": 51112, 11 | "shell_port": 51111 12 | } -------------------------------------------------------------------------------- /start-kernel.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ $# -lt 1 ]; then 4 | echo "Missing the connection file as first parameter" 5 | exit 1 6 | fi 7 | 8 | # Script settings 9 | DOCKER_IMAGE=python-kernel 10 | 11 | # This thing should be a full path by Jupyter 12 | connection_file=$1 13 | 14 | # Get the port number out 15 | # Here we assume the local machine has JQ installed 16 | CONTROL_PORT=$(jq '.control_port' < $connection_file) 17 | SHELL_PORT=$(jq '.shell_port' < $connection_file) 18 | STDIN_PORT=$(jq '.stdin_port' < $connection_file) 19 | HB_PORT=$(jq '.hb_port' < $connection_file) 20 | IOPUB_PORT=$(jq '.iopub_port' < $connection_file) 21 | 22 | echo control ${CONTROL_PORT} 23 | echo shell ${SHELL_PORT} 24 | echo stdin ${STDIN_PORT} 25 | echo hb ${HB_PORT} 26 | echo iopub ${IOPUB_PORT} 27 | 28 | sed -i.bu 's;127.0.0.1;0.0.0.0;' "${connection_file}" 29 | 30 | cat $connection_file 31 | 32 | # Run docker image with the connection file mounted in, and ports forwarded 33 | docker run --rm \ 34 | -v $connection_file:/tmp/connection-file.json \ 35 | -p $CONTROL_PORT:$CONTROL_PORT \ 36 | -p $SHELL_PORT:$SHELL_PORT \ 37 | -p $STDIN_PORT:$STDIN_PORT \ 38 | -p $HB_PORT:$HB_PORT \ 39 | -p $IOPUB_PORT:$IOPUB_PORT \ 40 | $DOCKER_IMAGE 41 | --------------------------------------------------------------------------------