Docker:手动制作镜像 [五]

一、制作docker镜像的步骤

1、启动容器安装软件服务

2、将安装好服务的容器commit提交为镜像

3:、启动新容器来测试新提交的镜像

二、制作支持ssh远程登录的docker镜像

1、启动容器安装软件服务

1、启动容器

[root@luoahong ~]# docker run -it -p 1022:22 --name luoahong4 centos:6.9

2、安装软件openssh-server

[root@0485b9ecd1ad /]# yum install openssh-server -y

[root@0485b9ecd1ad /]# /etc/init.d/sshd start
Generating SSH2 RSA host key: [ OK ]
Generating SSH1 RSA host key: [ OK ]
Generating SSH2 DSA host key: [ OK ]
Starting sshd: [ OK ]
[root@0485b9ecd1ad /]# netstat -lntup
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 118/sshd
tcp 0 0 :::22 :::* LISTEN 118/sshd

3、测试是否可以上网

[root@0485b9ecd1ad /]# curl www.baidu.com
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>鐧惧害涓€涓嬶紝浣犲氨鐭ラ亾</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class="head_wrapper"> <div class="s_form"> <div class="s_form_wrapper"> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class="fm"> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class="s_ipt" value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=鐧惧害涓€涓?class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class="mnav">鏂伴椈</a> <a href=http://www.hao123.com name=tj_trhao123 class="mnav">hao123</a> <a href=http://map.baidu.com name=tj_trmap class="mnav">鍦板浘</a> <a href=http://v.baidu.com name=tj_trvideo class="mnav">瑙嗛</a> <a href=http://tieba.baidu.com name=tj_trtieba class="mnav">璐村惂</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class="lb">鐧诲綍</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">鐧诲綍</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class="bri" style="display: block;">鏇村浜у搧</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>鍏充簬鐧惧害</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>浣跨敤鐧惧害鍓嶅繀璇?/a>  <a href=http://jianyi.baidu.com/ class="cp-feedback">鎰忚鍙嶉</a> 浜琁CP璇?30173鍙?nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

4、设置远程登录密码

echo 123456|passwd --stdin root

5、远程测试

[root@luoahong ~]# ssh root@192.168.228.134 -p 1022
The authenticity of host '[192.168.228.134]:1022 ([192.168.228.134]:1022)' can't be established.
RSA key fingerprint is SHA256:GBs8S1bKbhC5CxVHm+CeJouV5OJtpjes6mr9Dej7UZY.
RSA key fingerprint is MD5:32:14:d6:77:c9:60:3b:66:8d:e3:a7:d1:88:d3:0f:fc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.228.134]:1022' (RSA) to the list of known hosts.
root@192.168.228.134's password:
[root@0485b9ecd1ad ~]#

2、将安装好服务的容器commit提交为镜像

1、提交镜像

[root@luoahong ~]# docker commit luoahong4 centos6-ssh:v1
sha256:e414afbffe052453b38faed463070f4a35663ca1c9f8d64e44e131369ba31630
[root@luoahong ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0485b9ecd1ad centos:6.9 "/bin/bash" 42 minutes ago Exited (0) 3 minutes ago luoahong4

3:、启动新容器来测试新提交的镜像

[root@luoahong ~]# docker run -d -p 2022:22 centos6-ssh:v1 /usr/sbin/sshd -D
156d80283acf6f884df3589aeca834c38f2d78497e46e6580ce7cdf2b8f0e787
[root@luoahong ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
156d80283acf centos6-ssh:v1 "/usr/sbin/sshd -D" 4 seconds ago Up 3 seconds 0.0.0.0:2022->22/tcp romantic_chandrasekhar
0485b9ecd1ad centos:6.9 "/bin/bash" 42 minutes ago Exited (0) 3 minutes ago luoahong4
[root@luoahong ~]#

注意事项:记得一定要在启动容器的后面加上

/usr/sbin/sshd -D

要不然会出现启动四秒退出3秒

[root@luoahong ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0485b9ecd1ad centos:6.9 "/bin/bash" 42 minutes ago Exited (0) 3 minutes ago luoahong4

再次测试

[root@luoahong ~]# ssh root@192.168.228.134 -p 2022
The authenticity of host '[192.168.228.134]:2022 ([192.168.228.134]:2022)' can't be established.
RSA key fingerprint is SHA256:GBs8S1bKbhC5CxVHm+CeJouV5OJtpjes6mr9Dej7UZY.
RSA key fingerprint is MD5:32:14:d6:77:c9:60:3b:66:8d:e3:a7:d1:88:d3:0f:fc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.228.134]:2022' (RSA) to the list of known hosts.
root@192.168.228.134's password:
Last login: Tue Jan 15 09:16:31 2019 from 192.168.228.134
[root@156d80283acf ~]#

三、制作支持ssh+httpd双服务的镜像

1、启动容器安装软件服务

1、启动容器

[root@luoahong ~]# docker run -it --name luoahong centos:6.9

2、安装软件服务

[root@0d09d3a1591a /]# yum install httpd -y
[root@0d09d3a1591a /]# yum install openssh-server -y

3、启动服务设置远程登录密码

[root@0d09d3a1591a /]# /etc/init.d/sshd start
Generating SSH2 RSA host key:                              [  OK  ]
Generating SSH1 RSA host key:                              [  OK  ]
Generating SSH2 DSA host key:                              [  OK  ]
Starting sshd:                                             [  OK  ]
[root@0d09d3a1591a /]# echo 123456|passwd --stdin root
Changing password for user root.
passwd: all authentication tokens updated successfully.

4、编写容器启动脚本

vi /init.sh
#!/bin/bash
/etc/init.d/httpd start
/usr/sbin/sshd -D

2、将安装好服务的容器commit提交为镜像

docker commit luoahong centos6-ssh-httpd:v1

3、启动新容器来测试新提交的镜像

1、命令行操作

[root@luoahong ~]# docker run -d -p 8080:80 -p 1122:22 centos6-ssh-httpd:v1 /bin/bash /init.sh
ea82b95830817f3087b35a6447dd9e62ea697dd6bb0306d0336d946392e03670
[root@luoahong ~]# docker ps -a
CONTAINER ID        IMAGE                  COMMAND                CREATED             STATUS                      PORTS  NAMES
ea82b9583081        centos6-ssh-httpd:v1   "/bin/bash /init.sh"   9 seconds ago       Up 9 seconds                0.0.0.0:1122->22/tcp, 0.0.0.0:8080->80/tcp  stupefied_rosalind
0d09d3a1591a        centos:6.9             "/bin/bash"            17 minutes ago      Exited (0) 12 seconds ago  luoahong
156d80283acf        centos6-ssh:v1         "/usr/sbin/sshd -D"    19 minutes ago      Up 19 minutes               0.0.0.0:2022->22/tcp  romantic_chandrasekhar
0485b9ecd1ad        centos:6.9             "/bin/bash"            About an hour ago   Exited (0) 23 minutes ago

2、浏览器测试截图 

3、注意事项

多服务的话需要编写容器脚本,启动命令格式如下

[root@luoahong ~]# docker run -d -p 8080:80 -p 1122:22 centos6-ssh-httpd:v1 /bin/bash /init.sh

  

posted @ 2019-01-15 18:16  活的潇洒80  阅读(990)  评论(0编辑  收藏  举报