Search code examples
sshdockercontainersopenssh

Docker SSH Server Connection Refused


I have the following Dockerfile for my container:

FROM ubuntu:latest
RUN apt-get upgrade
RUN apt-get update
RUN apt-get -y install openssh-server
CMD tail -f /dev/null

However, when I build and run it, it says connection refused. I tried almost everything I could find for solutions, including "ufw allow" which does not work as it tells me that I need to be root (how can I do that for a Dockerfile?).

I need to access the container in exactly this way:

ssh <someUsername?>@containerIP

PS: I know that there are other ways to connect to a Docker container, but I need to use SSH so attach etc. is not an option.

EDIT: It seems to be because sshd is not running, because it does not show up if I run "ps -A".


Solution

  • Docker containers isolate an application or command, in your case, they are isolating the command tail -f /dev/null which will keep the container running, but not do anything useful. If you want an sshd server running, you need to launch that as your foreground command:

    CMD ["/usr/sbin/sshd", "-D"]
    

    There are quite a few images already built for this on Docker Hub, including https://hub.docker.com/r/trsouz/ssh/ and https://hub.docker.com/r/rastasheep/ubuntu-sshd/.