简体   繁体   English

如何从另一台机器(centos 或 mac)从一台机器(centos)创建 ssh 到 docker 容器

[英]how to ssh to docker container created from one machine (centos) from another machine(centos or mac)

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).我想从一台机器(假设有 centos)机器创建一个 docker 容器,然后从另一台机器(可能是 centos 或 mac)访问该容器。 How can we do that?我们怎么能做到这一点? Is it possible with macvlan networking? macvlan网络可以吗? 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:管理容器并在远程主机上的容器中执行bash

    Easiest way is to use the environment variable DOCKER_HOST最简单的方法是使用环境变量 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您可以在此答案https://stackoverflow.com/a/51897942/2816703中找到更多信息

  2. Use the container as a form of virtual machine on which user can ssh:将容器用作一种虚拟机形式,用户可以在其上使用 ssh:

    First you will need a container that is running the sshd .首先,您需要一个运行sshd的容器。 You will expose the port 22 on another port on the host network.您将在主机网络上的另一个端口上公开端口 22。 Finally you will use the ssh with -p to connect that port.最后,您将使用带有-pssh来连接该端口。 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:或者如果您在远程机器上,使用主机 IP 地址 xxx.xxx.xxx.xxx 连接到容器使用:

     $ 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.您还可以预先选择一个端口(在本例中为端口 22000)并从主机进行测试。

     ~# 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:在主机之间设置网络层(L2/L3):

    Using macvlan is one approach.使用 macvlan 是一种方法。 Another approach is the ipvlan.另一种方法是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/您可以在此链接上找到详细说明http://networkstatic.net/configuring-macvlan-ipvlan-linux-networking/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过docker机器在Centos 6 docker容器中使用kvm - How to use kvm in a Centos 6 docker container via docker machine 如何从另一台机器访问Docker容器? - How to access docker container from another machine? SSH 从主机到 docker 容器 - SSH from host machine into docker container 如何将docker容器从一台机器迁移到另一台机器? - How do I migrate docker container from one machine to another machine? 从不在Docker容器和另一台机器中的其他应用程序在一台机器上调用或与Docker容器通信 - Calling or communicating with docker container on one machine from other application not in docker container and in another machine 如何从Docker容器获取远程计算机的MAC地址 - How to fetch MAC address of remote machine from docker container docker(Mac / Windows):如何从容器访问主机服务? - docker(Mac/Windows): how to access host machine service from the container? 如何从ansible控制节点(Mac机)ping docker容器 - How to ping docker container from ansible control node (Mac machine ) Docker Mac 上的桌面问题与 ssh 到 centos 容器在 localhost - Docker Desktop on Mac issue with ssh to centos container on localhost 从另一台机器控制 Docker 容器 - Controlling Docker Container from another machine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM