Docker容器中无法访问Jupyter Notebook问题求助
问题:Docker容器中Jupyter Notebook无法访问
尝试使用Docker Hub预构建镜像yitzchak/common-lisp-jupyter运行Jupyter Notebook,浏览器无法正常访问。涉及的配置文件及日志如下:
Dockerfile
# Use the prebuilt Docker image from Docker Hub FROM yitzchak/common-lisp-jupyter # Copy the custom Jupyter configuration COPY jupyter_notebook_config.py /root/.jupyter/ # Set the working directory to /notebooks WORKDIR /notebooks # Start the Jupyter Notebook on container startup CMD ["jupyter", "notebook", "--ip=0.0.0.0"]
docker-compose.yml
version: '3.8' services: common-lisp-jupyter: build: context: . dockerfile: Dockerfile container_name: common-lisp-jupyter network_mode: host ports: - "8888:8888" tty: true volumes: - notebook-data:/notebooks image: my-common-lisp-jupyter-image volumes: notebook-data:
jupyter_notebook_config.py
# jupyter_notebook_config.py c.NotebookApp.open_browser = False
Docker日志
common-lisp-jupyter | [I 21:08:46.152 NotebookApp] Writing notebook server cookie secret to /home/app/.local/share/jupyter/runtime/notebook_cookie_secret common-lisp-jupyter | [W 2023-09-27 21:08:46.987 LabApp] 'ip' has moved from NotebookApp to ServerApp. This config will be passed to ServerApp. Be sure to update your config before our next release. common-lisp-jupyter | [W 2023-09-27 21:08:46.987 LabApp] 'ip' has moved from NotebookApp to ServerApp. This config will be passed to ServerApp. Be sure to update your config before our next release. common-lisp-jupyter | [I 2023-09-27 21:08:46.997 LabApp] JupyterLab extension loaded from /usr/lib/python3.9/site-packages/jupyterlab common-lisp-jupyter | [I 2023-09-27 21:08:46.997 LabApp] JupyterLab application directory is /home/app/.local/share/jupyter/lab common-lisp-jupyter | [I 21:08:47.003 NotebookApp] Serving notebooks from the local directory: /notebooks common-lisp-jupyter | [I 21:08:47.004 NotebookApp] Jupyter Notebook 6.3.0 is running at: common-lisp-jupyter | [I 21:08:47.004 NotebookApp] http://docker-desktop:8888/?token=4508beed261716fa50380308b533b9a2d19f190d0c9f9804 common-lisp-jupyter | [I 21:08:47.004 NotebookApp] or http://127.0.0.1:8888/?token=4508beed261716fa50380308b533b9a2d19f190d0c9f9804 common-lisp-jupyter | [I 21:08:47.004 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation). common-lisp-jupyter | [W 21:08:47.010 NotebookApp] No web browser found: could not locate a runnable browser. common-lisp-jupyter | [C 21:08:47.010 NotebookApp] common-lisp-jupyter | common-lisp-jupyter | To access the notebook, open this file in a browser: common-lisp-jupyter | file:///home/app/.local/share/jupyter/runtime/nbserver-1-open.html common-lisp-jupyter | Or copy and paste one of these URLs: common-lisp-jupyter | http://docker-desktop:8888/?token=4508beed261716fa50380308b533b9a2d19f190d0c9f9804 common-lisp-jupyter | or http://127.0.0.1:8888/?token=4508beed261716fa50380308b533b9a2d19f190d0c9f9804
尝试访问http://localhost:8888或http://127.0.0.1:8888时,浏览器无法连接。
原因分析
- 网络模式冲突:
docker-compose.yml中同时设置了network_mode: host和ports映射。在Docker Desktop(Windows/macOS)环境下,host模式并非直接使用主机网络栈,会导致端口映射失效,外部无法访问容器内的8888端口。 - 配置文件路径错误:Dockerfile将配置文件复制到
/root/.jupyter/,但从日志可知Jupyter以app用户运行,实际读取的配置目录是/home/app/.jupyter/,导致自定义配置未生效。 - Jupyter配置项过时:日志提示
'ip'配置已从NotebookApp迁移到ServerApp,旧配置项可能引发兼容性问题。
解决方案
1. 修复网络配置
移除network_mode: host,保留ports映射即可实现端口转发:
version: '3.8' services: common-lisp-jupyter: build: context: . dockerfile: Dockerfile container_name: common-lisp-jupyter ports: - "8888:8888" tty: true volumes: - notebook-data:/notebooks image: my-common-lisp-jupyter-image volumes: notebook-data:
2. 修正配置文件路径
将配置文件复制到app用户的Jupyter目录,并设置正确权限:
# Use the prebuilt Docker image from Docker Hub FROM yitzchak/common-lisp-jupyter # Copy the custom Jupyter configuration to app user's directory COPY jupyter_notebook_config.py /home/app/.jupyter/ # Ensure config file has correct ownership RUN chown app:app /home/app/.jupyter/jupyter_notebook_config.py # Set the working directory to /notebooks WORKDIR /notebooks # Start the Jupyter Notebook on container startup CMD ["jupyter", "notebook", "--ip=0.0.0.0"]
3. 更新Jupyter配置项
改用新的ServerApp配置项,避免版本兼容性问题:
# jupyter_notebook_config.py c.ServerApp.open_browser = False c.ServerApp.ip = '0.0.0.0'
4. 验证步骤
- 停止并清理现有容器:
docker-compose down - 重新构建镜像并启动服务:
docker-compose up --build - 查看日志中的访问URL(包含token),在浏览器中访问
http://localhost:8888/?token=xxx(替换xxx为日志中的实际token)
内容的提问来源于stack exchange,提问作者boss man
相关产品推荐
相关产品推荐

