【问题标题】:Use local ssh port-forwarding from docker使用来自 docker 的本地 ssh 端口转发
【发布时间】:2020-03-21 19:27:46
【问题描述】:

所以,我正在从 dockerfile 调用一个网站(例如 https://ipinfo.io/ip)。我也想从ssh local port-forward tunnel传递这个流量。

所以我做了什么,

  1. 创建 ssh 隧道
sudo ssh -N -L 0.0.0.0:443:ipinfo.io:443  my-username@xx.xx.xx.xx
  1. /etc/hosts 中添加ipinfo
127.0.0.1 ipinfo.io
  1. 创建一个类似的 Dockerfile
FROM alpine


RUN apk add curl 

RUN curl https://ipinfo.io/ip

所以ipinfo.io 发生的事情可以在环回中解决,但不会通过 ssh 隧道。 我如何从 docker 调用 ipinfo.io,使其通过 ssh 隧道?

P.S:我使用的是 macOS High Sierra

【问题讨论】:

  • "but doesn't go through ssh tunnel" 当你尝试时究竟会发生什么?您收到错误消息吗?他们说什么?
  • 显示连接拒绝
  • 出于安全原因,禁止容器与主机通信。而是创建另一个将创建隧道的容器。有关此主题,请参阅 my answer
  • 您可以使用host network驱动程序与主机交谈。它适合您吗?
  • 已尝试使用主机网络。它不适合我。你可以试试看,如果你能解决这个问题

标签: docker ssh


【解决方案1】:

以下是我为使其正常工作所做的事情:

  1. 使用ipinfo.io 的IP 地址,而不是使用ssh 命令中的主机名
sudo ssh -g -N -L 0.0.0.0:443:216.239.38.21:443 my-username@xx.xx.xx.xx
  1. 使用下面的Dockerfile
FROM alpine


RUN apk add curl
RUN cat /etc/hosts

RUN curl -v https://ipinfo.io/ip
  1. 使用以下命令执行构建,其中我将 ipinfo.io 映射到我的本地计算机的 IP(在本例中为 docker 虚拟接口的 IP):
docker build --add-host ipinfo.io:172.17.0.1 -t test:0.0.1 .
  1. 样本输出:
Step 3/4 : RUN cat /etc/hosts
 ---> Running in b7384b27c1a5
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.1      ipinfo.io
172.17.0.2      b7384b27c1a5
Removing intermediate container b7384b27c1a5
 ---> 2a8b062984b8
Step 4/4 : RUN curl -v https://ipinfo.io/ip
 ---> Running in 0fa2c413ab2c
Trying 172.17.0.1:443...
* TCP_NODELAY set
* Connected to ipinfo.io (172.17.0.1) port 443 (#0)
...
> GET /ip HTTP/2
> Host: ipinfo.io
> User-Agent: curl/7.66.0
> Accept: */*
>
...
< HTTP/2 200
< date: Fri, 20 Mar 2020 15:27:12 GMT
< content-type: text/html; charset=utf-8
< content-length: 14
< access-control-allow-origin: *
< x-frame-options: DENY
< x-xss-protection: 1; mode=block
< x-content-type-options: nosniff
< referrer-policy: strict-origin-when-cross-origin
< via: 1.1 google
<
37.42.143.111
...

我认为可能需要在sshd_config 中启用GatewayPorts 并重新启动sshd 才能使其正常工作。

【讨论】:

  • 感谢您的回答,但它对我不起作用。我也尝试过启用GatewayPorts 。我正在使用 mac OS 的另一件事
  • 您是否从您的/etc/hosts 中删除了127.0.0.1 ipinfo.io?您也可以运行命令ifconfig 并在您的问题中分享它们以确保
  • 我已从/etc/hosts 中删除。没用。我也在使用 macOS High Sierra。你的操作系统是什么?
  • 它是 windows 上的 boot2docker tcl linux vm,那么docker build 命令的输出是什么?
  • 感谢-g 密钥。它允许从其他容器连接。
猜你喜欢
  • 2017-01-30
  • 2017-04-27
  • 2013-02-12
  • 2022-06-20
  • 2017-03-14
  • 2021-12-14
  • 2021-01-28
  • 1970-01-01
  • 2017-07-11
相关资源
最近更新 更多