Python 访问运行在 Docker 容器上的 Jupyter notebook

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38830610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:27:58  来源:igfitidea点击:

Access Jupyter notebook running on Docker container

pythondockerjupyter-notebook

提问by J.Guillaumin

I created a docker image with python libraries and Jupyter. I start the container with the option -p 8888:8888, to link ports between host and container. When I launch a Jupyter kernel inside the container, it is running on localhost:8888(and does not find a browser). I used the command jupyter notebook

我用 python 库和 Jupyter 创建了一个 docker 镜像。我使用选项启动容器-p 8888:8888,以链接主机和容器之间的端口。当我在容器内启动 Jupyter 内核时,它正在运行localhost:8888(并且找不到浏览器)。我使用了命令jupyter notebook

But from my host, what is the IP address I have to use to work with Jupyter in host's browser ?

但是对于我的主机,在主机浏览器中使用 Jupyter 必须使用的 IP 地址是什么?

With the command ifconfig, I find eth0, docker, wlan0, lo...

使用命令ifconfig,我发现eth0, docker, wlan0, lo...

Thanks !

谢谢 !

回答by Mateusz Moneta

You need to run your notebook on 0.0.0.0: jupyter notebook -i 0.0.0.0. Running on localhost make it available only from inside the container.

您需要在0.0.0.0:上运行您的笔记本jupyter notebook -i 0.0.0.0。在 localhost 上运行使其只能从容器内部使用。

回答by Lahiru Karunaratne

Host machine: docker run -it -p 8888:8888 image:version

主机: docker run -it -p 8888:8888 image:version

Inside the Container : jupyter notebook --ip 0.0.0.0 --no-browser --allow-root

容器内部: jupyter notebook --ip 0.0.0.0 --no-browser --allow-root

Host machine access this url : localhost:8888/tree??

主机访问此网址: localhost:8888/tree??

When you are logging in for the first time there will be a link displayed on the terminal to log on with a token.

当您第一次登录时,终端上会显示一个使用令牌登录的链接。

回答by drillep

The docker runcommand is mandatory to open a port for the container to allow the connection from a host browser, assigning the port to the docker container with -p, select your jupyter image from your docker images.

docker run命令必须为容器打开一个端口以允许来自主机浏览器的连接,使用 -p 将端口分配给 docker 容器,从docker images.

docker run -it -p 8888:8888 image:version

Inside the container launch the notebook assigning the port you opened:

在容器内启动分配您打开的端口的笔记本:

jupyter notebook --ip 0.0.0.0 --port 8888 --no-browser --allow-root

Access the notebook through your desktops browser on http://localhost:8888The notebook will prompt you for a token which was generated when you create the notebook.

通过http://localhost:8888上的桌面浏览器访问笔记本 该笔记本将提示您输入创建笔记本时生成的令牌。

回答by Josephine M. Ho

To get the link to your Jupyter notebook server:

要获取 Jupyter 笔记本服务器的链接:

After your docker runcommand, a hyperlink should be automatically generated. It looks something like this: http://localhost:8888/?token=f3a8354eb82c92f5a12399fe1835bf8f31275f917928c8d2 :: /home/jovyan/work

在您的docker run命令之后,应自动生成一个超链接。它看起来像这样:http://localhost:8888/?token=f3a8354eb82c92f5a12399fe1835bf8f31275f917928c8d2 :: /home/jovyan/work

If you want to get the link again later down the line, you can type docker exec -it <docker_container_name> jupyter notebook list.

如果您想稍后再次获取该链接,您可以输入docker exec -it <docker_container_name> jupyter notebook list.

回答by ivan7707

The below is how I get it running on Windows 7 with docker toolbox.

下面是我如何使用 docker 工具箱在 Windows 7 上运行它。

If you are using docker toolbox, open up the Docker quickstart terminal, and note the IP here:

如果您使用的是 docker 工具箱,请打开 Docker 快速入门终端,并在此处记下 IP:

