SSH服务器不接受连接

我使用Docker在本地构build了一个Docker镜像,它运行一个SSH服务器+一些其他服务。 Dockerfile是内部的,所以我不能在这里发布。 但我所做的基本事情是:

RUN apt-get install -q -y openssh-server openssh-client RUN cp /etc/ssh/sshd_config /etc/ssh/sshd_config.factory-defaults RUN chmod aw /etc/ssh/sshd_config.factory-defaults RUN mkdir /var/run/sshd RUN echo 'root:changeme' |chpasswd ADD ./bootstrap.sh /root/bootstrap.sh ENTRYPOINT ["sh", "/root/bootstrap.sh"] 

在bootstrap.sh里面:

 ... echo "Starting SSH service..." /usr/sbin/sshd -D > /dev/null 2>&1 & ps -A | grep sshd ... 

…迄今为止工作。 我可以从主机连接到SSH服务器(在Ubuntu容器内部运行)。

到现在为止还挺好。 现在我已经把图片上传到Dockerhub,我在tutum.co上运行它。

现在的问题是:SSH服务器启动,但我无法连接到它。 即使在本地的容器内。 顺便说一下,我有一个浏览器shell,所以我仍然能够执行命令。

我在容器内执行:

 ssh root@localhost -v OpenSSH_5.9p1 Debian-5ubuntu1.4, OpenSSL 1.0.1 14 Mar 2012 debug1: Reading configuration data /etc/ssh/ssh_config debug1: /etc/ssh/ssh_config line 19: Applying options for * debug1: Connecting to localhost [::1] port 22. debug1: Connection established. debug1: permanently_set_uid: 0/0 debug1: identity file /root/.ssh/id_rsa type -1 debug1: identity file /root/.ssh/id_rsa-cert type -1 debug1: identity file /root/.ssh/id_dsa type -1 debug1: identity file /root/.ssh/id_dsa-cert type -1 debug1: identity file /root/.ssh/id_ecdsa type -1 debug1: identity file /root/.ssh/id_ecdsa-cert type -1 debug1: Remote protocol version 2.0, remote software version OpenSSH_5.9p1 Debia n-5ubuntu1.4 debug1: match: OpenSSH_5.9p1 Debian-5ubuntu1.4 pat OpenSSH* debug1: Enabling compatibility mode for protocol 2.0 debug1: Local version string SSH-2.0-OpenSSH_5.9p1 Debian-5ubuntu1.4 debug1: SSH2_MSG_KEXINIT sent Read from socket failed: Connection reset by peer 

你试图以rootforms进行连接,这是作为容器中的/etc/ssh/sshd_config中的标准选项被禁止的。 你需要在你的Dockerfile设置一个user ,或者允许root-login或者在你的Dockerfile复制一个公钥。 我会阻止你允许root -login和通过Dockerfile复制你的Dockerfile 。 我的解决scheme将是以下列方式编辑您的Dockerfile

 # Set root passwd RUN echo 'root:changeme' |chpasswd # Add user so that container does not run as root RUN useradd -m michaelkunzmann RUN echo "michaelkunzmann:test" | chpasswd RUN usermod -s /bin/bash michaelkunzmann RUN usermod -aG sudo michaelkunzmann ENV HOME /home/michaelkunzmann ENV HOSTNAME michaelkunzmannsbox 

然后build立图像并通过ssh michaelkunzmann@localhost -vlogin。 (如果这不起作用,请确保你使用的是正确的port ,你可以通过使用docker run -d -p 127.0.0.1:5000:22 -P --name="name-your-container!" your-image来指定它docker run -d -p 127.0.0.1:5000:22 -P --name="name-your-container!" your-image你可以通过ssh michaelkunzmann@localhost -p 5000login。)

顺便说一句,如果你在容器中运行多个进程,你应该熟悉Supervisor 。

我的docker文件供像我这样的其他人参考:

 FROM centos:6 RUN yum install -y openssh-server RUN mkdir /var/run/sshd RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key RUN sed -ri 's/UsePAM yes/#UsePAM yes/g' /etc/ssh/sshd_config RUN sed -ri 's/#UsePAM no/UsePAM no/g' /etc/ssh/sshd_config RUN echo 'root:root' | chpasswd CMD ["/usr/sbin/sshd", "-D"]