Celeb Glow
news | March 31, 2026

ssh connect to host port 22: Connection refused

So I have a docker container and I want to connect to it with ssh from host(I know it's not indicated to ssh to a docker container, but I need to do it).

I have installed open ssh server on both container and host and the service is running on both. I also checked the ports for both docker and host and both are 22. I'm trying to connect to it as root and also as non privileged user. What can I do?

when running ssh user@host -vvv I'm getting this:

debug1: reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: resolving "host" port 22
debug2: ssh_connect_direct: needpriv 0
debug2: connecting to host [host] port 22
debug1: connect to address host port 22: connection refused
7

1 Answer

By default docker containers do not expose any ports.

To expose port to your host you need to add the option: -p 22:22 to expose the port when you start running the container.

Example:

docker run -p 22:22 $CONTAINER_NAME

To permanatly expose a port in Docker you need to edit the Dockerfile for the container and rebuild it.

In the Dockerfile add the line.

EXPOSE 22

Not recommended to expose ssh port to a conatiner running in production.

5

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy