笔记本在Docker容器中对Jupyter不持久



在docker容器中运行jupyter对我来说是一个很好的解决方案,但我很难像这里的文档中宣传的那样保持笔记本文件。

文档说,在会话关闭并且服务器关闭之后,.ipynb(笔记本电脑(文件应该持久存在中/工作目录,然而,对我来说,他们不是。我在根目录和Jupyter主页中显示的/work目录中都创建了笔记本,但在关闭后都找不到,如果我重新启动服务器,它们也不在目录列表中。我试着用两种方式启动容器——第一种是按照文档的建议(用最新的代替图像标签(:

docker run -p 8888:8888 jupyter/scipy-notebook:latest

其次是创建一个docker-compose.yml文件,该文件允许我捕获命令文本选项并避免令牌安全性(我不需要(,如下所示:

version: '3'
services: # jupyter notebook
jupyter_notebook:
image: jupyter/scipy-notebook
volumes:
- ./work:/work
ports:
- "8888:8888"
command: "start.sh jupyter notebook --NotebookApp.token=''"

我在Ubuntu 18.04.1 LTS和docker 18.06.1-ce下运行我希望在主机系统中找到笔记本(至少是我在/work文件夹中创建的笔记本(/工作文件夹,它在我启动docker(或docker-compose(的目录中,但什么都不存在。

以下是会议记录:

s@VC66:ls -la
-rw-r--r-- 1 steve steve  232 Nov  7 22:45 docker-compose.yml
drwxr-xr-x 2 steve steve 4096 Nov  7 21:34 work
s@VC66:~/sambashare/jupyter$ cat docker-compose.yml 
version: '3'
services:
jupyter_notebook:
image: jupyter/scipy-notebook
volumes:
- ./work:/work
ports:
- "8888:8888"
command: "start.sh jupyter notebook --NotebookApp.token=''"
s@VC66:~/sambashare/jupyter$ docker-compose up
Creating network "jupyter_default" with the default driver
Creating jupyter_jupyter_notebook_1 ... done
Attaching to jupyter_jupyter_notebook_1
jupyter_notebook_1  | Container must be run with group "root" to update passwd file
jupyter_notebook_1  | Executing the command: jupyter notebook --NotebookApp.token=
jupyter_notebook_1  | [I 16:08:40.454 NotebookApp] Writing notebook server cookie secret to /home/jovyan/.local/share/jupyter/runtime/notebook_cookie_secret
jupyter_notebook_1  | [W 16:08:40.597 NotebookApp] All authentication is disabled.  Anyone who can connect to this server will be able to run code.
jupyter_notebook_1  | [I 16:08:40.625 NotebookApp] JupyterLab extension loaded from /opt/conda/lib/python3.6/site-packages/jupyterlab
jupyter_notebook_1  | [I 16:08:40.625 NotebookApp] JupyterLab application directory is /opt/conda/share/jupyter/lab
jupyter_notebook_1  | [I 16:08:40.631 NotebookApp] Serving notebooks from local directory: /home/jovyan
jupyter_notebook_1  | [I 16:08:40.631 NotebookApp] The Jupyter Notebook is running at:
jupyter_notebook_1  | [I 16:08:40.631 NotebookApp] http://(62b087792f87 or 127.0.0.1):8888/
jupyter_notebook_1  | [I 16:08:40.631 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
jupyter_notebook_1  | [I 16:08:58.820 NotebookApp] 302 GET / (172.21.0.1) 0.48ms
jupyter_notebook_1  | [I 16:09:07.941 NotebookApp] Creating new file in /work
jupyter_notebook_1  | [I 16:09:17.360 NotebookApp] Saving file at /work/untitled.txt
jupyter_notebook_1  | [I 16:09:24.725 NotebookApp] Shutting down on /api/shutdown request.
jupyter_notebook_1  | [I 16:09:24.727 NotebookApp] Shutting down 0 kernels
jupyter_jupyter_notebook_1 exited with code 0
s@VC666:~/sambashare/jupyter$ ls work
s@VC66:~/sambashare/jupyter$ ls
docker-compose.yml  work

正如你所看到的,它说它在/work目录中保存了"untitled.txt",但在退出时,里面什么都没有。

因此,为了进一步完善这里的问题,我修改了docker-compose.yml文件,以运行一个简单的python脚本,在/work目录中创建一个文件,并查看它是否仍然存在。确实如此!

command: "python3 /work/test.py"  # rather than start.sh...

这是python test.py脚本:

import os
import pytz
from datetime import datetime
dir = "/work"
if not os.path.isdir(dir):
dir = "" # to test outside docker container...
nyc_time = datetime.now( pytz.timezone("America/New_York"))
fname = os.path.join(dir,"test.txt")
f = open(fname, 'w')
f.write(f"Test time is {nyc_time}n")
f.close()
exit()

这一次,在docker合成后,工作文件夹包含"test.txt",其中包含

测试时间为2018-11-09 11:55:28.472581-05:00

所以安装/work目录的docker容器似乎很好——问题可能是jupyter映像在关闭时所做的事情?

我认为您对使用/work的docker容器有误解。AFAIK改为/home/jovyan/work

因此,您可以通过例如此卷映射来解决您的问题

mkdir -P /your-jupyter/work
docker run -p 8888:8888 -e JUPYTER_ENABLE_LAB=yes -v /your-jupyter/work:home/jovyan/work jupyter/scipy-notebook

HTH。

最新更新