简体   繁体   English

创建到运行Jupyter Notebook的远程Docker容器的ssh隧道

[英]Create ssh tunnel to remote docker container running Jupyter Notebook

I'd like to create a ssh tunnel from my computer to a remote server to a docker container running Jupyter Notebook (computer>server>Docker container) that allows me to run a Jupyter Notebook in my browser on my computer. 我想创建一条从我的计算机到远程服务器的SSH隧道,再到运行Jupyter Notebook的Docker容器(计算机>服务器> Docker容器),使我可以在计算机上的浏览器中运行Jupyter Notebook。

The Docker container is hosted on a machine running OS X (El Capitan). Docker容器托管在运行OS X(El Capitan)的计算机上。 Docker is using the default machine IP: 192.168.99.100. Docker使用默认机器IP:192.168.99.100。

$ docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER    ERRORS
default   *        virtualbox   Running   tcp://192.168.99.100:2376           v1.11.1  

I am able to physically sit at the server running the Docker container and use my browser (192.168.99.100:8888) to create Jupyter Notebooks from that Docker container. 我能够实际坐在运行Docker容器的服务器上,并使用我的浏览器(192.168.99.100:8888)从该Docker容器创建Jupyter Notebook。 This verifies that my Docker port bindings work and that I'm running the Jupyter Notebook correctly. 这可以验证我的Docker端口绑定是否正常工作,以及我是否在正确运行Jupyter Notebook。

However, I don't know how to establish a ssh tunnel from a client machine to that remote machine's Docker container and launch a Jupyter Notebook in my browser on the client machine. 但是,我不知道如何建立从客户端计算机到该远程计算机的Docker容器的ssh隧道,以及如何在客户端计算机上的浏览器中启动Jupyter Notebook。

The output from: 来自以下内容的输出:

$ docker ps

produces the following: 产生以下内容:

CONTAINER ID        IMAGE                      COMMAND             CREATED             STATUS              PORTS                              NAMES
48a8ac126c72        kubu4/bioinformatics:v11   "/bin/bash"         55 minutes ago      Up 55 minutes       8787/tcp, 0.0.0.0:8888->8888/tcp   stupefied_pasteur

My attempts at creating a ssh tunnel to the remote machine's Docker container results in the following error message in Terminal when I try to launch the Jupyter Notebook in my browser on the client machine (localhost:8888): 当我尝试在客户端计算机(localhost:8888)上的浏览器中启动Jupyter Notebook时,我尝试创建到远程计算机Docker容器的ssh隧道导致在终端中出现以下错误消息:

channel 3: open failed: connect failed: Connection refused

I'm currently using the following in my .ssh/config file to create the tunnel: 我目前在.ssh / config文件中使用以下命令创建隧道:

Host tunnel3
HostName remote.ip.address
User user
ControlMaster auto
ServerAliveInterval 30
ServerAliveCountMax 3
LocalForward localhost:8888 localhost:8888

I can use this tunneling configuration to successfully launch Jupyter Notebooks in my client browser if I run the Jupyter Notebook on the remote machine outside of the Docker container that's on the remote machine. 如果我在远程计算机上的Docker容器之外的远程计算机上运行Jupyter Notebook,则可以使用此隧道配置在客户端浏览器中成功启动Jupyter Notebook。

Just for added info, this is the output when I launch the Jupyter Notebook in the remote machine's Docker container: 仅作为补充信息,这是我在远程计算机的Docker容器中启动Jupyter Notebook时的输出:

$ jupyter notebook
[I 18:23:32.951 NotebookApp] Writing notebook server cookie secret to /root/.local/share/jupyter/runtime/notebook_cookie_secret
[I 18:23:33.072 NotebookApp] Serving notebooks from local directory: /usr/local/bioinformatics
[I 18:23:33.073 NotebookApp] 0 active kernels 
[I 18:23:33.073 NotebookApp] The Jupyter Notebook is running at: http://0.0.0.0:8888/
[I 18:23:33.074 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

I figured it out! 我想到了! The "A-ha!" “啊哈!” moment was remembering that the remote machine running Docker was OS X (El Capitan). 当时我想起运行Docker的远程机器是OS X(El Capitan)。 All my Docker builds/tests had been performed on a Linux (Ubuntu 14.04) machine. 我所有的Docker构建/测试都是在Linux(Ubuntu 14.04)机器上执行的。 The difference, it turns out, is critical to solving this problem. 事实证明,差异对于解决此问题至关重要。

Docker installs on Ubuntu allow you to use "localhost" to address the Docker container. 在Ubuntu上安装的Docker允许您使用“ localhost”来寻址Docker容器。 Docker installs on OSX generate an IP address to use to address the Docker container. 在OSX上安装的Docker会生成一个IP地址,用于寻址Docker容器。

Realizing this, I changed my ssh tunneling configuration in the.ssh/config file on my client computer. 意识到这一点,我在客户端计算机上的.ssh / config文件中更改了ssh隧道配置。

Old tunneling config: 旧的隧道配置:

Host tunnel3
HostName remote.ip.address
User user
ControlMaster auto
ServerAliveInterval 30
ServerAliveCountMax 3
LocalForward localhost:8888 localhost:8888

New tunneling config: 新的隧道配置:

Host tunnel3
HostName remote.ip.address
User user
ControlMaster auto
ServerAliveInterval 30
ServerAliveCountMax 3
LocalForward localhost:8888 192.168.99.100:8888

With this change, I can successfully create/use Jupyter Notebooks in my client browser that are actually hosted in the Docker container on the remote machine, using localhost:8888 in the URL bar. 进行此更改后,我可以使用URL栏中的localhost:8888在客户端浏览器中成功创建/使用实际上托管在远程计算机的Docker容器中的Jupyter Notebook。

Had the same problem, trying to ssh-tunnel into a google cloud instance, then into a docker container. 遇到相同的问题,尝试通过ssh-tunnel进入Google云实例,然后进入docker容器。

  • Local machine: Ubuntu (14.04) 本地计算机:Ubuntu(14.04)
  • Cloud Instance: Debian (9-stretch) 云实例:Debian(9拉伸)

Find the IP address Debian assigns to docker ( credit ): 查找Debian分配给docker的IP地址credit ):

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id

This gave me 172.18.0.2 for the first instance running, 172.18.0.3 for the second, ..0.4, ..0.5, etc. 这给了我172.18.0.2一审的第二行驶,172.18.0.3,..0.4,..0.5等。
( Note: The below didn't work if I was running multiple containers on the same instance. Since I only need to run one container, I'm not going to figure out how to fix it) 注意:如果我在同一实例上运行多个容器,则下面的命令不起作用。由于我只需要运行一个容器,因此我不会弄清楚如何修复它)

ssh into the compute instance ssh进入计算实例
Make sure ports are exposed between your Docker container and Compute instance (I used 8888:8888), then ( credit ): 确保在您的Docker容器和Compute实例之间暴露了端口(我使用8888:8888),然后暴露了( credit ):

gcloud compute ssh {stuff to your instance} -- -L 8888:172.18.0.2:8888

Run jupyter 运行jupyter

jupyter-notebook --no-browser --ip=0.0.0.0 --allow-root

Now I can open my local browser to localhost:8888/?token... and use jupyter running in a container on my gcloud instance. 现在,我可以打开本地浏览器到localhost:8888 /?token ...,并使用在gcloud实例上的容器中运行的jupyter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM