Docker install jupyter-notebook

简介

Jupyter Notebook 是一个开源的 Web 应用程序,允许用户创建和共享包含代码、方程式、可视化和文本的文档。主要用于 数据清理和转换数值模拟统计建模数据可视化机器学习 等等。
具有以下优势:

  • 可选择语言:支持超过 40 种编程语言,包括 PythonRJuliaScala 等。
  • 分享笔记本:可以使用电子邮件、DropboxGitHubJupyter Notebook Viewer 与他人共享。
  • 交互式输出:代码可以生成丰富的交互式输出,包括 HTML、图像、视频、LaTeX 等等。
  • 大数据整合:通过 PythonRScala 编程语言使用 Apache Spark 等大数据框架工具。支持使用 pandasscikit-learnggplot2TensorFlow 来探索同一份数据。

安装

查找镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
$ docker search jupyter
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
jupyter/scipy-notebook Scientific Jupyter Notebook Python Stack fro… 404
jupyter/tensorflow-notebook Scientific Jupyter Notebook Python Stack w/ … 344
jupyter/all-spark-notebook Python, Scala, R and Spark Jupyter Notebook … 417
jupyter/pyspark-notebook Python and Spark Jupyter Notebook Stack from… 277
jupyter/datascience-notebook Data Science Jupyter Notebook Python Stack f… 1027
jupyterhub/singleuser single-user docker images for use with Jupyt… 45 [OK]
jupyterhub/jupyterhub JupyterHub: multi-user Jupyter notebook serv… 326 [OK]
jupyter/minimal-notebook Minimal Jupyter Notebook Python Stack from h… 183
jupyter/base-notebook Base image for Jupyter Notebook stacks from … 203
jupyterhub/k8s-hub 22
jupyterhub/k8s-network-tools 2
jupyterhub/configurable-http-proxy node-http-proxy + REST API 6 [OK]
jupyter/nbviewer Jupyter Notebook Viewer 32 [OK]
jupyterhub/k8s-singleuser-sample 10
jupyter/r-notebook R Jupyter Notebook Stack from https://github… 54
jupyterhub/k8s-image-awaiter 2
jupyter/repo2docker Turn git repositories into Jupyter enabled D… 21
jupyterhub/k8s-secret-sync 1
jupyterhub/jupyterhub-onbuild onbuild version of JupyterHub images 6
jupyter/demo (DEPRECATED) Demo of the IPython/Jupyter Not… 16
bitnami/jupyter-base-notebook 39
jupyterhub/k8s-image-cleaner 1
jupyterhub/k8s-binderhub 3
jupyterhub/k8s-pre-puller 1
bitnami/jupyterhub 18

拉取镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
$ dockcer pull jupyter/datascience-notebook
Using default tag: latest
latest: Pulling from jupyter/datascience-notebook
d5fd17ec1767: Pull complete
9288915018bc: Pull complete
ad895732ee5c: Pull complete
4f4fb700ef54: Pull complete
7b053b0d567d: Pull complete
577d6f3bb6f4: Pull complete
1d18b5a5f242: Pull complete
ffc9ad0a0b36: Pull complete
75f3e04b1547: Pull complete
e9036ae1aec3: Pull complete
8016e50184c6: Pull complete
55f4c93ee7b8: Pull complete
17c3e54db24b: Pull complete
e8c81a9b6c9a: Pull complete
530f1db1e9d7: Pull complete
44fa9360bdc5: Pull complete
6f59df66069f: Pull complete
a8c1c1bcf1d4: Pull complete
5784e3ca1d66: Pull complete
60e2c9b0e0a4: Pull complete
4866b0f6598a: Pull complete
613fc67c0714: Pull complete
2a41639ceb55: Pull complete
fa391f2a4b79: Pull complete
Digest: sha256:acd52864dd364e2e5c494ccefa661e6f58f551c9006ec7263d7b5afd5d1852e9
Status: Downloaded newer image for jupyter/datascience-notebook:latest
docker.io/jupyter/datascience-notebook:latest

启动镜像

1
2
$ docker run -d -p 8000:8888 –name jupyter-notebook jupyter/datascience-notebook
xxxxxxxxx

部署代理

服务部署完毕后,通过 Nginx 反向代理到公网访问,下面是示例配置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
server {
listen 80;
server_name jupyter.vgbhfive.com;
index index.html;

location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host http://127.0.0.1:8000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

location ~ /api/kernels/ {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;

proxy_http_version 1.1; # websocket support
proxy_set_header Upgrade "websocket";
proxy_set_header Connection "Upgrade";
proxy_read_timeout 86400;
}
location ~ /terminals/ {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;

proxy_http_version 1.1; # websocket support
proxy_set_header Upgrade "websocket";
proxy_set_header Connection "Upgrade";
proxy_read_timeout 86400;
}
}

页面访问

第一次访问建议通过 Token 访问并设置,之后就只需要密码即可进入。


疑问

403 GET /api/kernels

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[W 2023-06-21 08:28:50.567 ServerApp] 403 GET /api/contents/demo.ipynb/checkpoints?1687335545679 (@172.17.0.1) 1.28ms referer=http://jupyter.vgbhfive.com/lab/tree/demo.ipynb
[I 2023-06-21 08:32:19.956 ServerApp] Connecting to kernel 66e74bf7-4e19-40cc-8437-1eb43b35e208.
[I 2023-06-21 08:32:19.956 ServerApp] Restoring connection for 66e74bf7-4e19-40cc-8437-1eb43b35e208:35bb8ace-627e-4574-b476-08fae23ccaa5
[W 2023-06-21 08:33:49.957 ServerApp] WebSocket ping timeout after 90000 ms.
[I 2023-06-21 08:33:54.961 ServerApp] Starting buffering for 66e74bf7-4e19-40cc-8437-1eb43b35e208:35bb8ace-627e-4574-b476-08fae23ccaa5
[W 2023-06-21 08:34:04.639 ServerApp] wrote error: 'Forbidden'
Traceback (most recent call last):
File "/opt/conda/lib/python3.11/site-packages/tornado/web.py", line 1784, in _execute
result = method(*self.path_args, **self.path_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/conda/lib/python3.11/site-packages/tornado/web.py", line 3278, in wrapper
url = self.get_login_url()
^^^^^^^^^^^^^^^^^^^^
File "/opt/conda/lib/python3.11/site-packages/jupyter_server/base/handlers.py", line 753, in get_login_url
raise web.HTTPError(403)
tornado.web.HTTPError: HTTP 403: Forbidden

根据 403 状态码的解释,服务器已经解析请求但没有权限访问资源,那么问题就来了,在查了无数资料之后,看到了这个东西:

该问题是由于在 Nginx 代理请求时,将所有的请求代理为 HTTP 请求,而 Jupyter Notebook 的部分请求为 WebSocket,从而导致请求异常,修改 Nginx 代理之后问题就迎刃而解。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
location ~ /api/kernels/ {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;

proxy_http_version 1.1; # websocket support
proxy_set_header Upgrade "websocket";
proxy_set_header Connection "Upgrade";
proxy_read_timeout 86400;
}
location ~ /terminals/ {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;

proxy_http_version 1.1; # websocket support
proxy_set_header Upgrade "websocket";
proxy_set_header Connection "Upgrade";
proxy_read_timeout 86400;
}

引用

Jupyter Docker Image Kernel Disconnected 400 Apache Proxy


个人备注

此博客内容均为作者学习所做笔记,侵删!
若转作其他用途,请注明来源!