I want to switch my notebook easily between different kernels. One use case is to quickly test a piece of code in tensorflow 2, 2.2, 2.3, and there are many similar use cases. However I prefer to define my environments as dockers these days, rather than as different (conda) environments.
Now I know that you can start jupyter in a container, but that it not what I want. I would like to just click Kernel > use kernel > TF 2.2 (docker), and let jupyter connect to a kernel running in this container.
Is something like that around? I have used livy to connect to remote spark kernels via ssh, so it feels like this should be possible.
Docker is a popular open-source project written in go and developed by Dotcloud (A PaaS Company). It is a container engine that uses the Linux Kernel features like namespaces and control groups to create containers on top of an operating system. So you can call it OS-level virtualization.
To run docker inside docker, all you have to do it just run docker with the default Unix socket docker. sock as a volume. Just a word of caution: If your container gets access to docker. sock , it means it has more privileges over your docker daemon.
Full disclosure: I'm the author of Dockernel.
Put the following in a file called Dockerfile, in a separate directory.
FROM python:3.7-slim-buster
RUN pip install --upgrade pip ipython ipykernel
CMD python -m ipykernel_launcher -f $DOCKERNEL_CONNECTION_FILE
Then issue the following commands:
docker build --tag my-docker-image /path/to/the/dockerfile/dir
pip install dockernel
dockernel install my-docker-image
You should now see "my-docker-image" option when creating a new notebook in Jupyter.
It is possible to do this kind of thing without much additional implementation/tooling, it just requires a bit of manual work:
Dockerfile:FROM python:3.7-slim-buster
RUN pip install --upgrade pip ipython ipykernel
Build the image using docker build --tag my-docker-image .
Create a directory for your kernelspec, e.g. ~/.local/share/jupyter/kernels/docker_test (%APPDATA%\jupyter\kernels\docker_test on Windows)
Put the following kernelspec into kernel.json file in the directory you created (Windows users might need to change argv a bit)
{
 "argv": [
  "/usr/bin/docker",
  "run",
  "--network=host",
  "-v",
  "{connection_file}:/connection-spec",
  "my-docker-image",
  "python",
  "-m",
  "ipykernel_launcher",
  "-f",
  "/connection-spec"
 ],
 "display_name": "docker-test",
 "language": "python"
}
Jupyter should now be able spin up a container using the docker image specified above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With