jupyter inside container


Image credits: NASA, ESA, and J. Nichols (University of Leicester)
Image credits: NASA, ESA, and J. Nichols (University of Leicester)

-- Why would I want to run a Jupyter notebook inside a container, when I have it installed right on my laptop?

This question is maybe the one you are asking now. Let me show you one common scenario I've faced: my OS (let's say mac OSX) is sometimes picky with some specific libraries I need to install, as requisites of some Python modules. So I need the modules, and the installation of these modules is not impossible, but painful. What do I do? Well, use a linux container, put what I need inside it, and then run a jupyter notebook from inside it, exposing my notebook on the local web browser.

This approach will save you some stress in tweaking your system libraries/configurations, and it is sustainable over time. To save your jupyter notebook simply mount a volume inside the container and work on that folder: it will persist on your machine after the container is terminated.

Here are the 3 simple steps to achieve it:

  1. Deploy the container in interactive mode, running bash , exposing a port you may like (here I'm using the common 8888). I'm also adding the --rm argument to remove the name after it terminates: docker run -it -p 8888:8888 --rm image_name bash
  2. Inside the container, run the following to make jupyter aware you don't want the browser but want a specific ip: jupyter notebook --ip --no-browser It will generate and print a token you'll need.
  3. In your local machine, outside the docker container, open your browser and put localhost:8888/tree‌​ on the url section. Here your token will be needed.

That's all! you're running your notebook from inside the container, exposing it on your local browser!

Some additional tips:

  • When plotting in the notebook, remember to use either %matplotlib notebook or %matplotlib ipympl This will make your plots more manageable... but this is personal taste.
  • To install jupyter inside your Dockerfile, use a block like this:

Hope this helps!

For ideas/comments you can tweet me or comment/message in instagram.