Docker容器如何通过主机上的SSH隧道访问(Mongo-DB)服务

发布于 2025-01-27 02:12:48 字数 493 浏览 4 评论 0原文

我正在尝试连接到限制对其本地网络访问的远程MONGO-DB实例。因此,我创建了一个SSH隧道,它允许我连接:

ssh -L [端口]:localhost:[hostport] [userName]@[remote -ip]

,但是当我想连接到来自Docker容器的同一Mongo-DB服务连接时间。

我尝试指定一个绑定地址 SO

ssh -L 172.17.0.1.1:[port] host-port] [username]@[remote-ip]

,然后从172.17.0.0.1:facter ot docker容器连接到远程mongo-db,但没有成功。我的错误是什么?

注意:我正在寻找适用于Linux和MAC的解决方案。

I'm trying to connect to a remote mongo-db instance that has restricted access to its local network. So, I create an ssh tunnel, which allows me to connect:

ssh -L [port]:localhost:[hostport] [username]@[remote-ip]

However, when I want to connect to the same mongo-db service from a docker container the connection times out.

I tried to specify a bind address like so

ssh -L 172.17.0.1:[port]:localhost:[host-port] [username]@[remote-ip]

And connect to the remote mongo-db from a docker container at 172.17.0.1:[port], but without success. What's my mistake?

Note: I am looking for a solution that works on both Linux and Mac.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

牵你手 2025-02-03 02:12:48

我建议这样的事情:

version: "3"

services:
  sshproxy:
    image: docker.io/alpine:latest
    restart: on-failure
    volumes:
      - ./id_rsa:/data/id_rsa
    command:
      - sh
      - -c
      - |
        apk add --update openssh
        chmod 700 /data
        exec ssh -N -o StrictHostkeyChecking=no -i /data/id_rsa -g -L 3128:localhost:3128 [email protected]

  client:
    image: docker.io/alpine:latest
    command:
      - sh
      - -c
      - |
        apk add --update curl
        while :; do
          curl -x http://sshproxy:3128 http://worldtimeapi.org/api/timezone/America/New_York
          sleep 5
        done

在这里,我正在设置一个SSH隧道,可访问遥控器
http代理,然后在另一个容器中访问该代理
在SSH隧道上。这几乎正​​是您要与MongoDB做的事情。

在真实的环境中,您可能会使用预构建的图像,而不是像我在此示例中那样即时安装软件包。

I am suggesting something like this:

version: "3"

services:
  sshproxy:
    image: docker.io/alpine:latest
    restart: on-failure
    volumes:
      - ./id_rsa:/data/id_rsa
    command:
      - sh
      - -c
      - |
        apk add --update openssh
        chmod 700 /data
        exec ssh -N -o StrictHostkeyChecking=no -i /data/id_rsa -g -L 3128:localhost:3128 [email protected]

  client:
    image: docker.io/alpine:latest
    command:
      - sh
      - -c
      - |
        apk add --update curl
        while :; do
          curl -x http://sshproxy:3128 http://worldtimeapi.org/api/timezone/America/New_York
          sleep 5
        done

Here I'm setting up an ssh tunnel that provides access to a remote
http proxy, and then in another container I'm accessing that proxy
over the ssh tunnel. This is pretty much exactly what you're looking to do with mongodb.

In a real environment, you would probably be using pre-built images, rather than installing packages on-the-fly as I've done in this example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文