AfterWorkGuinness
AfterWorkGuinness

Reputation: 1850

SSH between local Docker containers

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

Upvotes: 3

Views: 4278

Answers (1)

dskow
dskow

Reputation: 944

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

Upvotes: 2

Related Questions