如何从另一台机器(centos或mac)SSH到从一台机器(centos)创建的docker容器 [英] how to ssh to docker container created from one machine (centos) from another machine(centos or mac)

查看:59
本文介绍了如何从另一台机器(centos或mac)SSH到从一台机器(centos)创建的docker容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一台机器(假设有centos)机器创建docker容器,然后从另一台机器(可能是centos或mac)访问该容器.我们该怎么做?macvlan网络可能吗?如果是,请问该如何处理?如果没有,那怎么办?

解决方案

取决于您的最终目标.以下是一些方法(取决于您要实现的最终目标):

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

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

      export DOCKER_HOST = ssh://vagrant@192.168.5.178docker 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端口test_sshd 220.0.0.0:49154$ ssh root @ localhost -p 49154#密码为"root"root @ test_sshd $ 

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

      $ ssh root@xxx.xxx.xxx.xxx -p 49154#密码为"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/

I want to create a docker container from one machine (suppose having centos) machine and then access that container from another machine(may be centos or mac). How can we do that? Is it possible with macvlan networking? If yes , what are steps? If not, what is the way?

解决方案

Depends from what is your final goal. Following are some approaches (depending on what you want to achieve as final goal):

  1. Manage container and execute bash in the container on a remote host:

    Easiest way is to use the environment variable DOCKER_HOST

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

    You can find more information in this answer https://stackoverflow.com/a/51897942/2816703

  2. Use the container as a form of virtual machine on which user can ssh:

    First you will need a container that is running the sshd. You will expose the port 22 on another port on the host network. Finally you will use the ssh with -p to connect that port. Here is a working example:

    $ 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 $
    

    or if you are on a remote machine, use the host IP address xxx.xxx.xxx.xxx, to connect to the container use:

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

    Also you can pre-select a port (in this case port 22000) and test from the host.

    ~# docker run -d -p 22000:22 --name test_sshd rastasheep/ubuntu-sshd:14.04   
    ~# ssh root@<ipaddress> -p 22000
    

  3. Setup a network layer (L2/L3) between the hosts:

    Using macvlan is one approach. Another approach is the ipvlan. In both cases, you are converting the host network adapter to a virtual router, after which you need to setup the routes. You can find detailed explanation on this link http://networkstatic.net/configuring-macvlan-ipvlan-linux-networking/

这篇关于如何从另一台机器(centos或mac)SSH到从一台机器(centos)创建的docker容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