SSH into a docker container from another container on a different host

5.5k views Asked by At

I have a docker container running on an EC2 host, and another running on another ec2 host. How do I ssh from one to another, without providing any port numbers? I want to do something like ssh root@ip-address-of-container

3

There are 3 answers

0
user1016313 On BEST ANSWER
0
Usman Ismail On

For you to be able to ssh into the second container on port 22 you would need get the host ec2 vm's ssh daemon out of the way.

  1. 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.

  2. 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.

  3. 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
    

Now you can just do ssh other_docker

0
mikeLundquist On

I haven't tested this yet, but you might be able to do something like ssh [email protected] 'ssh [email protected]' using ssh's command parameter.