Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 242
user1016313 Avatar asked Nov 11 '14 09:11

user1016313


People also ask

Can two Docker containers talk to each other?

If you are running more than one container, you can let your containers communicate with each other by attaching them to the same network. Docker creates virtual networks which let your containers talk to each other. In a network, a container has an IP address, and optionally a hostname.

Can we share container network between Docker hosts?

If you use the host network mode for a container, that container's network stack is not isolated from the Docker host (the container shares the host's networking namespace), and the container does not get its own IP-address allocated.


1 Answers

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

like image 86
Usman Ismail Avatar answered Sep 28 '22 23:09

Usman Ismail