Skip to main content
 首页 » 编程设计

python之Docker jupyter notebook 使用容器 id 作为 ip,localhost

2024年12月31日30davidwang456

我正在尝试在基于 python 3 的 Docker 中运行 jupyter 笔记本。笔记本启动但将容器 ID 显示为 ip。如果我在打开链接后在浏览器中将其更改为“localhost:”,它确实有效。

如何让它正常工作并在 url 中显示 localhost:?/操作系统:Ubuntu 18.04/

编辑:我试过 jupyter/minimal-notebook这有效,显示本地主机,但它基于 ubuntu 18.04,它与我稍后想在笔记本中使用的一些包不兼容。

docker 文件:

FROM python:3 
RUN apt-get -y update \ 
&& apt-get upgrade -y \ 
&& apt-get install -y \ 
    curl \ 
    vim \ 
    xterm 
RUN pip install --upgrade pip 
RUN pip install --no-cache-dir jupyter numpy matplotlib 
# Add Tini. Tini operates as a process subreaper for jupyter. This prevents 
# kernel crashes. 
ENV TINI_VERSION v0.6.0 
ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini /usr/bin/tini 
RUN chmod +x /usr/bin/tini 
ENTRYPOINT ["/usr/bin/tini", "--"] 
 
EXPOSE 8888 
CMD ["jupyter", "notebook", "--port=8888", "--no-browser", "--ip=0.0.0.0", "--allow-root"] 

构建后运行

docker run --rm -it -p 8888:8888 -v "$PWD":/home/work e4b37115446d 

输出:

[I 11:48:12.580 NotebookApp] Writing notebook server cookie secret to /root/.local/share/jupyter/runtime/notebook_cookie_secret 
[I 11:48:12.826 NotebookApp] Serving notebooks from local directory: / 
[I 11:48:12.826 NotebookApp] 0 active kernels 
[I 11:48:12.826 NotebookApp] The Jupyter Notebook is running at: 
[I 11:48:12.826 NotebookApp] http://3b906c2d97f3:8888/?token=06d43a78966a7b7e5fd04318183fa3e7af5bca7542477d31 
[I 11:48:12.826 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation). 
[C 11:48:12.827 NotebookApp]  
 
    Copy/paste this URL into your browser when you connect for the first time, to login with a token: 
        http://3b906c2d97f3:8888/?token=06d43a78966a7b7e5fd04318183fa3e7af5bca7542477d31&token=06d43a78966a7b7e5fd04318183fa3e7af5bca7542477d31 

请您参考如下方法:

在运行容器时使用 --hostname 标志。如果将其留空,则默认为容器名称。

docker run --rm  -it -p 8888:8888 --hostname localhost jup 

运行时:

[I 14:58:32.508 NotebookApp] The Jupyter Notebook is running at: 
[I 14:58:32.508 NotebookApp] http://localhost:8888/?token=0e59cd9003b843f529b9bc6e7b39921001ddfdb253c029e9