Skip to content

Instantly share code, notes, and snippets.

@mariusvniekerk
Last active November 7, 2024 08:07
Show Gist options
  • Save mariusvniekerk/09062bc8974e5d1f4af6 to your computer and use it in GitHub Desktop.
Save mariusvniekerk/09062bc8974e5d1f4af6 to your computer and use it in GitHub Desktop.
Jupyter Docker Kernel
FROM jupyter/demo
USER root
EXPOSE 6000 6001 6002 6003 6004
#!/usr/bin/env python
from __future__ import print_function
import sys
import subprocess
import json
import uuid
import os
from tempfile import NamedTemporaryFile, mkdtemp
kernel_file = sys.argv[1]
image_name = sys.argv[2]
if '--' in sys.argv:
docker_additional_args = sys.argv[3:sys.argv.index('--')]
command_line = sys.argv[sys.argv.index('--') + 1:]
else:
docker_additional_args = []
command_line = sys.argv[3:]
with open(kernel_file, 'r') as fp:
kernel_json = json.load(fp)
docker_args = ['docker', 'run', #'--rm',
#'-t',
#'-d',
'-p', '127.0.0.1:{0}:6000'.format(kernel_json['stdin_port']),
'-p', '127.0.0.1:{0}:6001'.format(kernel_json['control_port']),
'-p', '127.0.0.1:{0}:6002'.format(kernel_json['hb_port']),
'-p', '127.0.0.1:{0}:6003'.format(kernel_json['shell_port']),
'-p', '127.0.0.1:{0}:6004'.format(kernel_json['iopub_port']),
'-p', '127.0.0.1:{0}:6000/udp'.format(kernel_json['stdin_port']),
'-p', '127.0.0.1:{0}:6001/udp'.format(kernel_json['control_port']),
'-p', '127.0.0.1:{0}:6002/udp'.format(kernel_json['hb_port']),
'-p', '127.0.0.1:{0}:6003/udp'.format(kernel_json['shell_port']),
'-p', '127.0.0.1:{0}:6004/udp'.format(kernel_json['iopub_port']),
]
docker_args.extend(docker_additional_args)
tmpdir = mkdtemp()
try:
with NamedTemporaryFile(dir=tmpdir, suffix='.json') as fp:
print ('tmpfile:', fp.name)
print ('tmpfile:', fp.name)
new_kernel = kernel_json.copy()
new_kernel['stdin_port'] = 6000
new_kernel['control_port'] = 6001
new_kernel['hb_port'] = 6002
new_kernel['shell_port'] = 6003
new_kernel['iopub_port'] = 6004
# Inside the container we need to listen on 0.0.0.0. This should give us enough power.
# Hopefully other languages respect this and dont just do localhost
new_kernel['ip'] = '0.0.0.0'
json.dump(new_kernel, fp)
fp.flush()
os.chmod(fp.name, 0666)
docker_args.extend(['-v', tmpdir + ':' + tmpdir])
docker_args.append(image_name)
docker_args.extend(command_line)
docker_args[docker_args.index(kernel_file)] = fp.name
print("FINAL COMMANDLINE")
print(docker_args)
subprocess.check_call(docker_args, stdout=sys.stdout, stderr=sys.stderr, stdin=sys.stdin)
finally:
if os.path.exists(tmpdir):
os.rmdir(tmpdir)
{
"display_name": "Docker-Python2",
"language": "python",
"argv": [
"/home/mniekerk/docker-kernel.py",
"{connection_file}",
"jupyter/mniekerk",
"-v", "/home/mniekerk:/home/mniekerk", "--",
"/opt/conda/envs/python2/bin/python",
"-m",
"IPython.kernel",
"--debug",
"-f",
"{connection_file}"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment