Connection refused when trying to SSH into Docker containers. But port 22 is not blocked. Pings work

I’m running CentOS 7 VMs and open source Docker. I want to SSH to Docker containers from other VMs or other Docker containers. How do I do this?

I can SSH from the Docker containers to their shared host VM. I cannot SSH into the Docker containers from the host VM because of an error “Connection refused.” I cannot SSH to the Docker containers from the containers themselves because of “connection refused.” nmap results show that ports 1 through 1000 are closed but not filtered on each container. I tested against the IP address in the result of a docker inspect command.

I can ping the Docker containers from each other using their IP addresses. sftp between the containers fails because of “connection refused.” Given that there is no firewall rule on port 22, what is causing this “connection refused” problem?

Why do you need to SSH into containers at all?

Would using docker exec to spawn a shell into the container solve your use case?

Perhaps sshd was installed, but not started via the service? The startup of the service creates the ssh keys when you start it for the first time. If you just start the service with sshd -D the service starts, but the keys are not created.

Sometimes there is a use case to SSH into a container, say with a shared environment from remote machines or the developer simply prefers using PUTTY for display options etc.

Hello, you use root user to acess container?

If yes, you need the change permission sshd_config

nano /etc/ssh/sshd_config

PermitRootLogin without-password
change to
PermitRootLogin yes

service ssh restart

and try again.

Once again, don’t SSH into containers unless the specific purpose of that container is to run SSH. Why not use docker exec -ti containername sh? It’s a drop-dead simple way to spawn shell in existing container.

Because, for example, if you use PHPStorm and just started using Docker it’s much simpler to continue using SSH remote interpreter in the IDE than figuring out how to set it up for Docker.


So here’s a question. I set up a Docker [v Engine 19.03.08] container on a Macbook [running Catalina] and port forwarded 2222:22 in my docker-compose.yml file. This yields an “authentication failed” in PHPStorm, when I try to connect using a key-pair, However, also in PHPStorm, if I don’t do any port forwarding, or use 22 as the port, authentication succeeds. I would rather use 2222 on the host to leave 22 free for other ftp tasks if I need them, but for now am sticking with 22. Any idea why this would happen. [I think I have my ssh authentication set up correctly, or it wouldn’t be working when I go to port 22] Addendum: I should add that the “User” name I use in order to get this to work on port 22 is a valid user name on my Macbook. “root” does not work.

If you want something based on ssh, like scp, I advice you to try docker cp according to documentation docker cp | Docker Documentation .