Dockerizing an SSH Daemon Service

docker

Dockerfile

一个开启Docker SSH的Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#sshd
#
# VERSION 1.0.0
FROM docker.cn/docker/ubuntu:14.04
MAINTAINER Lanvige Jiang <lanvige(@)gmail.com>
RUN apt-get update && apt-get install -y openssh-server
RUN mkdir /var/run/sshd
RUN echo 'root:password01' | chpasswd
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
# SSH login fix. Otherwise user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]

Build the image:

1
$ sudo docker build -t ubuntu_sshd .

使用该Image运行一个 container

运行这个Image,并给container起名叫test_sshd。然后映射docker的22端口到本机。

1
2
3
$ sudo docker run -d -P --name test_sshd ubuntu_sshd
$ sudo docker port test_sshd 22
0.0.0.0:49154

And now you can ssh as root on the container’s IP address (you can find it with docker inspect) or on port 49154 of the Docker daemon’s host IP address (ip address or ifconfig can tell you that) or localhost if on the Docker daemon host:

现在,就可以用root账号和Host IP(Host 中可直接使用localhost) + 映射返回的端口号,ssh到contrainer中。

1
2
3
$ ssh root@10.0.0.2 -p 49154
# The password is ``password01``.
$$

REF::