Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1237559
  • 博文数量: 220
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1769
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-13 16:19
个人简介

努力, 努力, 再努力

文章分类

全部博文(220)

文章存档

2018年(8)

2017年(46)

2016年(75)

2015年(92)

我的朋友

分类: 系统运维

2016-07-26 14:54:02

原文链接:http://blog.csdn.net/kongxx/article/details/38412119

原文链接:Docker使用Dockerfile创建支持ssh服务自启动的容器镜像
http://www.kongxx.info/blog/?p=59

1. 首先创建一个Dockerfile文件,文件内容如下

  1. # 选择一个已有的os镜像作为基础  
  2. FROM centos:centos6  
  3.    
  4. # 镜像的作者  
  5. MAINTAINER Fanbin Kong "kongxx@hotmail.com"  
  6.    
  7. # 安装openssh-server和sudo软件包,并且将sshd的UsePAM参数设置成no  
  8. RUN yum install -y openssh-server sudo  
  9. RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config  
  10.    
  11. # 添加测试用户admin,密码admin,并且将此用户添加到sudoers里  
  12. RUN useradd admin  
  13. RUN echo "admin:admin" | chpasswd  
  14. RUN echo "admin   ALL=(ALL)       ALL" >> /etc/sudoers  
  15.    
  16. # 下面这两句比较特殊,在centos6上必须要有,否则创建出来的容器sshd不能登录 ,此句同样适用centos7
  17. RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key  
  18. RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key  
  19.    
  20. # 启动sshd服务并且暴露22端口  
  21. RUN mkdir /var/run/sshd  
  22. EXPOSE 22  
  23. CMD ["/usr/sbin/sshd", "-D"] 

2. 有了Dockerfile文件以后,就可以根据Dockerfile来创建image文件了,在Dockerfile所在的目录下,运行下面的命令

1 sudo docker build -t centos6-ssh .

命令成功后,就会创建一个名字为centos6-ssh的image,可以使用“sudo docker images”来查看。

3. 此时就可以根据上面创建出来的image文件来创建自己的容器了,下面的命令会创建一个名字为“mytest”的容器。

1 sudo docker run -d -P --name=mytest centos6-ssh

4. 有了容器,就可以测试我们的ssh服务了。
4.1 运行“sudo docker inspect mytest”,查看当前启动容器IP地址,然后运行下面的命令来测试

1 ssh admin@<容器IP>

4.2 另外,也可以通过docker的端口映射来访问, 使用“sudo docker port mytest 22”查看当前容器的22端口对应的宿主机器的端口,然后通过下面的命令来访问

1 ssh admin@<宿主机器IP> -p <宿主机器端口>

build的过程:

[root@cent7y sshd_centos]# docker build -t cent7_ssh:dockerfile .
Sending build context to Docker daemon 3.072 kB
Step 1 : FROM docker.io/ansible/centos7-ansible
 ---> 62fd4cbeb8ed
Step 2 : MAINTAINER by songjh (fredmail03@126.com)
 ---> Running in 4750388c573a
 ---> 53d446381610
Removing intermediate container 4750388c573a
Step 3 : RUN yum clean expire-cache
 ---> Running in 01e57f8154ac
Loaded plugins: fastestmirror, ovl
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
Cleaning repos: base epel extras updates
7 metadata files removed
 ---> dd0bf62151e4
Removing intermediate container 01e57f8154ac
Step 4 : RUN yum -y install openssh-server
 ---> Running in d849128bbc7f
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
 * base: mirrors.yun-idc.com
 * epel: mirrors.neusoft.edu.cn
 * extras: mirrors.tuna.tsinghua.edu.cn
 * updates: mirrors.tuna.tsinghua.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package openssh-server.x86_64 0:6.6.1p1-25.el7_2 will be installed
--> Processing Dependency: libwrap.so.0()(64bit) for package: openssh-server-6.6.1p1-25.el7_2.x86_64
--> Running transaction check
---> Package tcp_wrappers-libs.x86_64 0:7.6-77.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                Arch        Version                  Repository    Size
================================================================================
Installing:
 openssh-server         x86_64      6.6.1p1-25.el7_2         updates      436 k
Installing for dependencies:
 tcp_wrappers-libs      x86_64      7.6-77.el7               base          66 k

