How to Run Jupyter Notebooks Locally in a Docker Container

In python on 2~4 minutes
How to Run Jupyter Notebooks Locally in a Docker Container

We use Jupyter Notebook to quickly run and test simple Python scripts and entire Jupyter notebooks. But installing the Jupyter Notebook on our local PC can be quite redundant if we do not use it often. For that kind of situation, we can run Jupyter Notebook in a Docker container without installing anything, at all.

Just open a new terminal window and run the following command. If you need to access Jupyter through a different port other than default 8888, you can mention it like -p 1234:8888.

docker run -p 8888:8888 jupyter/scipy-notebook

A few seconds later, you will be able to see a URL something similar to below in the terminal. Directly open it or copy and paste the complete… URL in the browser.

[C 10:16:14.004 NotebookApp] 
    To access the notebook, open this file in a browser:
    Or copy and paste one of these URLs:

Now you should be able to use the Jupyter Notebook.

Make Jupyter Notebooks Persist With Docker Volumes

By default, the files you create with Jupyter Notebook such as ipynb and txt will NOT persist in permanent storage. That means, when you stop or remove the Jupyter Notebook Docker container, all the files you created will be permanently lost and will not be able to recover. But we can easily attach an external directory in the host as a Docker volume to the container so we can easily access the project files. For that, create a directory.

mkdir work

At this moment, the Jupyter Notebook Docker container will not be able to write files into the directory. Run the following command to change the ownership of the directory we created. After that, the Jupyter Notebook Docker container will get full permission to read and write files into the directory.

chown -R 1000 work/

Now run the following command, make sure to mention the full path to the directory you created.

docker run -p 8888:8888 -v /full/path/to/work:/home/jovyan/work jupyter/scipy-notebook

How To Fix “ModuleNotFoundError”

When you start to work with many notebooks, occasionally you might an error message similar to this.

ModuleNotFoundError: No module named 'tensorflow'

Under that kind of situation, install the necessary modules by adding them all at the top as a new cell. After running that newly added cell, make sure to restart the notebook kernel to apply changes.

conda install tensorflow keras

Visit Jupyter Docker Stacks for more information about available features.