笔记本在Docker容器中对Jupyter来说不是持久的吗?

时间:2018-11-08 04:07:58

标签: python docker jupyter-notebook

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

文档说,在关闭会话并关闭服务器后,.ipynb(笔记本)文件应保留在./work目录中,但对我而言,不是。我既在根目录中也在Jupyter主页中显示的/ work目录中创建了笔记本,但是在关机后都找不到笔记本,并且如果我重新启动服务器,它们也将不在目录列表中。我尝试了两种启动容器的方法-第一种是按照文档建议的(用最新的image标记代替):

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=''"

我正在docker 18.06.1-ce下在Ubuntu 18.04.1 LTS下运行 我期望在主机系统的./work文件夹中找到笔记本(至少是我在/ 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-compose之后,工作文件夹包含“ test.txt”,其中包含

  

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

所以挂载/ work目录的docker容器似乎还不错-问题可能出在关闭时jupyter映像正在做的事情吗?

1 个答案:

答案 0 :(得分:2)

我认为您的误解是使用/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。