【问题标题】:how to ssh to docker container created from one machine (centos) from another machine(centos or mac)如何从另一台机器(centos 或 mac)SSH 到从一台机器(centos)创建的 docker 容器
【发布时间】:2021-02-22 08:02:15
【问题描述】:

我想从一台机器(假设有 centos)机器创建一个 docker 容器,然后从另一台机器(可能是 centos 或 mac)访问该容器。我们怎样才能做到这一点? macvlan网络可以吗?如果是,有哪些步骤?如果不是,那是什么方法?

【问题讨论】:

  • docker run -p 选项对您有帮助吗?您现在如何构建和启动容器?为什么要特别“ssh”?
  • 我们在裸机上安装应用程序,然后在我们的公司网络上对裸机执行 ssh。因此,现在我们尝试将 app/run 安装到 docker 容器而不是裸机,而不是裸机,并对其执行 ssh

标签: docker networking


【解决方案1】:

取决于你的最终目标是什么。以下是一些方法(取决于您想要实现的最终目标):

  1. 管理容器并在远程主机上的容器中执行bash

    最简单的方法是使用环境变量 DOCKER_HOST

    export DOCKER_HOST=ssh://vagrant@192.168.5.178
    docker exec -ti centos_remote /bin/bash
    

    您可以在此答案https://stackoverflow.com/a/51897942/2816703中找到更多信息

  2. 将容器用作用户可以在其上进行 ssh 的虚拟机:

    首先,您需要一个运行sshd 的容器。您将在主机网络上的另一个端口上公开端口 22。最后,您将使用ssh-p 来连接该端口。这是一个工作示例:

    $ sudo docker run -d -P --name test_sshd rastasheep/ubuntu-sshd:14.04
    $ sudo docker port test_sshd 22
      0.0.0.0:49154
    
    $ ssh root@localhost -p 49154
    # The password is `root`
    root@test_sshd $
    

    或者如果你在远程机器上,使用主机 IP 地址 xxx.xxx.xxx.xxx,连接到容器使用:

    $ ssh root@xxx.xxx.xxx.xxx -p 49154
    # The password is `root`
    

    您还可以预先选择一个端口(在本例中为端口 22000)并从主机进行测试。

    ~# docker run -d -p 22000:22 --name test_sshd rastasheep/ubuntu-sshd:14.04   
    ~# ssh root@<ipaddress> -p 22000
    
  3. 在主机之间设置网络层(L2/L3):

    使用 macvlan 是一种方法。另一种方法是ipvlan。在这两种情况下,您都将主机网络适配器转换为虚拟路由器,之后您需要设置路由。详细解释可以在这个链接http://networkstatic.net/configuring-macvlan-ipvlan-linux-networking/

【讨论】:

  • 我猜想,您正在尝试在一台机器上运行容器映像并尝试从同一台机器 ssh 到该映像。我不需要这个。我需要从另一台机器上 ssh,而不是从那台机器上。你误解了我的问题吗? point3 也在做同样的事情吗?
  • 同样适用,如果你暴露了容器的 22 端口,你可以从任何你想要的地方 ssh 到它。只需将“localhost”替换为主机的ip
  • 我按照以下步骤 1)docker run -d -P --name test_sshd rastasheep/ubuntu-sshd:14.04 90f0c64ac74b601a86ec0b6043368845f55fce8262​​a2e44225e2b20b8014fb2a 2) 690.span 端口测试.0:27 690.sshd 0:27.
  • 3)docker ps 容器 ID 图像命令已创建状态端口名称 90f0c64ac74b rastasheep/ubuntu-sshd:14.04 "/usr/sbin/sshd -D" 2 分钟前 2 分钟前 0.0.0.0:32769- >22/tcp test_sshd 4)AUX756844:~ YogeshYadav$ ssh root@10.195.1.131 -p 32769 ssh: 连接到主机 10.195.1.131 端口 49154: 连接被拒绝
  • 您的端口是“32769”,映射到 22(参见命令“0.0.0.0:32769->22”的响应)。在 -p 中使用此端口
猜你喜欢
  • 2021-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-19
  • 2021-10-15
  • 1970-01-01
  • 2014-11-02
  • 1970-01-01
相关资源
最近更新 更多