https://docs.docker.com/engine/examples/running_ssh_service/
http://wiki.jikexueyuan.com/project/docker/examples/running_ssh_service.html

获取centos镜像

1
sudo docker pull centos

CentOS Linux release 7.2.1511 (Core)

运行容器

1
sudo docker run --name=centos-ssh -i -t centos /bin/bash

进入容器执行命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
docker attach 81946f6f1a3a

yum install openssh-server -y

vi /etc/ssh/sshd_config # UsePAM参数设置成“no”

/usr/sbin/sshd -D # 前台运行 sshd
/usr/sbin/sshd # 后台运行 sshd

useradd hadoop # 添加一个用户
echo 'hadoop:hadoop123' | chpasswd

yum install sudo # 安装sudo命令

echo "hadoop ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers

exit

提交镜像

1
docker commit 81946f6f1a3a centos-sshd

验证ssh登陆
https://docs.docker.com/engine/examples/running_ssh_service/
http://crashedbboy.blogspot.com/2015/06/docker-centosopenssh-server.html

1
2
3
4
5
6
7
docker run -d -p 22 --name test_sshd centos-sshd /usr/sbin/sshd -D

docker port test_sshd 22 # 返回结果 0.0.0.0:32768

docker inspect test_sshd|grep IPAddress # 结果172.17.0.3

ssh hadoop@172.17.0.3 -p 32768

原创文章,转载请注明: 转载自whoami的博客
本博客的文章集合: http://www.itweet.cn/archives/