我有一个在 EC2 主机上运行的 Docker 容器,另一个在另一个 EC2 主机上运行。如何在不提供任何端口号的情况下从一个容器 SSH 到另一个容器?我想要像这样做: ssh root@容器IP地址
我有一个在 EC2 主机上运行的 Docker 容器,另一个在另一个 EC2 主机上运行。如何在不提供任何端口号的情况下从一个容器 SSH 到另一个容器?我想要像这样做: ssh root@容器IP地址
如果您想通过端口22登录第二个容器,您需要使主机EC2 VM的ssh守护程序让路。
One way is to change your host machine's ssh port by adding an entry in /etc/ssh/sshd_config to something like 3022. Now you can use -p 22:22 when you run your docker container(s) and be able to ssh between them. However, ssh`ing the ec2 instance is on 3022.
If you would like to keep host-vms also ssh enabled on port 22 you
will then need to create a second virtual ethernet interface. This
is easy to do if you are able to set static IPs. something like
ifconfig eth0:0 192.168.1.11 up
. However, in ec2 this won't be
possible as you have DHCP based IPs.
The third way is to setup your .ssh/config file to map to the non standard port. It does not allow you to ssh over port 22 but at least you don't have to know about the non-standard port. Here is a tutorial, and relevant parts are below.
# contents of $HOME/.ssh/config
Host other_docker
HostName ec2-host-name-of-other-docker.com
Port 22000
User some_user
# must be added to authorized keys on other docker host for some_user
IdentityFile ~/.ssh/this-docker-private-key
ssh other_docker
。Open vswitch是最简单的解决方案! - https://goldmann.pl/blog/2014/01/21/connecting-docker-containers-on-multiple-hosts/
ssh hostUser@xxx.xxx.xxx.xxx 'ssh containerUser@xxx.xxx.xxx.xxx'
,使用 ssh 的 command
参数。