Docker Community Forums

Share and learn in the Docker community.

Cant connect to docker container via SSH from another container


I have two containers:

  • The first expose an ssh server on port 2222
  • The second have a ssh client

When I connect to the ssh server container from the host it works fine.
But when I try from the client container I get “Connection refused”.

Here is my ssh command:

ssh my_user@ -p 2222

Do you have any idea ?

Many thanks for your help.

Use docker service discovery and then you can ssh from one container to another container. Here you can achieve service discovery by connecting all the containers to the same network.

docker network create -d bridge test
docker run -p 22 --rm -ti --name u1 --network test ubuntu:v2 bash
docker run -p 22 --rm -ti --name u2 --network test ubuntu:v2 bash
Now from u1 you can ssh into u2 as ssh user@u2.