Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jupyter starting a kernel in a docker container?

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.

like image 978
Roelant Avatar asked Sep 02 '20 09:09

Roelant


People also ask

Does a docker container have a kernel?

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.

How do I start docker daemon inside a container?

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.


1 Answers

Full disclosure: I'm the author of Dockernel.

By using 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.

Manually

It is possible to do this kind of thing without much additional implementation/tooling, it just requires a bit of manual work:

  1. Use the following Dockerfile:
FROM python:3.7-slim-buster

RUN pip install --upgrade pip ipython ipykernel
  1. Build the image using docker build --tag my-docker-image .

  2. Create a directory for your kernelspec, e.g. ~/.local/share/jupyter/kernels/docker_test (%APPDATA%\jupyter\kernels\docker_test on Windows)

  3. 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.

like image 187
Błażej Michalik Avatar answered Oct 09 '22 00:10

Błażej Michalik