Docker-无法运行Jupyter Notebook-KeyError:'allow_remote_access'

时间:2019-01-07 04:03:30

标签: python docker jupyter-notebook

这是我第一次使用Docker。我正在尝试安装jupyter并运行ipynb文件。 我尝试了2张图片,但在两者上都遇到了相同的错误:
1)python:3.6.2-slim
2)jupyter /数据科学笔记本

Dockerfile:

#Use this python image
FROM python:3.6.2-slim
#Set metadata
LABEL maintainer="MyName"
#Set working directory to /app
WORKDIR /app
#Copy all files  in current directory to working directory (/app)
COPY . /app
#Install the required libraries
RUN pip --no-cache-dir install numpy pandas seaborn sklearn jupyter
#Make port 8888 available to the world outside this Container
EXPOSE 8888
#Run jupyter when container launches
CMD ["jupyter","notebook","--ip='*'","--port=8888","--no-browser","--allow-root"]

内部版本:

docker build -t helloworld_container .

运行:

docker run -p 9999:8888 helloworld_container 

我收到以下错误:

Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/traitlets/traitlets.py", line 528, in get
    value = obj._trait_values[self.name]
KeyError: 'allow_remote_access'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/notebook/notebookapp.py", line 869, in _default_allow_remote
    addr = ipaddress.ip_address(self.ip)
  File "/opt/conda/lib/python3.6/ipaddress.py", line 54, in ip_address
    address)
ValueError: '' does not appear to be an IPv4 or IPv6 address

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/conda/bin/jupyter-notebook", line 11, in <module>
    sys.exit(main())
  File "/opt/conda/lib/python3.6/site-packages/jupyter_core/application.py", line 266, in launch_instance
    return super(JupyterApp, cls).launch_instance(argv=argv, **kwargs)
  File "/opt/conda/lib/python3.6/site-packages/traitlets/config/application.py", line 657, in launch_instance
    app.initialize(argv)
  File "<decorator-gen-7>", line 2, in initialize
  File "/opt/conda/lib/python3.6/site-packages/traitlets/config/application.py", line 87, in catch_config_error
    return method(app, *args, **kwargs)
  File "/opt/conda/lib/python3.6/site-packages/notebook/notebookapp.py", line 1629, in initialize
    self.init_webapp()
  File "/opt/conda/lib/python3.6/site-packages/notebook/notebookapp.py", line 1379, in init_webapp
    self.jinja_environment_options,
  File "/opt/conda/lib/python3.6/site-packages/notebook/notebookapp.py", line 158, in __init__
    default_url, settings_overrides, jinja_env_options)
  File "/opt/conda/lib/python3.6/site-packages/notebook/notebookapp.py", line 251, in init_settings
    allow_remote_access=jupyter_app.allow_remote_access,
  File "/opt/conda/lib/python3.6/site-packages/traitlets/traitlets.py", line 556, in __get__
    return self.get(obj, cls)
  File "/opt/conda/lib/python3.6/site-packages/traitlets/traitlets.py", line 535, in get
    value = self._validate(obj, dynamic_default())
  File "/opt/conda/lib/python3.6/site-packages/notebook/notebookapp.py", line 872, in _default_allow_remote
    for info in socket.getaddrinfo(self.ip, self.port, 0, socket.SOCK_STREAM):
  File "/opt/conda/lib/python3.6/socket.py", line 745, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

我认为此问题的潜在解决方法是更新jupyter_notebook_config.py文件。所以我需要使用以下文件生成文件:

jupyter notebook --generate-config

我需要取消注释

#c.NotebookApp.ip = ‘*’

并将其设置为:

c.NotebookApp.ip = ‘0.0.0.0’

我知道如何在计算机上执行这些步骤,但是我不知道如何在映像上执行这些步骤。因此,我在机器上生成了配置文件,进行了更改,然后通过在dockerfile中添加以下行(并创建一个新容器)将其复制到容器中:

COPY jupyter_notebook_config.py /config/location/jupyter_notebook_config.py

这不能解决问题。

目前,我已移至已安装jupyter的其他映像(tensorflow \ tensorflow),但我想知道我在做什么错。

3 个答案:

答案 0 :(得分:1)

猜想这行,特别是"--ip='*'"是问题所在:

CMD ["jupyter","notebook","--ip='*'","--port=8888","--no-browser","--allow-root"]

我不确定docker文件如何转义,但是错误指出ip地址有误:

ValueError: '' does not appear to be an IPv4 or IPv6 address

请尝试此操作,以减少转义问题:

CMD["/bin/bash", "-lc", "jupyter notebook --ip=0.0.0.0 --port=8888 --no-browser --allow-root"]

答案 1 :(得分:0)

您已经自己发现问题,因为您发现必须将*替换为0.0.0.0。但是,除了在配置中执行此操作外,您只需在命令中执行操作即可(无需使用bash):

CMD ["jupyter", "notebook", "--ip=0.0.0.0", "--port=8888", "--no-browser", "--allow-root"]

此问题是通过notebook中的错误引入的,并在docker-stacks中进行了报告。

答案 2 :(得分:0)

我想使用Docker运行Jupyter笔记本,而我也遇到了您报告的相同问题。幸运的是,我能够找到解决方案。

注意:我正在使用write_only=True图片。

步骤

  1. 从Docker Hub中拉取图像
docker pull continuumio/anaconda3
  1. 运行以下命令(该命令提供的文档图像不起作用并生成您报告的错误)
docker run -i -t -p 8888:8888 continuumio/anaconda3 /bin/bash -c "/opt/conda/bin/conda install jupyter -y --quiet && mkdir /opt/notebooks && /opt/conda/bin/jupyter notebook --notebook-dir=/opt/notebooks --ip='0.0.0.0' --port=8888 --no-browser --allow-root"

要完成这项工作,有两个关键的修改。 continuumio/anaconda3,使用--ip='0.0.0.0'代替--ip='*'。其次,添加--allow-root以查看浏览器中正在运行的笔记本。

在临时容器中添加卷

如果要创建一个临时容器,即暂时停止运行的容器,并且要创建一个卷,以便将在容器内创建的文件保存在主机中,请使用以下命令:

docker run --volume /path/in/your/host/jupyter_notebooks:/opt/notebooks --rm -it -p 8888:8888 continuumio/anaconda3 /bin/bash -c "/opt/conda/bin/conda install jupyter -y --quiet && /opt/conda/bin/jupyter notebook --notebook-dir=/opt/notebooks --ip='0.0.0.0' --port=8888 --no-browser --allow-root"