Setup Docker Container with SSH server? Setup Docker Container with SSH server? docker docker

Setup Docker Container with SSH server?


In order to start, the SSH daemon does need host keys.
Those does not represents the keys that you are going to use to connect to your container, just the keys that define this specific host.

A host key is a cryptographic key used for authenticating computers in the SSH protocol.

Source: https://www.ssh.com/ssh/host-key

So you have to generate some keys for your host, you can then safely ignore those if you do not really intend to use them.

Generating those keys can be done via

ssh-keygen -A

So in your image, just adding a

RUN ssh-keygen -A

should do.


For the record, here is my own sshd Alpine image:

FROM alpineRUN apk add --no-cache \         openssh \    && ssh-keygen -A \    && mkdir /root/.ssh \    && chmod 0700 /root/.ssh \    && echo "root:root" | chpasswd \    && ln -s /etc/ssh/ssh_host_ed25519_key.pub /root/.ssh/authorized_keysEXPOSE 22CMD ["/usr/sbin/sshd", "-D", "-e"]

Extra notes:

  • I am reusing the SSH keys generated by ssh-keygen -A, exposing them in a volume, this is the reason why I am doing the command:
    ln -s /etc/ssh/ssh_host_ed25519_key.pub /root/.ssh/authorized_keys
  • Because this is just an Ansible node cluster lab, I am SSH'ing this machine as the root user, this is why I need the, quite insecure
    echo "root:root" | chpasswd