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
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.
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.
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.
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
Now you can just do ssh other_docker
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With