Running Jupyter Notebook by Docker
reference:
Prerequisites
- Having doocker or Docker Desktop installed according to your operating system.
Running
docker run --rm -p 8889:8888 quay.io/jupyter/base-notebook start-notebook.py --NotebookApp.token='my-token'
-p 8889:8888host port mapping to container portstart-notebook.py --NotebookApp.token='my-token'customizing your token.- The
start-notebook.pyscript configures the internal container and then runsjupyter labcommand (see here). - We can use the
crtl + ccommand to stop the terminal process.
Letting the container reading host folder, run:
docker run --rm -p 8889:8888 -v "$(pwd):/home/jovyan/work" quay.io/jupyter/base-notebook start-notebook.py --NotebookApp.token='my-token'
-vmaps the current working directory to/home/jovyan/workfolder of the container.- Your can edit,save and run notebooks in the container. Files will be saved on the volume except the packages that you have newly installed during the running of the container. You will have to install them everytime when restarting the container and running the code that you previously saved.
To avoid the problem, we can define an enviroment in a file named Dockerfile. Below the matplotlib and scikit-learn packages are what we want to added to the default environment:
# syntax=docker/dockerfile:1
FROM quay.io/jupyter/base-notebook
RUN pip install --no-cache-dir matplotlib scikit-learn
Then build the defined envrionment into an image:
docker build -t my-jupyter-image .
-tsets the name and tag of the image, using the current working direcotry..
More: reference.