【问题标题】:Docker - unable to run Jupyter notebook - KeyError: 'allow_remote_access'Docker - 无法运行 Jupyter 笔记本 - KeyError:'allow_remote_access'
【发布时间】:2023-03-10 15:10:01
【问题描述】:

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

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),但我想知道我做错了什么。

【问题讨论】:

  • 我已经为您的问题添加了答案。

标签: python docker jupyter-notebook


【解决方案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"]

【讨论】:

    【解决方案2】:

    您自己已经发现了问题,因为您注意到必须将* 替换为0.0.0.0。但是,您可以在命令中简单地执行此操作,而不是在配置中执行此操作(无需使用 bash):

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

    这是通过notebook 中的错误引入的问题,并且在docker-stacks 中也报告了。

    【讨论】:

      【解决方案3】:

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

      注意:我使用的是continuumio/anaconda3 图片。

      步骤

      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"
      

      有两个重要的修改来完成这项工作。 First,使用--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"
      

      【讨论】:

        猜你喜欢
        • 2020-11-18
        • 2017-05-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-23
        • 2021-07-12
        相关资源
        最近更新 更多