How to Run Jupyter Notebook in a Docker Container
Running a Jupyter Notebook in Docker is simpler than expected. No need to install anything locally (other than Docker, of course).
Assuming we have Docker installed, let’s decide which Jupyter Notebook template we want to use.
1. Choose a Jupyter Notebook template
These are the available notebook templates in the Jupyter Docker Stack.
These are ready-to-run Docker images containing Jupyter applications.
- base-notebook: small baseline environment
- datascience-notebook: environment with common data science packages
- minimal-notebook: minimalistic environment
- pyspark-notebook: environment with PySpark
- all-spark-notebook: environment with Scala, R, etc.
- r-notebook: environment with R
- scipy-notebook: environment with SciPy
- tensorflow-notebook: environment with TensorFlow
Let’s say we want to use the scipy-notebook
.
If you’re running Docker without
root
, prepend all the commands withsudo
.
2. Start the container
The following command is all we need to get a container up and running.
docker run -p 8888:8888 jupyter/scipy-notebook
However, ideally, we’ll want to edit a Jupyter Notebook that already exists, or at least save a notebook to our local machine.
This requires us to mount a directory on the host inside the container. This will expose our host directory inside the container, which can then read from and write to this directory.
We can achieve this with the -v
argument.
docker run -v $(pwd):/home/jovyan/work -p 8888:8888 jupyter/scipy-notebook
The $(pwd)
will refer to the directory in which this command is run, so anything in the current directory will be accessible inside the container. We can also specify an absolute or relative path. Either way, we’ll want our existing notebooks to reside in that location.
The /home/jovyan/work
is the starting workspace inside the Docker image.
jovyan
is a play on the wordjovian
, which means “like Jupiter”, and refers to members of the Jupyter community (source).
3. Open localhost
After running docker run
, we should see logs that look something like this.
[I 01:50:11.178 NotebookApp] Serving notebooks from local directory: /home/jovyan
[I 01:50:11.178 NotebookApp] Jupyter Notebook 6.4.5 is running at:
[I 01:50:11.178 NotebookApp] http://64fdfb2c87f1:8888/?token=d4b65f7b870928aca803efc025b914725adc4160b70b8c44
[I 01:50:11.178 NotebookApp] or http://127.0.0.1:8888/?token=d4b65f7b870928aca803efc025b914725adc4160b70b8c44
[I 01:50:11.178 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 01:50:11.191 NotebookApp]
To access the notebook, open this file in a browser:
file:///home/jovyan/.local/share/jupyter/runtime/nbserver-8-open.html
Or copy and paste one of these URLs:
http://64fdfb2c87f1:8888/?token=d4b65f7b870928aca803efc025b914725adc4160b70b8c44
or http://127.0.0.1:8888/?token=d4b65f7b870928aca803efc025b914725adc4160b70b8c44
This log contains the generated localhost URL and a token. We can easily open up the 127.0.0.1:8888
URL with the token
parameter in our browser to start using the notebook.