Docker SSH Server Connection Refused

10,618

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/.

Share:
10,618
dingoglotz
Author by

dingoglotz

Updated on June 28, 2022

Comments

  • dingoglotz
    dingoglotz almost 2 years

    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".