使用dockerfile创建基于ubuntu的ssh容器

使用dockerfile创建基于ubuntu的ssh容器:

1.创建目录
[root@test-devops home]# mkdir sshd_ubuntu
[root@test-devops home]# cd sshd_ubuntu/
[root@test-devops sshd_ubuntu]# touch Dockerfile run.sh
[root@test-devops sshd_ubuntu]#

2.编写run.sh脚本和认证文件
[root@test-devops sshd_ubuntu]# cat run.sh
#!/bin/bash
/usr/sbin/sshd -D
[root@test-devops sshd_ubuntu]# chmod +x run.sh
[root@test-devops sshd_ubuntu]#

[root@test-devops sshd_ubuntu]# chmod 400 authorized_keys

3.编写dockerFile
[root@test-devops sshd_ubuntu]# cat Dockerfile
# 继承的镜像
FROM ubuntu:14.04

# 作者信息
MAINTAINER from steven (www.opsnotes.net)

# 安装SSH服务和VIM服务
RUN apt-get install -y openssh-server vim
RUN mkdir -p /var/run/sshd
RUN mkdir -p /root/.ssh
RUN echo 'root:123qwer' |chpasswd
RUN sed -i "s/#UsePrivilegeSeparation.*/UsePrivilegeSeparation no/g" /etc/ssh/sshd_config
RUN sed -i "s/UsePAM.*/UsePAM no/g" /etc/ssh/sshd_config
RUN sed -i 's/^PermitRootLogin without-password/PermitRootLogin yes/g' /etc/ssh/sshd_config

# 取消PAM限制
RUN sed -i '/pam_loginuid.so/c session    optional     pam_loginuid.so'  /etc/pam.d/sshd

# 复制文件,给脚本可执行权限
ADD authorized_keys /root/.ssh/authorized_keys
ADD run.sh /run.sh
RUN chmod +x /run.sh

# 设置开放端口
EXPOSE 22

# 设置自动开启命令
CMD ["/run.sh"]
[root@test-devops sshd_ubuntu]#
4.创建镜像
[root@test-devops sshd_ubuntu]# docker build -t sshd:dockerfile .
Sending build context to Docker daemon 4.608 kB
Sending build context to Docker daemon
Step 0 : FROM ubuntu:14.04
 ---> 6d4946999d4f
Step 1 : MAINTAINER from www.dockerpool.com by steven
 ---> Running in e1b2a2e326a7
 ---> 818919de1a48
Removing intermediate container e1b2a2e326a7
Step 2 : RUN apt-get install -y openssh-server vim
 ---> Running in 56f0e8510f4e
Reading package lists...
Building dependency tree...
Reading state information...
……略……
 ---> Running in 819a996beecc
 ---> d5d7ef949227
Removing intermediate container 819a996beecc
Step 10 : CMD /run.sh
 ---> Running in 4e3399154feb
 ---> 053f79ebfb32
Removing intermediate container 4e3399154feb
Successfully built 053f79ebfb32
[root@test-devops sshd_ubuntu]#

5.查看镜像
[root@test-devops sshd_ubuntu]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
sshd                dockerfile          053f79ebfb32        15 minutes ago      261.4 MB
[root@test-devops sshd_ubuntu]#

6.测试镜像,运行容器
[root@test-devops sshd_ubuntu]# docker run -d -p 10122:22 sshd:dockerfile
91055c500eaa619e6758199c2e5cc3a55d881e30c20a66857e45f3f650456ebd
[root@test-devops sshd_ubuntu]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                   NAMES
91055c500eaa        sshd:dockerfile     "/run.sh"           4 seconds ago       Up 3 seconds        0.0.0.0:10122->22/tcp   fervent_kowalevski
[root@test-devops sshd_ubuntu]# netstat -tlnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      924/sshd
tcp        0      0 121.41.118.184:4505         0.0.0.0:*                   LISTEN      1030/python2.6
tcp        0      0 121.41.118.184:4506         0.0.0.0:*                   LISTEN      1042/python2.6
tcp        0      0 0.0.0.0:10050               0.0.0.0:*                   LISTEN      17777/zabbix_agentd
tcp        0      0 0.0.0.0:10051               0.0.0.0:*                   LISTEN      20845/zabbix_server
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      20670/php-fpm
tcp        0      0 0.0.0.0:10122               0.0.0.0:*
[root@test-devops sshd_ubuntu]# 

7.登陆
zhangkeyuan@opsnotes:~ » ssh -p 10122 root@121.41.118.184
Welcome to Ubuntu 14.04.2 LTS (GNU/Linux 2.6.32-504.12.2.el6.x86_64 x86_64)

 * Documentation:  https://help.ubuntu.com/
Last login: Thu Jul  2 08:23:44 2015 from 101.231.116.2
root@91055c500eaa:~#
OpsNotes wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!