Jupyter Notebook on Docker
Introduction
Although I do not like using Jupyter Notebook due to its “bad” garbage collection mechanism, I still sometimes have to use it on the Docker. This blog documented the most basic protocols of connecting to the Jupyter Notebook running on the Docker on the remote server from your local computer.
Protocols
Connect Ports of Docker Container to Server
This is usually done via the -p
argument of docker run
command. Jupyter Notebook uses port 8888
by default, so we connect the port 8888
(0.0.0.0:8888
) on Docker container to the port 5000
(0.0.0.0:5000
) on the server.
1 | $ nvidia-docker run -it --name leimao-speech-instance -v /home/leimao/workspace:/workspace -p 5000:8888 -p 5001:6006 leimao/speech |
To exit the docker container while keep the container running in the background, click Ctrl
+ P
+ Ctrl
+ Q
.
Run docker container ls
to check if we have connected the port successfully.
1 | $ docker container ls |
SSH to Server
To connect the local port to the server port, in our local terminal:
1 | $ ssh -L 127.0.0.1:16006:0.0.0.0:5000 username@server |
We use the full port name because sometimes there are warnings from the server terminal if we do not do so.
Restart Docker Container
To restart the docker container, in our server terminal:
1 | $ docker start -i 05ee0d5a5a0e |
Start Jupyter Notebook
To start Jupyter notebook, go to the workspace
directory and use the following command in our docker container terminal:
1 | $ jupyter notebook --ip 0.0.0.0 --port 8888 --allow-root & |
--allow-root
argument sometimes prevents weird errors.
The &
sign is used to run Jupyter Notebook in the background.
After starting Jupyter Notebook successfully, we will receive such message:
1 | [I 13:46:05.790 NotebookApp] Serving notebooks from local directory: / |
Use Ctrl
+ P
+ Ctrl
+ Q
to exit the docker container while keep the container running in the background if necessary.
To kill Jupyter Notebook process in the background if necessary, we first check the PID (Process ID) of Jupyter Notebook using top
:
1 | $ top |
In our case, the PID of Jupyter Notebook is 49
. We could kill the process in terminal:
1 | $ kill -09 49 |
Use Jupyter Notebook Locally
Open a web browser, such as Chrome, and go to the url http://127.0.0.1:16006
. Input token 7b3ffc8481d4a664f3d44dcb418af5a2492087711adf7649
as requested. Alternatively, use the url provided in the message we saw after starting the Jupyter Notebook for the first time connection and do not forget to replace http://aa01088dd584:8888
to http://127.0.0.1:16006
.
Jupyter Notebook on Docker