Docker container ssh error: ssh_exchange_identification: Connection closed by remote host

RUN service ssh restart

This runs an ssh service restart (well actually a start) during the image creation phase, not in the future running container. There is no CMD nor ENTRYPOINT in you Dockerfile so it defaults to the one(s) configured in your base image (which is bash)

In other words, there is no ssh daemon running when you start your container. A temporary solution is to launch an exec command on the running container: docker exec your_container_name service ssh start

To fix the issue correctly you need to instruct the image it should start sshd when a container is created (see the dockerize an ssh service at docker docs). In short:

  • remove the RUN service ssh restart line
  • add the two next lines
RUN mkdir /var/run/sshd
CMD ['/usr/sbin/sshd', '-D']
  • rebuild your image, launch a new container, ssh and enjoy.