0%

Win10系统使用Docker安装jupyter-notebook

摘要:安装适合 win10 的 jupyter-notebook docker 镜像并访问。

前提 · 运行环境

已经在 Win10 系统开启了 WSL2,安装了 Docker 软件并完成配置,相关教程可参考 MS 官方文档:

Step1 查找镜像

使用docker search jupyter 命令查找相关镜像,按需选择,本文选择最多starjupyter/datascience-notebook

1
2
3
4
5
6
7
8
9
PS D:\hexo> docker search jupyter
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
jupyter/datascience-notebook Jupyter Notebook Data Science Stack from htt… 866
jupyter/all-spark-notebook Jupyter Notebook Python, Scala, R, Spark, Me… 362
jupyter/scipy-notebook Jupyter Notebook Scientific Python Stack fro… 317
jupyterhub/jupyterhub JupyterHub: multi-user Jupyter notebook serv… 295 [OK]
jupyter/tensorflow-notebook Jupyter Notebook Scientific Python Stack w/ … 280
jupyter/pyspark-notebook Jupyter Notebook Python, Spark, Mesos Stack … 208
jupyter/base-notebook Small base image for Jupyter Notebook stacks… 150

Step2 获取镜像

使用命令docker pull jupyter/datascience-notebook 默认获取最新版本镜像,文件比较大,可能需要一些时间下载

如果已经下载成功,运行上面的命令会告知你镜像已存在:

1
2
3
4
5
6
PS D:\hexo> docker pull jupyter/datascience-notebook
Using default tag: latest
latest: Pulling from jupyter/datascience-notebook
Digest: sha256:58d84b8c7740ae84eb4ea4d6393ba55a740eef30b2984e43ab27e2218d609f27
Status: Image is up to date for jupyter/datascience-notebook:latest
docker.io/jupyter/datascience-notebook:latest

Step3 运行镜像

使用命令docker run -p 8888:8888 -d --restart=always -v C:\jupyter:/home/jovyan/work jupyter/datascience-notebook 运行镜像创建容器

其中参数解释:

1
2
3
4
5
6
docker run [可选参数] jupyter/datascience-notebook  # 基础运行指令,[可选参数]位置放置以下各类参数

-p 8888:8888 # 将容器的8888端口 映射到主机的8888端口,格式为 -p <主机端口>:<容器端口>
-d # 后台运行,不展示log,但可能会遗漏在控制台输出的运行信息
--restart=always # 保持容器运行,中断后自动重启
-v C:\jupyter:/home/jovyan/work # 将容器工作目录挂在到自定义的windows主机文件夹,格式为: -v <主机目录>:<容器目录>

通过上面的命令运行后,控制台不展示程序运行信息,默认返回容器 ID

如:95b548a1acc423dbd325c97f92bd8e9565bb618d33e5cb3f466f37dc236f2007

1
2
PS D:\hexo> docker run -p 8888:8888 -d --restart=always -v C:\jupyter:/home/jovyan/work  jupyter/datascience-notebook
95b548a1acc423dbd325c97f92bd8e9565bb618d33e5cb3f466f37dc236f2007

也可通过docker ps 命令查看当前正在运行当中的程序,其中的容器 ID,与前面运行返回的前几位一致:

1
2
3
PS D:\hexo> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
95b548a1acc4 jupyter/datascience-notebook "tini -g -- start-no…" 13 minutes ago Up 13 minutes 0.0.0.0:8888->8888/tcp gallant_dhawan

Step4 访问链接

使用docker logs <容器id> 命令,查看控制台输出的日志信息,包含访问链接和必要的token

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
PS D:\hexo> docker logs 95b548a1acc423dbd325c97f92bd8e9565bb618d33e5cb3f466f37dc236f2007

……
[I 2021-10-02 13:08:28.266 LabApp] JupyterLab application directory is /opt/conda/share/jupyter/lab
[I 13:08:28.269 NotebookApp] Serving notebooks from local directory: /home/jovyan
[I 13:08:28.269 NotebookApp] Jupyter Notebook 6.4.4 is running at:
[I 13:08:28.269 NotebookApp] http://95b548a1acc4:8888/?token=edbb7fb28e561c3e20cdc8dfbfb1469e2ed077db2fc225ea
[I 13:08:28.269 NotebookApp] or http://127.0.0.1:8888/?token=edbb7fb28e561c3e20cdc8dfbfb1469e2ed077db2fc225ea
[I 13:08:28.270 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 13:08:28.272 NotebookApp]

To access the notebook, open this file in a browser:
file:///home/jovyan/.local/share/jupyter/runtime/nbserver-7-open.html
Or copy and paste one of these URLs:
http://95b548a1acc4:8888/?token=edbb7fb28e561c3e20cdc8dfbfb1469e2ed077db2fc225ea
or http://127.0.0.1:8888/?token=edbb7fb28e561c3e20cdc8dfbfb1469e2ed077db2fc225ea

其中,日志最后一行的http://127.0.0.1:8888/?token=edbb7fb28e561c3e20cdc8dfbfb1469e2ed077db2fc225ea 是程序访问链接,粘贴在浏览器可以直接访问,也可以在运行起来的 jupyter-notebook 首页,通过token值修改访问密码。