Docker Community Forums

Share and learn in the Docker community.

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


(Mike812) #1

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?

(Nathan Le Claire) #2

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?

(Qadinc) #3

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.

(Lucas da Silva Simão) #4

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.

(Nathan Le Claire) #5

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.

(Anatoliyarkhipov) #6

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.