Transaction Summary
================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 502 k
Installed size: 1.0 M
Downloading packages:
--------------------------------------------------------------------------------
Total                                              1.8 MB/s | 502 kB  00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : tcp_wrappers-libs-7.6-77.el7.x86_64                          1/2
  Installing : openssh-server-6.6.1p1-25.el7_2.x86_64                       2/2
  Verifying  : tcp_wrappers-libs-7.6-77.el7.x86_64                          1/2
  Verifying  : openssh-server-6.6.1p1-25.el7_2.x86_64                       2/2

Installed:
  openssh-server.x86_64 0:6.6.1p1-25.el7_2                                      

Dependency Installed:
  tcp_wrappers-libs.x86_64 0:7.6-77.el7                                         

Complete!
 ---> 8422c4e61a41
Removing intermediate container d849128bbc7f
Step 5 : RUN yum -y install net-tools
 ---> Running in 6c6ce9f09d16
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: mirrors.yun-idc.com
 * epel: mirrors.neusoft.edu.cn
 * extras: mirrors.tuna.tsinghua.edu.cn
 * updates: mirrors.tuna.tsinghua.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package net-tools.x86_64 0:2.0-0.17.20131004git.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package         Arch         Version                          Repository  Size
================================================================================
Installing:
 net-tools       x86_64       2.0-0.17.20131004git.el7         base       304 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 304 k
Installed size: 917 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : net-tools-2.0-0.17.20131004git.el7.x86_64                    1/1
  Verifying  : net-tools-2.0-0.17.20131004git.el7.x86_64                    1/1

Installed:
  net-tools.x86_64 0:2.0-0.17.20131004git.el7                                   

Complete!
 ---> 560e3b18da24
Removing intermediate container 6c6ce9f09d16
Step 6 : RUN mkdir /var/run/sshd
 ---> Running in 5d8844a5ac3f
 ---> fb7690b91e72
Removing intermediate container 5d8844a5ac3f
Step 7 : RUN echo "root:dfklsdjk" | chpasswd
 ---> Running in f8bd3a1eade7
 ---> 347a6326fb85
Removing intermediate container f8bd3a1eade7
Step 8 : RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
 ---> Running in dd8927ec05b8
Enter passphrase (empty for no passphrase): Enter same passphrase again: Generating public/private dsa key pair.
Your identification has been saved in /etc/ssh/ssh_host_dsa_key.
Your public key has been saved in /etc/ssh/ssh_host_dsa_key.pub.
The key fingerprint is:
3d:c8:0c:f1:d5:1b:70:2e:4c:2e:44:52:77:27:74:61 root@73b1fe371681
The key's randomart image is:
+--[ DSA 1024]----+
|      oo+ =+= E. |
|       = * +o=   |
|      . o + .o   |
|       + + ..    |
|        S o      |
|           .     |
|                 |
|                 |
|                 |
+-----------------+
 ---> 34632d5e7e9a
Removing intermediate container dd8927ec05b8
Step 9 : RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
 ---> Running in a31056d62a08
Enter passphrase (empty for no passphrase): Enter same passphrase again: Generating public/private rsa key pair.
Your identification has been saved in /etc/ssh/ssh_host_rsa_key.
Your public key has been saved in /etc/ssh/ssh_host_rsa_key.pub.
The key fingerprint is:
e2:9f:99:3f:b4:bf:97:57:44:9d:29:9a:ac:6a:c6:15 root@73b1fe371681
The key's randomart image is:
+--[ RSA 2048]----+
|                +|
|             . oo|
|          . o .. |
|         E +    .|
|      . S o    . |
|     . . o.     .|
|      o o. .   ..|
|       * +o   o .|
|      o =..ooo . |
+-----------------+
 ---> 86b40aeed747
Removing intermediate container a31056d62a08
Step 10 : EXPOSE 22
 ---> Running in 7c2ae1f531af
 ---> 6f26db4c22f8
Removing intermediate container 7c2ae1f531af
Step 11 : CMD /usr/sbin/sshd -D
 ---> Running in ed9898a864a0
 ---> 1a1646b82e3b
Removing intermediate container ed9898a864a0
Successfully built 1a1646b82e3b
[root@cent7y sshd_centos]#


阅读(6766) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~