Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSH between local Docker containers

Tags:

docker

ssh

Just starting out with Docker. Running multiple docker containers locally from the same image.

docker run -it -p 52022:22 ubuntu
docker run -it -p 52023:22 ubuntu

I've added openssh-server and setup keys. How do I ssh between them (for experimental purposes)? Trying to ssh using the hostname I get from the docker container results in Could not resolve hostname

like image 756
AfterWorkGuinness Avatar asked Nov 27 '25 11:11

AfterWorkGuinness


1 Answers

WARN ssh into a root container may potentially give root access its host machine.

You can make a swarm and talk to them with the generated dns name.

FYI this example, since the ubuntu is not running anything, it stops the container and launches another. You will need to launch a container that stays up like httpd

Here is my doggy example. The doggy/docker-compose.yml file would look like this.

version: '3.0'
services:
  sparkey:
    image: "httpd"
    ports:
     - "52022:22"
     - "9002:80"
  limey:
    image: "httpd"
    ports:
     - "52023:22"
     - "9003:80"

Intialize a swarm if not already done.

docker swarm init

Then in a doggy folder.

docker stack deploy --compose-file docker-compose.yml doggy

Then when you go into the container get container name from docker ps.

docker exec -it doggy_limey_1.7jm5muapfhekb11v2ei8gvnc9 bash

You can find the machine, if multiple host machines in the swarm.

docker service ps doggy

You can talk to the other with a generated dns name or the ip of the swarm manager.

ping doggy_sparkey
PING doggy_sparkey (10.0.0.4): 56 data bytes
64 bytes from 10.0.0.4: seq=0 ttl=64 time=0.036 ms
64 bytes from 10.0.0.4: seq=1 ttl=64 time=0.070 ms

Stop the stack with this command.

docker stack rm doggy
like image 134
dskow Avatar answered Nov 30 '25 05:11

dskow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!