【问题标题】:SSH config file port forwarding not working as expected in Docker (compose)SSH 配置文件端口转发在 Docker 中未按预期工作(撰写)
【发布时间】:2019-01-08 13:34:23
【问题描述】:

我有一个 Docker Compose 文件,其中包含 nginx、php、redis 和一个专用于 SSH 隧道的容器。

我的 Mac 上有一个 ssh 配置文件,当我运行 ssh my-tunnel 时,它按预期工作,并且通过文件 ./ssh/config 暴露给图像

这是我的 ssh 隧道的 Dockerfile

FROM alpine:edge

RUN apk add --update openssh-client && rm -rf /var/cache/apk/*

# cagataygurturk/docker-ssh-tunnel:0.0.1
CMD rm -rf /root/.ssh && mkdir /root/.ssh && \
    cp /run/secrets/ssh_config /root/.ssh/config && \
    cp /run/secrets/ssh_key /root/.ssh/id_rsa && \
    chmod -R 600 /root/.ssh/* && \
    # see https://man.openbsd.org/ssh
    ssh -4 -o StrictHostKeyChecking=no -N $TUNNEL_HOST \
    && while true; do sleep 30; done;
EXPOSE 1-65535

这是我的 docker-compose.yaml(删节)

version: '3.1'

services:
  php:
    ...
    links:
     - ssh-tunnel

  ssh-tunnel:
    build:
      context: ./ssh
    secrets:
      - ssh_config
      - ssh_key
    environment:
      TUNNEL_HOST: my-tunnel

secrets:
  ssh_config:
    file: ./ssh/config
  ssh_key:
    file: ./ssh/id_rsa

我从这个容器得到的唯一输出是Warning: Permanently added '[IP_SPECIFIED_IN_CONFIG]:PORT_SPECIFIED_IN_CONFIG_FILE' (ECDSA) to the list of known hosts.,应用程序无法连接到隧道服务。我可以解决此问题的唯一方法是将 CMD 更改为

CMD rm -rf /root/.ssh && mkdir /root/.ssh && \
cp /run/secrets/ssh_config /root/.ssh/config && \
cp /run/secrets/ssh_key /root/.ssh/id_rsa && \
chmod -R 600 /root/.ssh/* && \
# see https://man.openbsd.org/ssh
ssh -4 -o StrictHostKeyChecking=no -N $TUNNEL_HOST \
-L *:$LOCAL_PORT_1:$REMOTE_HOST_1:$REMOTE_PORT_1 \
-L *:$LOCAL_PORT_2:$REMOTE_HOST_2:$REMOTE_PORT_2 \
&& while true; do sleep 30; done;

这并不理想,因为需要多个本地转发,否则我只会使用this container

为什么不使用命令行选项-L指定本地转发的配置不起作用?我知道正在读取配置文件(包含本地转发),因为 IP 地址已添加到 known_hosts。

netstat -an 的输出

# WORKING
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address            Foreign Address         
State
tcp        0      0 127.0.0.11:34951         0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:$LOCAT_PORT_2  0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:$LOCAL_PORT_1  0.0.0.0:*               LISTEN
tcp        0      0 172.20.0.3:34902         HostName:Port     
ESTABLISHED
udp        0      0 127.0.0.11:50743         0.0.0.0:*
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node Path

# NOT WORKING
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:$LOCAL_PORT_2   0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.11:39857        0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:$LOCAL_PORT_1   0.0.0.0:*               LISTEN
tcp        0      0 172.21.0.3:47994        Hostname:Port     ESTABLISHED
udp        0      0 127.0.0.11:42928        0.0.0.0:*
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node Path

【问题讨论】:

  • 你的问题是什么?
  • 抱歉,我措辞不好。当 CMD 手动指定本地转发时,为什么只有 `ssh -4 -o StrictHostKeyChecking=no -N $TUNNEL_HOST ` 的 Dockerfile 不起作用?

标签: docker ssh openssh alpine


【解决方案1】:

想通了,在我的 ~/.ssh/config 文件中,我指定了

LocalForward PORT PUBLIC_URL:PORT

而不是

LocalForward *:PORT PUBLIC_URL:PORT

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-30
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    • 2015-04-25
    相关资源
    最近更新 更多