Asked password when ssh to container

Toshi picture Toshi · May 11, 2016 · Viewed 10.4k times · Source

I created a docker image which is subjected to test to login into container with SSH. However when I try to ssh into the container, I was asked the root password. Any ideas to get around it.

Dockerfile

FROM ubuntu:trusty

RUN apt-get update

RUN apt-get install -y openssh-server supervisor vim build-essential git
RUN mkdir -p /var/run/sshd
ADD supervisord/sshd.conf /etc/supervisor/conf.d/sshd.conf
RUN echo 'root:root' | chpasswd

EXPOSE 22
CMD ["/usr/bin/supervisord"]

supervisord/sshd.conf

[supervisord]
nodaemon=true

[program:sshd]
command=/usr/sbin/sshd -D

Answer

VonC picture VonC · May 11, 2016

You need to add your public key to the container root/.ssh/authorized_keys

If sshd does not find your public key there, it will fallback to username/password authentication.

An example would be "Setting ssh public keys on Docker image", but I don't like it as it means the container has the private key (it does not need it)

It is best to:

  • generate your public/private key locally.
  • add a COPY yourPublicKey /root/.ssh/authorized_keys in your Dockerfile

That generates an image whose containers will be able to be accessed by ssh.

Make sure that, on your host, your $HOME/.ssh does have the private key id_rsa and public key id_rsa.pub.

That enables ssh authentication between your docker host and your docker container, following the general (ie., not specific to docker) ssh authentication mechanism shown here:

http://sebastien.saunier.me/images/posts/SSH%20Connection%20explained.png

(source "GitHub public key authentication", from Sébastien Saunier ‏- @ssaunier)