docker is configured to use the default machine with IP 192.168.99.100
For help getting started, check out the docs at https://docs.docker.com

Once you run the docker commands from the tensorflow installation website:

tensorflow 安装网站运行docker命令后:

docker pull tensorflow/tensorflow                  # Download latest image
docker run -it -p 8888:8888 tensorflow/tensorflow  # Start a Jupyter notebook server

You will receive a message like this:

你会收到这样的消息:

Copy/paste this URL into your browser when you connect for the first time,
to login with a token:
    http://127.0.0.1:8888/?token=d6e80acaf08e09853dc72f6b0f022b8225f94f

In the host, replace 127.0.0.1with 192.168.99.100and use the rest of that URL

在主机中,将127.0.0.1替换为192.168.99.100并使用该 URL 的其余部分

回答by Hemant jajoo

You can use the command jupyter notebook --allow-root --ip[of your container]or give access to all ip using option --ip0.0.0.0.

您可以使用该命令jupyter notebook --allow-root --ip[of your container]或使用 option 授予对所有 ip 的访问权限--ip0.0.0.0

回答by Lukas Masuch

As an alternative to building your own Docker image, you can also use the ML Workspaceimage. The ML Workspace is an open-source web IDE that combines Jupyter, VS Code, a Desktop GUI, and many other tools & libraries into one convenient Docker image. Deploying a single workspace instance is as simple as:

作为构建您自己的 Docker 映像的替代方法,您还可以使用ML Workspace映像。ML Workspace 是一种开源 Web IDE,它将 Jupyter、VS Code、桌面 GUI 以及许多其他工具和库结合到一个方便的 Docker 映像中。部署单个工作区实例非常简单:

docker run -p 8080:8080 mltooling/ml-workspace:latest

All tools are accessible from the same port and integrated into the Jupyter UI. You can find further documentation here.

所有工具都可以从同一个端口访问并集成到 Jupyter UI 中。您可以在此处找到更多文档。

回答by abng88

Check out the Torus project that Manifoldopen sourced recently. We wanted an easy way for our ML engineers to hit the ground running on new projects with a consistent development environment across the entire team. This Python cookiecutter will scaffold out a new project structure for you that includes a Dockerfile that uses a pre-baked ML dev image that we put in Docker Hub and a Docker Compose config that takes care of all the port forwarding for you. The config is written to pick an open port on your host machine to forward to the notebook server running on 8888 inside the container. No more hassle running multiple notebook servers on your machine! Check it out hopefully this is helpful!

查看Manifold最近开源的 Torus 项目。我们想要一种简单的方法让我们的 ML 工程师在整个团队中使用一致的开发环境开始新项目的运行。这个 Python cookiecutter 将为您搭建一个新的项目结构,其中包括一个 Dockerfile,它使用我们放在 Docker Hub 中的预烘焙 ML 开发映像和一个 Docker Compose 配置,它会为您处理所有端口转发。编写配置以在主机上选择一个开放端口以转发到容器内运行在 8888 上的笔记本服务器。在您的机器上运行多个笔记本服务器不再麻烦!检查出来希望这是有帮助的!

Github repo: https://github.com/manifoldai/docker-cookiecutter-data-science

Github 存储库:https: //github.com/manifoldai/docker-cookiecutter-data-science

Why we built it (w/ demo): https://medium.com/manifold-ai/torus-a-toolkit-for-docker-first-data-science-bddcb4c97b52

为什么我们构建它(带演示):https: //medium.com/manifold-ai/torus-a-toolkit-for-docker-first-data-science-bddcb4c97b52

回答by SeekingAlpha

In the container you can run the following to make it available on your local machine (using your docker machine's ip address).

在容器中,您可以运行以下命令使其在本地机器上可用(使用 docker 机器的 ip 地址)。

jupyter notebook --ip 0.0.0.0 --allow-root

jupyter 笔记本 --ip 0.0.0.0 --allow-root

You may not need to provide the --allow-root flag depending on your container's setup.

根据容器的设置,您可能不需要提供 --allow-root 标志。