如何连接到在docker容器上运行的远程Jupyter Notebook?

问题描述 投票:2回答:1

我在远程机器(amazon p2)上运行的docker容器(ubuntu)上运行Jupyter-notebook。我想通过我的浏览器(http://localhost:8883/)访问笔记本,但我得到:

open failed: connect failed: Connection refused

我的ssh配置:

Host p3
    HostName <remote machine ip>
    User <user>
    LocalForward 8883 127.0.0.1:8884

我正在运行容器(在远程计算机上):

nvidia-docker run -it -p 8884:8884 <docker image>

在我正在运行的docker容器上:

jupyter-notebook --NotebookApp.iopub_data_rate_limit=10000000000 --no-browser --port=8884 --allow-root

我能够连接到远程机器上运行的笔记本(而不是在docker容器中)。

如何从浏览器连接到docker机器上运行的笔记本?

docker ssh jupyter-notebook portforwarding ssh-tunnel
1个回答
2
投票

这是一个很好的问题。我的日常开发工作流程涉及从我的笔记本电脑上连接到在ppu加速服务器上的docker容器上运行的jupyter笔记本。

该问题分为三个部分:

  1. 容器
  2. 主办
  3. 远程

Part 1: Configuring the Container.

你需要确保你可以ssh到你的容器。我从Docker's sshd example.修改了Dockerfile你应该修改Docker示例以满足你的需求;例如您的用例是否允许您以root身份连接?

我为passwordless ssh设置了容器。请注意,您可以预先制作无密码ssh所需的文件,并在构建映像时将它们复制到相应的目录中。

如果你想稍后节省一些打字,我强烈建议你也修改你的jupyter配置文件。

Part 2: Configuring the Host

我假设你在主机上运行linux。你将不得不创建一个docker network。你应该只需要--attachable选项。我希望在我的/etc/hosts中有一个条目,让生活更轻松,例如:

172.21.0.2  container-1 container-1

您还必须在主机系统上修改/home/<user>/.ssh/config文件。

Host container-1
    Hostname container-1
    IdentityFile ~/.ssh/containers_rsa
    User root

Host tunnel-container-1
    Hostname container-1
    IdentityFile ~/.ssh/containers_rsa
    User root
    LocalForward 8888 localhost:8888
    LocalForward 8000 localhost:8000

Don't you get sick of having to type nvidia-docker? 这是/etc/docker/daemon.json的示例:

  {
    "runtimes": {
        "nvidia": {
            "path": "nvidia-container-runtime",
            "runtimeArgs": []
        }
    },
    "graph":"/docker-files"
  }

Part 3: Configuring the Local System.

你快到了。您需要能够建立到远程系统的ssh隧道。本地系统的示例~/.ssh/config

Host remote_host
    Hostname remote_host
    IdentityFile ~/.ssh/remote_host/remote_rsa

Host tunnel-remote_host
    Hostname remote_host
    IdentityFile ~/.ssh/remote_host/remote_rsa
    LocalForward 10000 localhost:8888
    LocalForward 10006 localhost:8000

一个重要的说明。我的本地/etc/hosts为我的远程主机提供了一个条目。您的实例可能没有静态IP。因此,您可以在每次启动实例时修改本地/etc/hosts,或者使用所有正确的LocalForward指令将脚本用于sshing到远程系统。

Putting it all together.

假设您的远程实例已成功启动并且Docker正常运行。此外,它假设您已经完成了我提到的便利步骤。

  1. SSH到远程。
  2. 发射码头集装箱。请注意容器名称,甚至更好地指定它。
  3. 将容器连接到网络。例如docker network connect skynet container-1。您可以通过运行docker network inspect skynet来检查您的容器是否已连接。请注意,您的网络很可能是skynet以外的其他名称。
  4. SSH到您的docker容器,例如ssh container-1
  5. 启动juypyter服务器。 jupyter notebook &。我介绍了这个过程,所以我不必打开另一个会话。
  6. 在您的远程/主机上,系统:ssh -N -f tunnel-container-1。好的,我们现在有一个从主机系统到docker容器的ssh隧道。
  7. 在您当地的系统:ssh -N -f tunnel-remote_host
  8. 在您的本地系统上导航到http://localhost:10000

如果一切正常,您应该连接到您的jupyter服务器和您在Dockerfile中指定的根目录!

How to trouble shoot.

首先确认您可以使用正确的运行时环境手动启动docker容器。确认您可以ssh并启动jupyter笔记本。是的,你将无法连接到它,但你会知道所有这些都在工作。接下来确认您可以使用容器从远程到容器建立ssh隧道。您可以使用wget确认隧道是否正常工作,以获取您的jupyter服务器的登录页面的html。接下来检查从本地到远程/主机的转发。为了使LocalForward指令的顺序正确,我花了一些试验错误。

我希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.