﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-zhiye_wang</title><link>http://www.cppblog.com/zhiyewang/</link><description>向星空仰望的越深，越发现自己的渺小</description><language>zh-cn</language><lastBuildDate>Thu, 09 Apr 2026 05:40:00 GMT</lastBuildDate><pubDate>Thu, 09 Apr 2026 05:40:00 GMT</pubDate><ttl>60</ttl><item><title>ubuntu安装OpenLDAP(二) 配置LDAP</title><link>http://www.cppblog.com/zhiyewang/archive/2016/04/20/213312.html</link><dc:creator>zhiye_wang</dc:creator><author>zhiye_wang</author><pubDate>Wed, 20 Apr 2016 09:31:00 GMT</pubDate><guid>http://www.cppblog.com/zhiyewang/archive/2016/04/20/213312.html</guid><wfw:comment>http://www.cppblog.com/zhiyewang/comments/213312.html</wfw:comment><comments>http://www.cppblog.com/zhiyewang/archive/2016/04/20/213312.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhiyewang/comments/commentRss/213312.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhiyewang/services/trackbacks/213312.html</trackback:ping><description><![CDATA[<p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 微软雅黑, sans-serif;"><a href="http://blog.chinaunix.net/uid-29757900-id-5705855.html" _href="http://blog.chinaunix.net/uid-29757900-id-5705855.html" style="text-decoration: none; color: #19599b; font-family: 微软雅黑, 黑体, Verdana; font-size: 20px;"><span>ubuntu安装OpenLDAP(二) 配置LDAP</span></a></span><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">上一篇博文中，我们成功的安装了OpenLDAP-2.4.4到系统中，这篇文章介绍如何配置一个我</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">们自己的ldap服务器来使用。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">1 配置ldap的dc和cn</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: red;">vim&nbsp;/usr/local/etc/openldap/slapd.conf</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">修改其中的下面两行：</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: red;">suffix&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"dc=example,dc=com"</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: red;">rootdn&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"cn=Manager,dc=example,dc=com"</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">我修改后的效果如图：</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><img src="http://www.cppblog.com/images/cppblog_com/zhiyewang/09.png" width="540" height="149" alt="" /><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">2&nbsp;<span style="font-family: 宋体;">启动</span>slapd</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: red;"># su root -c /usr/local/libexec/slapd</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">验证一下能不能使用：</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: red;"># ldapsearch -x -b '' -s base'(objectclass=*)'</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><img src="http://www.cppblog.com/images/cppblog_com/zhiyewang/10.png" width="796" height="378" alt="" /><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">说明</span>ldap<span style="font-family: 宋体;">服务器已经可以使用了。</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">3 新建管理账号<br />建立一个</span>cloudsoar.ldif&nbsp;<span style="font-family: 宋体;">文件</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: red;"># vim cloudsoar.ldif</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: red;">dn: dc=cloudsoar,dc=com</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: red;">objectclass: dcObject</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: red;">objectclass: organization</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: red;">o: cloudsoar company</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: red;">dc: cloudsoar</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: red;">dn:cn=Manager,dc=cloudsoar,dc=com</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: red;">objectclass:organizationalRole</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: red;">cn: Manager</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">将文件中的内容插入数据库</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: #ff0000;">ldapadd -x -D "cn=Manager,dc=cloudsoar,dc=com"-W -f cloudsoar.ldif&nbsp;</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">如果需要密码，我们的默认密码是：</span><span style="font-family: Calibri, sans-serif;">secret</span><span style="font-family: 宋体;">（位于</span><span style="font-family: Calibri, sans-serif;">/usr/local/etc/openldap/slapd.conf</span><span style="font-family: 宋体;">）</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><img src="http://www.cppblog.com/images/cppblog_com/zhiyewang/11.1.png" width="781" height="111" alt="" /><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">这里可以看到节点已经插入到</span>ldap<span style="font-family: 宋体;">了。这里的</span>cloudsoar<span style="font-family: 宋体;">是我起的域名，您也可以根据需要设置一个自己喜欢的域名。</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">查询一下：</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: #ff0000;"># ldapsearch -x -b 'dc=cloudsoar,dc=com''(objectclass=*)'</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><img src="http://www.cppblog.com/images/cppblog_com/zhiyewang/12.1.png" width="791" height="377" alt="" /><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">这里我还是不放心，从另外一台安装了</span>windows<span style="font-family: 宋体;">操作系统的电脑上，使用</span>LDAPAdmin.exe&nbsp;<span style="font-family: 宋体;">来连接试试。</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><img src="http://www.cppblog.com/images/cppblog_com/zhiyewang/13.png" width="456" height="421" alt="" /><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><img src="http://www.cppblog.com/images/cppblog_com/zhiyewang/14.png" width="958" height="204" alt="" /><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">可以看到我的</span>ldap<span style="font-family: 宋体;">已经可以使用了。</span></p><div><span style="font-family: 宋体;"><br /></span></div><img src ="http://www.cppblog.com/zhiyewang/aggbug/213312.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhiyewang/" target="_blank">zhiye_wang</a> 2016-04-20 17:31 <a href="http://www.cppblog.com/zhiyewang/archive/2016/04/20/213312.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title> ubuntu安装OpenLDAP(附错误的详细解决办法)</title><link>http://www.cppblog.com/zhiyewang/archive/2016/04/20/213311.html</link><dc:creator>zhiye_wang</dc:creator><author>zhiye_wang</author><pubDate>Wed, 20 Apr 2016 09:27:00 GMT</pubDate><guid>http://www.cppblog.com/zhiyewang/archive/2016/04/20/213311.html</guid><wfw:comment>http://www.cppblog.com/zhiyewang/comments/213311.html</wfw:comment><comments>http://www.cppblog.com/zhiyewang/archive/2016/04/20/213311.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhiyewang/comments/commentRss/213311.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhiyewang/services/trackbacks/213311.html</trackback:ping><description><![CDATA[<div></div><div><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>ubuntu安装OpenLDAP(附错误的详细解决办法)</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>1&nbsp;<span style="font-family: 宋体;">下载</span>OpenLDAP<span style="font-family: 宋体;">源码</span></strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;"><br /></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;<a href="http://www.openldap.org/software/download/" _href="http://www.openldap.org/software/download/">http://www.openldap.org/software/download/</a></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">或者</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;<a href="ftp://ftp.openldap.org/pub/OpenLDAP/openldap-release.tgz" _href="ftp://ftp.openldap.org/pub/OpenLDAP/openldap-release.tgz">ftp://ftp.openldap.org/pub/OpenLDAP/openldap-release.tgz</a></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>2&nbsp;<span style="font-family: 宋体;">解压到本地</span></strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;# gunzip-c openldap-VERSION.tgz | tar xf -</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto; text-indent: 7px;"><span style="color: #ff0000;"># cd openldap-2.4.44</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto; text-indent: 7px;"><span style="color: #ff0000;"># ./configure</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;configure: error: BDB/HDB: BerkeleyDB notavailable</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">提示本地没有安装</span>BerkeleyDB<span style="font-family: 宋体;">数据库</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>3&nbsp;<span style="font-family: 宋体;">安装</span>BerkeleyDB</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;<span style="font-family: 宋体;">去</span>Oracle<span style="font-family: 宋体;">官网下载：</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;http://www.oracle.com/technetwork/database/database-technologies/berkeleydb/downloads/index.html</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">解压到本地</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><img src="http://www.cppblog.com/images/cppblog_com/zhiyewang/01.png" width="743" height="54" alt="" /><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">切换到</span>build_unix&nbsp;<span style="font-family: 宋体;">目录</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: #ff0000;"># cd build_unix</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: #ff0000;"># ../dist/configure</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: #ff0000;"># make</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: #ff0000;"># make install</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">root@cloudsoar-virtual-machine:/home/cloudsoar/db-6.2.23/build_unix#<span style="color: red;">make install</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">Installing DB include files:/usr/local/BerkeleyDB.6.2/include ...</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">Installing DB library:/usr/local/BerkeleyDB.6.2/lib ...</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">libtool: install: cp -p .libs/libdb-6.2.so/usr/local/BerkeleyDB.6.2/lib/libdb-6.2.so</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">libtool: install: cp -p .libs/db_upgrade/usr/local/BerkeleyDB.6.2/bin/db_upgrade</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">libtool: install: cp -p .libs/db_verify/usr/local/BerkeleyDB.6.2/bin/db_verify</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">Installing documentation:/usr/local/BerkeleyDB.6.2/docs ...</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>4&nbsp;<span style="font-family: 宋体;">查看安装</span></strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><img src="http://www.cppblog.com/images/cppblog_com/zhiyewang/02.png" width="780" height="57" alt="" /><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>5&nbsp;<span style="font-family: 宋体;">设置到系统变量</span></strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">不设置的话，等下安装</span>OpenLDAP<span style="font-family: 宋体;">时候执行</span>./configure<span style="font-family: 宋体;">检查时候还会报同样的错</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: #ff0000;"># vim /etc/ld.so.conf</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">添加一行：</span>/usr/local/BerkeleyDB.6.2/lib/</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">保存退出。</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">执行生效：<span style="color: #ff0000;">#&nbsp;</span></span><span style="color: #ff0000;">ldconfig &#8211;v</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>6&nbsp;<span style="font-family: 宋体;">继续安装</span>openldap-2.4.44</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><img src="http://www.cppblog.com/images/cppblog_com/zhiyewang/03.png" width="783" height="126" alt="" /><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">切换到</span>openldap<span style="font-family: 宋体;">的源码目录下，重新执行</span>configure<span style="font-family: 宋体;">文件</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">#&nbsp;<span style="color: red;">./configure CPPFLAGS="-I/usr/local/BerkeleyDB.6.2/include-D_GNU_SOURCE" LDFLAGS="-L/usr/local/BerkeleyDB.6.2/lib"</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">执行后报错</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">error: BerkeleyDB version incompatible withBDB/HDB backends</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">这里找不到原因，后来查看</span>openladp-2.4.44,<span style="font-family: 宋体;">目录的</span>README<span style="font-family: 宋体;">文件发现如下内容：</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><img src="http://www.cppblog.com/images/cppblog_com/zhiyewang/04.png" width="574" height="86" alt="" /><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">郁闷，原来</span>openldap-2.4.44<span style="font-family: 宋体;">要求用</span>Oracle Berkeley 4.4-4.8<span style="font-family: 宋体;">或者</span>5.0-5.1<span style="font-family: 宋体;">版本的，而我用的是</span>db-6.2.23.tar.gz<span style="font-family: 宋体;">版本的。</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">没办法，只有重新去</span>Oracle<span style="font-family: 宋体;">官网下载一个</span>db-5.1.29.tar.gz<span style="font-family: 宋体;">版本的。</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">按照安装</span>Berkeley 6.2<span style="font-family: 宋体;">的方法再次安装</span>Berkeley DB 5.1<span style="font-family: 宋体;">。</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"># cd build_unix</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"># ../dist/configure</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"># make</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"># make install</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"># vim /etc/ld.so.conf</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">添加一行：</span>/usr/local/BerkeleyDB.5.1/lib/</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">保存退出。</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">执行生效：</span>ldconfig &#8211;v</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>8&nbsp;<span style="font-family: 宋体;">安装</span>openldap&nbsp;&nbsp;</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">切换到</span>openldap-2.4.44<span style="font-family: 宋体;">目录</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">#&nbsp;<span style="color: red;">./configure CPPFLAGS="-I/usr/local/BerkeleyDB.5.1/include-D_GNU_SOURCE" LDFLAGS="-L/usr/local/BerkeleyDB.5.1/lib"</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">执行完毕可以看到提示我执行</span>make depand<span style="font-family: 宋体;">了</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><img src="http://www.cppblog.com/images/cppblog_com/zhiyewang/05.png" width="615" height="218" alt="" /><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>9&nbsp;<span style="font-family: 宋体;">编译软件</span></strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">根据</span><a href="http://www.openldap.org/doc/admin24/install.html" _href="http://www.openldap.org/doc/admin24/install.html">http://www.openldap.org/doc/admin24/install.html</a><span style="font-family: 宋体;">的安装步骤继续执行</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">执行命令：</span>#&nbsp;<span style="color: red;">make depend</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><img src="http://www.cppblog.com/images/cppblog_com/zhiyewang/06.png" width="692" height="77" alt="" /><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">执行命令：</span>#&nbsp;<span style="color: red;">make</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>10&nbsp;<span style="font-family: 宋体;">测试软件</span></strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">根据官方文档说明：</span>Once the software has been properly configured and successfullymade, you should run the test suite to verify the build.</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">我们也需要测试一下安装是否成功，执行命令：</span>#&nbsp;<span style="color: red;">make test</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><img src="http://www.cppblog.com/images/cppblog_com/zhiyewang/07.png" width="669" height="169" alt="" /><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">说明安装环境是没问题了。</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>11&nbsp;<span style="font-family: 宋体;">安装</span>openldap<span style="font-family: 宋体;">到系统</span></strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">根据官方文档说明：</span>By default OpenLDAP Software is installed in /usr/local. If youchanged this setting with the --prefix configure option, it will be installedin the location you provided.</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">我们可以加</span>&#8211;prefix&nbsp;<span style="font-family: 宋体;">参数来指定我们自己想要安装的位置。默认是安装到</span>&nbsp;/usr/local<span style="font-family: 宋体;">目录下。</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">执行命令：</span>#&nbsp;<span style="color: red;">su root -c 'make install'</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><img src="http://www.cppblog.com/images/cppblog_com/zhiyewang/08.png" width="779" height="109" alt="" /><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 宋体;">到这里</span>openldap-2.4.44<span style="font-family: 宋体;">已经成功的安装到我的系统当中。默认的配置文件在</span>&nbsp;<span style="color: #ff0000;">/usr/local/etc/openldap</span>&nbsp;<span style="font-family: 宋体;">下。</span></p></div><div><span style="font-family: 宋体;"><br /></span></div><img src ="http://www.cppblog.com/zhiyewang/aggbug/213311.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhiyewang/" target="_blank">zhiye_wang</a> 2016-04-20 17:27 <a href="http://www.cppblog.com/zhiyewang/archive/2016/04/20/213311.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>CentOS安装运行mongo docker镜像</title><link>http://www.cppblog.com/zhiyewang/archive/2016/04/13/213258.html</link><dc:creator>zhiye_wang</dc:creator><author>zhiye_wang</author><pubDate>Wed, 13 Apr 2016 03:42:00 GMT</pubDate><guid>http://www.cppblog.com/zhiyewang/archive/2016/04/13/213258.html</guid><wfw:comment>http://www.cppblog.com/zhiyewang/comments/213258.html</wfw:comment><comments>http://www.cppblog.com/zhiyewang/archive/2016/04/13/213258.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhiyewang/comments/commentRss/213258.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhiyewang/services/trackbacks/213258.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: CentOS安装运行mongo docker镜像背景：&nbsp;根据需要，我需要安装一个mongoDB数据库，并且需要通过docker镜像作为容器运行这个 mongoDB 数据库步骤：1 下载镜像首先从 docker hub 下载 mongoDB 的 docker 镜像使用 docker search 命令查看有哪些可以下载的镜像12345[root@bogon&nbsp;test]# dock...&nbsp;&nbsp;<a href='http://www.cppblog.com/zhiyewang/archive/2016/04/13/213258.html'>阅读全文</a><img src ="http://www.cppblog.com/zhiyewang/aggbug/213258.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhiyewang/" target="_blank">zhiye_wang</a> 2016-04-13 11:42 <a href="http://www.cppblog.com/zhiyewang/archive/2016/04/13/213258.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Go 语言教程笔记（三）</title><link>http://www.cppblog.com/zhiyewang/archive/2016/03/28/213139.html</link><dc:creator>zhiye_wang</dc:creator><author>zhiye_wang</author><pubDate>Mon, 28 Mar 2016 08:54:00 GMT</pubDate><guid>http://www.cppblog.com/zhiyewang/archive/2016/03/28/213139.html</guid><wfw:comment>http://www.cppblog.com/zhiyewang/comments/213139.html</wfw:comment><comments>http://www.cppblog.com/zhiyewang/archive/2016/03/28/213139.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhiyewang/comments/commentRss/213139.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhiyewang/services/trackbacks/213139.html</trackback:ping><description><![CDATA[<p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong><span style="font-family: 微软雅黑, sans-serif;">Go 语言教程笔记（</span><span style="font-family: 微软雅黑, sans-serif;">三</span><span style="font-family: 微软雅黑, sans-serif;">）</span></strong><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>一 Go语言决策</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>if 语句</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">if语句包含一个布尔表达式后跟一个或多个语句</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">语法</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">if语句在Go编程语言的语法是：</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">if(boolean_expression)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">{</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;&nbsp;/*&nbsp;statement(s)&nbsp;will&nbsp;execute&nbsp;if&nbsp;the&nbsp;boolean&nbsp;expression&nbsp;is&nbsp;true&nbsp;*/</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">}&nbsp;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">如果布尔表达式的值为&nbsp;true，那么if语句里面代码块将被执行。如果if语句的结束(右大括号后)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">布尔表达式的值为false，那么语句之后第一行代码会被执行。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">例子：</p><pre tryit"="" style="box-sizing: border-box; font-family: 'Courier New', Courier, monospace; overflow: auto; padding: 0.5em; line-height: 16px; word-break: break-all; word-wrap: break-word; color: #313131; border-width: 1px 1px 1px 3px; border-style: solid; border-color: #cccccc; border-radius: 0px; white-space: pre-wrap; width: 604px; cursor: default; background-image: url(&quot;http://www.yiibai.com/go/images/try-it.jpg&quot;) !important; background-color: #f5f5f5; background-position: 100% 0%; background-repeat: no-repeat !important;">package main  import "fmt"  func main() {    /* local variable definition */    var a int = 10      /* check the boolean condition using if statement */    if( a &lt; 20 ) {        /* if condition is true then print the following */        fmt.Printf("a is less than 20\n" )    }    fmt.Printf("value of a is : %d\n", a) }</pre><p style="margin-right: 0px; margin-left: 0px; box-sizing: border-box; font-family: 'Open Sans', Arial, 'Hiragino Sans GB', 'Microsoft YaHei', 微软雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif, sans-serif; font-size: 16px; color: #535b60; line-height: 22px;">让我们编译和运行上面的程序，这将产生以下结果：</p><pre style="box-sizing: border-box; font-family: 'Courier New', Courier, monospace; overflow: auto; padding: 0.5em; margin-bottom: 0px; line-height: 22px; word-break: break-all; word-wrap: break-word; color: #313131; border-width: 1px 1px 1px 3px; border-style: solid; border-color: #cccccc; border-radius: 0px; white-space: pre-wrap; width: 604px; background-color: #f1f1f1;">a is less than 20; value of a is : 10</pre><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>if else 语句</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">if语句可以跟着一个可选的else语句，布尔表达式是假时它被执行。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">语法</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">在Go编程语言中的if&nbsp;...&nbsp;else语句的语法是：</p><pre style="box-sizing: border-box; font-family: 'Courier New', Courier, monospace; overflow: auto; padding: 0.5em; line-height: 16px; word-break: break-all; word-wrap: break-word; color: #313131; border-width: 1px 1px 1px 3px; border-style: solid; border-color: #cccccc; border-radius: 0px; white-space: pre-wrap; width: 604px; background-color: #f5f5f5;">if(boolean_expression) {    /* statement(s) will execute if the boolean expression is true */ } else {   /* statement(s) will execute if the boolean expression is false */ }</pre><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">如果布尔表达式的值为true，那么if代码块将被执行，否则else代码块将被执行</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">例子：</p><pre tryit"="" style="box-sizing: border-box; font-family: 'Courier New', Courier, monospace; overflow: auto; padding: 0.5em; line-height: 16px; word-break: break-all; word-wrap: break-word; color: #313131; border-width: 1px 1px 1px 3px; border-style: solid; border-color: #cccccc; border-radius: 0px; white-space: pre-wrap; width: 604px; cursor: default; background-image: url(&quot;http://www.yiibai.com/go/images/try-it.jpg&quot;) !important; background-color: #f5f5f5; background-position: 100% 0%; background-repeat: no-repeat !important;">package main  import "fmt"  func main() {    /* local variable definition */    var a int = 100;      /* check the boolean condition */    if( a &lt; 20 ) {        /* if condition is true then print the following */        fmt.Printf("a is less than 20\n" );    } else {        /* if condition is false then print the following */        fmt.Printf("a is not less than 20\n" );    }    fmt.Printf("value of a is : %d\n", a);  }</pre><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">当上述代码被编译和执行时，它产生了以下结果：</p><pre style="box-sizing: border-box; font-family: 'Courier New', Courier, monospace; overflow: auto; padding: 0.5em; margin-bottom: 0px; line-height: 22px; word-break: break-all; word-wrap: break-word; color: #313131; border-width: 1px 1px 1px 3px; border-style: solid; border-color: #cccccc; border-radius: 0px; white-space: pre-wrap; width: 604px; background-color: #f1f1f1;">a is not less than 20; value of a is : 100</pre><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>if...else&nbsp;if...else&nbsp;语句</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">if语句可以跟着一个可选的else&nbsp;if&nbsp;...&nbsp;else语句，这是非常有用的使用单个&nbsp;if...else&nbsp;if&nbsp;语句声明测试各种条件。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">当使用if&nbsp;,&nbsp;else&nbsp;if&nbsp;,&nbsp;else语句有几点要记住使用：</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">if可以有零或一个else，它必须跟从else&nbsp;if后面。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">一个if可以有零到个多else&nbsp;if并且它们必须在else之前。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">一旦一个else&nbsp;if测试成功，其它任何剩余else&nbsp;if将不会被测试。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">语法</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">if...else&nbsp;if...else在Go编程语言中语句的语法是：</p><pre style="box-sizing: border-box; font-family: 'Courier New', Courier, monospace; overflow: auto; padding: 0.5em; line-height: 16px; word-break: break-all; word-wrap: break-word; color: #313131; border-width: 1px 1px 1px 3px; border-style: solid; border-color: #cccccc; border-radius: 0px; white-space: pre-wrap; width: 604px; background-color: #f5f5f5;">if(boolean_expression 1) {    /* Executes when the boolean expression 1 is true */ } else if( boolean_expression 2) {    /* Executes when the boolean expression 2 is true */ } else if( boolean_expression 3) {    /* Executes when the boolean expression 3 is true */ } else  {    /* executes when the none of the above condition is true */ }</pre><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">例子：</p><pre tryit"="" style="box-sizing: border-box; font-family: 'Courier New', Courier, monospace; overflow: auto; padding: 0.5em; line-height: 16px; word-break: break-all; word-wrap: break-word; color: #313131; border-width: 1px 1px 1px 3px; border-style: solid; border-color: #cccccc; border-radius: 0px; white-space: pre-wrap; width: 604px; cursor: default; background-image: url(&quot;http://www.yiibai.com/go/images/try-it.jpg&quot;) !important; background-color: #f5f5f5; background-position: 100% 0%; background-repeat: no-repeat !important;">package main  import "fmt"  func main() {    /* local variable definition */    var a int = 100      /* check the boolean condition */    if( a == 10 ) {        /* if condition is true then print the following */        fmt.Printf("Value of a is 10\n" )    } else if( a == 20 ) {        /* if else if condition is true */        fmt.Printf("Value of a is 20\n" )    } else if( a == 30 ) {        /* if else if condition is true  */        fmt.Printf("Value of a is 30\n" )    } else {        /* if none of the conditions is true */        fmt.Printf("None of the values is matching\n" )    }    fmt.Printf("Exact value of a is: %d\n", a ) }</pre><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">让我们编译和运行上面的程序，这将产生以下结果</p><pre style="box-sizing: border-box; font-family: 'Courier New', Courier, monospace; overflow: auto; padding: 0.5em; margin-bottom: 0px; line-height: 22px; word-break: break-all; word-wrap: break-word; color: #313131; border-width: 1px 1px 1px 3px; border-style: solid; border-color: #cccccc; border-radius: 0px; white-space: pre-wrap; width: 604px; background-color: #f1f1f1;">None of the values is matching Exact value of a is: 100</pre><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>switch 语句</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">switch语句可以让一个变量对反对值的列表平等进行测试。每个值被称为一个的情况(case)，</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">变量被接通检查每个开关盒(switch&nbsp;case)。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">在Go编程，switch有两种类型。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;<span style="font-family: 微软雅黑, sans-serif;">表达式Switch&nbsp;-&nbsp;在表达式switch，case包含相比较，switch表达式的值。</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;<span style="font-family: 微软雅黑, sans-serif;">类型Switch&nbsp;-&nbsp;在这类型switch，此时含有进行比较特殊注明开关表达式的类型。</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>&nbsp;<span style="font-family: 微软雅黑, sans-serif;">表达式Switch</span></strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">在Go编程语言中表达switch语句的语法如下：</p><pre style="box-sizing: border-box; font-family: 'Courier New', Courier, monospace; overflow: auto; padding: 0.5em; line-height: 16px; word-break: break-all; word-wrap: break-word; color: #313131; border-width: 1px 1px 1px 3px; border-style: solid; border-color: #cccccc; border-radius: 0px; white-space: pre-wrap; width: 604px; background-color: #f5f5f5;">switch(boolean-expression or integral type){     case boolean-expression or integral type  :        statement(s);           case boolean-expression or integral type  :        statement(s);      /* you can have any number of case statements */     default : /* Optional */        statement(s); }</pre><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">以下规则适用于switch语句：</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">在switch语句中使用的表达式必须具有整体或布尔表达式，或者是一个类型，其中所述类具有</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">一个单一的转换函数，以一个整体或布尔值。如果表达不通过，默认值是true。<span style="font-family: 微软雅黑, sans-serif;">可以有任意数</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 微软雅黑, sans-serif;">量的case语句在switch内。每个case后跟值进行比较，以及一个冒号。</span><span style="font-family: 微软雅黑, sans-serif;">constant-expression&nbsp;</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 微软雅黑, sans-serif;">的情况，必须是相同的数据类型，在switch的变量，它必须是一个常量</span><span style="font-family: 微软雅黑, sans-serif;">或文字。</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">当变量被接通等于case的值，以下case中将执行语句。在case语句中break不是必需。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">switch语句可以有一个可选默认情况下，它必须出现在开关结束。缺省情况下，可用于执行任</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">务时没有的case为true。则case在默认情况下也不是必须的。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">例子</p><pre tryit"="" style="box-sizing: border-box; font-family: 'Courier New', Courier, monospace; overflow: auto; padding: 0.5em; line-height: 16px; word-break: break-all; word-wrap: break-word; color: #313131; border-width: 1px 1px 1px 3px; border-style: solid; border-color: #cccccc; border-radius: 0px; white-space: pre-wrap; width: 604px; cursor: default; background-image: url(&quot;http://www.yiibai.com/go/images/try-it.jpg&quot;) !important; background-color: #f5f5f5; background-position: 100% 0%; background-repeat: no-repeat !important;">package main  import "fmt"  func main() {    /* local variable definition */    var grade string = "B"    var marks int = 90     switch marks {       case 90: grade = "A"       case 80: grade = "B"       case 50,60,70 : grade = "C"       default: grade = "D"      }     switch {       case grade == "A" :          fmt.Printf("Excellent!\n" )            case grade == "B", grade == "C" :          fmt.Printf("Well done\n" )             case grade == "D" :          fmt.Printf("You passed\n" )             case grade == "F":          fmt.Printf("Better try again\n" )       default:          fmt.Printf("Invalid grade\n" );    }    fmt.Printf("Your grade is  %s\n", grade );       }</pre><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">当上述代码被编译和执行时，它产生了以下结果：</p><pre style="box-sizing: border-box; font-family: 'Courier New', Courier, monospace; overflow: auto; padding: 0.5em; margin-bottom: 0px; line-height: 22px; word-break: break-all; word-wrap: break-word; color: #313131; border-width: 1px 1px 1px 3px; border-style: solid; border-color: #cccccc; border-radius: 0px; white-space: pre-wrap; width: 604px; background-color: #f1f1f1;">Well done Excellent! Your grade is  A</pre><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>类型Switch</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">在Go编程语言的一个类型switch语句的语法如下：</p><pre style="box-sizing: border-box; font-family: 'Courier New', Courier, monospace; overflow: auto; padding: 0.5em; line-height: 16px; word-break: break-all; word-wrap: break-word; color: #313131; border-width: 1px 1px 1px 3px; border-style: solid; border-color: #cccccc; border-radius: 0px; white-space: pre-wrap; width: 604px; background-color: #f5f5f5;">switch x.(type){     case type:        statement(s);           case type:        statement(s);      /* you can have any number of case statements */     default: /* Optional */        statement(s); }</pre><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">以下规则适用于switch语句：</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">在switch语句中使用必须有接口的变量表达式{}输入。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">在switch内可以有任意数量case语句。每一种case后跟的值进行比较，以及一个冒号。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">case 类型必须是相同的数据类型，在switch的变量，它必须是一个有效的数据类型。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">当变量被接通等于某一case中的值，以下case语句将执行。在case语句块的break不是必需的。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">switch语句可以有一个可选默认case，它必须出现在switch的结束。缺省情况下，可用于执行</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">任务时没有匹配case时。default不是必需的。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">例子</p><pre tryit"="" style="box-sizing: border-box; font-family: 'Courier New', Courier, monospace; overflow: auto; padding: 0.5em; line-height: 16px; word-break: break-all; word-wrap: break-word; color: #313131; border-width: 1px 1px 1px 3px; border-style: solid; border-color: #cccccc; border-radius: 0px; white-space: pre-wrap; width: 604px; cursor: default; background-image: url(&quot;http://www.yiibai.com/go/images/try-it.jpg&quot;) !important; background-color: #f5f5f5; background-position: 100% 0%; background-repeat: no-repeat !important;">package main  import "fmt"  func main() {    var x interface{}          switch i := x.(type) {       case nil:	            fmt.Printf("type of x :%T",i)                       case int:	            fmt.Printf("x is int")                              case float64:          fmt.Printf("x is float64")                  case func(int) float64:          fmt.Printf("x is func(int)")                             case bool, string:          fmt.Printf("x is bool or string")              default:          fmt.Printf("don't know the type")         }    }</pre><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">让我们编译和运行上面的程序，这将产生以下结果：</p><pre style="box-sizing: border-box; font-family: 'Courier New', Courier, monospace; overflow: auto; padding: 0.5em; margin-bottom: 0px; line-height: 22px; word-break: break-all; word-wrap: break-word; color: #313131; border-width: 1px 1px 1px 3px; border-style: solid; border-color: #cccccc; border-radius: 0px; white-space: pre-wrap; width: 604px; background-color: #f1f1f1;">type of x :&lt;nil&gt;</pre><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>&nbsp;select语句</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">select语句的语法如下：</p><pre style="box-sizing: border-box; font-family: 'Courier New', Courier, monospace; overflow: auto; padding: 0.5em; line-height: 16px; word-break: break-all; word-wrap: break-word; color: #313131; border-width: 1px 1px 1px 3px; border-style: solid; border-color: #cccccc; border-radius: 0px; white-space: pre-wrap; width: 604px; background-color: #f5f5f5;">select {     case communication clause  :        statement(s);           case communication clause  :        statement(s);      /* you can have any number of case statements */     default : /* Optional */        statement(s); }</pre><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">以下规则适用于select语句：</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">可以有任意数量的范围内选择一个case语句。每一种情况下后跟的值进行比较，以及一个冒号。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">对于case的类型必须是一个通信通道操作。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">当通道运行下面发生的语句这种情况将执行。在case语句中break不是必需的。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">select语句可以有一个可选默认case，它必须出现在select的结束前。缺省情况下，可用于执行</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">任务时没有的情况下是真实的。在默认情况下break不是必需的。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">例如：</p><pre tryit"="" style="box-sizing: border-box; font-family: 'Courier New', Courier, monospace; overflow: auto; padding: 0.5em; line-height: 16px; word-break: break-all; word-wrap: break-word; color: #313131; border-width: 1px 1px 1px 3px; border-style: solid; border-color: #cccccc; border-radius: 0px; white-space: pre-wrap; width: 604px; cursor: default; background-image: url(&quot;http://www.yiibai.com/go/images/try-it.jpg&quot;) !important; background-color: #f5f5f5; background-position: 100% 0%; background-repeat: no-repeat !important;">package main  import "fmt"  func main() {    var c1, c2, c3 chan int    var i1, i2 int    select {       case i1 = &lt;-c1:          fmt.Printf("received ", i1, " from c1\n")       case c2 &lt;- i2:          fmt.Printf("sent ", i2, " to c2\n")       case i3, ok := (&lt;-c3):  // same as: i3, ok := &lt;-c3          if ok {             fmt.Printf("received ", i3, " from c3\n")          } else {             fmt.Printf("c3 is closed\n")          }       default:          fmt.Printf("no communication\n")    }     }   </pre><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">让我们编译和运行上面的程序，这将产生以下结果：</p><pre style="box-sizing: border-box; font-family: 'Courier New', Courier, monospace; overflow: auto; padding: 0.5em; margin-bottom: 0px; line-height: 22px; word-break: break-all; word-wrap: break-word; color: #313131; border-width: 1px 1px 1px 3px; border-style: solid; border-color: #cccccc; border-radius: 0px; white-space: pre-wrap; width: 604px; background-color: #f1f1f1;">no communication</pre><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&#8203;</p><img src ="http://www.cppblog.com/zhiyewang/aggbug/213139.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhiyewang/" target="_blank">zhiye_wang</a> 2016-03-28 16:54 <a href="http://www.cppblog.com/zhiyewang/archive/2016/03/28/213139.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Go 语言教程笔记（二</title><link>http://www.cppblog.com/zhiyewang/archive/2016/03/28/213136.html</link><dc:creator>zhiye_wang</dc:creator><author>zhiye_wang</author><pubDate>Mon, 28 Mar 2016 08:17:00 GMT</pubDate><guid>http://www.cppblog.com/zhiyewang/archive/2016/03/28/213136.html</guid><wfw:comment>http://www.cppblog.com/zhiyewang/comments/213136.html</wfw:comment><comments>http://www.cppblog.com/zhiyewang/archive/2016/03/28/213136.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhiyewang/comments/commentRss/213136.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhiyewang/services/trackbacks/213136.html</trackback:ping><description><![CDATA[<p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong style="font-family: 微软雅黑, sans-serif;">Go 语言教程笔记（二）</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>一 Go 语言常量</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">常量是指该程序可能无法在其执行期间改变的固定值。这些固定值也被称为文字。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">常量可以是任何像一个整型常量，一个浮点常量，字符常量或字符串文字的基本数据类型。还</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">有枚举常量。<span style="font-family: 微软雅黑, sans-serif;">常量是一样，只是它们的值不能自己定义后进行修改常规变量处理。</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>整形常量</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">一个整数文字也可以有一个后缀为U和L的组合，分别为无符号和长整型。后缀可以是大写或小</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">写，并且可以以任意顺序。<span style="font-family: 微软雅黑, sans-serif;">可以是十进制，八进制，或十六进制常数。前缀指定基或基数：0x</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 微软雅黑, sans-serif;">或 0X 的十六进制，0 表示八进制，并没有十进制。</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">整数常量的一些例子：</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">212&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Legal&nbsp;*/</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">215u&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Legal&nbsp;*/</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">0xFeeL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Legal&nbsp;*/</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">078&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Illegal:&nbsp;8&nbsp;is&nbsp;not&nbsp;an&nbsp;octal&nbsp;digit&nbsp;*/</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">032UU&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Illegal:&nbsp;cannot&nbsp;repeat&nbsp;a&nbsp;suffix&nbsp;*/</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">不同类型的整型常量的例子：</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">85&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;decimal&nbsp;*/</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">0213&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;octal&nbsp;*/</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">0x4b&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;hexadecimal&nbsp;*/</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">30&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;int&nbsp;*/</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">30u&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;unsigned&nbsp;int&nbsp;*/</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">30l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;long&nbsp;*/</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">30ul&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;unsigned&nbsp;long&nbsp;*/</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>浮点文本(常量)</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">浮点字面具有一个整数部分，一个小数点，一个小数部分，和一个指数部分。你可以表示十进</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">制形式或指数形式浮点文字。<span style="font-family: 微软雅黑, sans-serif;">同时采用十进制形式表示，则必须包括小数点，指数，或两者并</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 微软雅黑, sans-serif;">用而指数形式表示，则必须包括整数部分，小数部分，或者两者兼而有之。有符号的指数，通</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 微软雅黑, sans-serif;">过e或E表示。</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">下面是浮点面值的一些例子：</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">3.14159&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Legal&nbsp;*/</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">314159E-5L&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Legal&nbsp;*/</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">510E&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Illegal:&nbsp;incomplete&nbsp;exponent&nbsp;*/</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">210f&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Illegal:&nbsp;no&nbsp;decimal&nbsp;or&nbsp;exponent&nbsp;*/</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">.e55&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Illegal:&nbsp;missing&nbsp;integer&nbsp;or&nbsp;fraction&nbsp;*/</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>字符串文字</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">字符串文字或常量用双引号&#8220;&#8221;。一个字符串包含类似于字符文字字符：普通字符，转义序列</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">和通用字符。<span style="font-family: 微软雅黑, sans-serif;">可以使用字符串和分隔使用空格打破一个长行成多行。</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">"hello,&nbsp;dear"</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">"hello,&nbsp;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">dear"</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">"hello,&nbsp;"&nbsp;"d"&nbsp;"ear"</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>const&nbsp;关键字</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">package&nbsp;main</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">import&nbsp;"fmt"</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">func&nbsp;main()&nbsp;{</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;&nbsp;const&nbsp;LENGTH&nbsp;int&nbsp;=&nbsp;10</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;&nbsp;const&nbsp;WIDTH&nbsp;int&nbsp;=&nbsp;5&nbsp;&nbsp;&nbsp;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;&nbsp;var&nbsp;area&nbsp;int</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;&nbsp;area&nbsp;=&nbsp;LENGTH&nbsp;*&nbsp;WIDTH</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;&nbsp;fmt.Printf("value&nbsp;of&nbsp;area&nbsp;:&nbsp;%d",&nbsp;area)&nbsp;&nbsp;&nbsp;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">}</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">习惯大写定义常量<span style="font-family: 微软雅黑, sans-serif;">是一个良好的编程习惯。</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>二 Go 语言运算符</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">Go语言有丰富的内置运算符和运算符：</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">算术运算符</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">关系运算符</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">逻辑运算符</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">位运算符</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">赋值运算符</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">其它运算符</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>算术运算符</strong></p><table width="604" style="clear: both; margin-bottom: 10px; border-collapse: collapse; word-break: break-all; color: #000000; font-family: 微软雅黑; font-size: 14px; widows: auto;"><tbody style="box-sizing: border-box; font-size: 16px;"><tr style="box-sizing: border-box;"><th style="padding: 5px; border: 1px solid #d6d6d6; background-color: #eeeeee; box-sizing: border-box; font-size: 11pt; margin: 0px; border-collapse: collapse; line-height: 1.42857; vertical-align: top; width: 76px; background-position: initial initial; background-repeat: initial initial;">运算符</th><th style="padding: 5px; border: 1px solid #d6d6d6; background-color: #eeeeee; box-sizing: border-box; font-size: 11pt; margin: 0px; border-collapse: collapse; line-height: 1.42857; vertical-align: top; width: 331px; background-position: initial initial; background-repeat: initial initial;">描述</th><th style="padding: 5px; border: 1px solid #d6d6d6; background-color: #eeeeee; box-sizing: border-box; font-size: 11pt; margin: 0px; border-collapse: collapse; line-height: 1.42857; vertical-align: top; background-position: initial initial; background-repeat: initial initial;">示例</th></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">+</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">两个操作数相加</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">A + B = 30</td></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">-</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">第一个操作数减第二操作数</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">A - B = -10</td></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">*</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">两个操作数相乘</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">A * B = 200</td></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">/</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">通过去分子除以分母</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">B / A = 2</td></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">%</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">模运算和整数除法后的余数</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">B % A = 0</td></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">++</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">运算符递增整数值增加一</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">A++ = 11</td></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">--</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">运算符递减整数值减一</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">A-- =&nbsp;9</td></tr></tbody></table><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>关系运算符</strong></p><table width="604" style="clear: both; margin-bottom: 10px; border-collapse: collapse; word-break: break-all; color: #000000; font-family: 微软雅黑; font-size: 14px; widows: auto;"><tbody style="box-sizing: border-box; font-size: 16px;"><tr style="box-sizing: border-box;"><th style="padding: 5px; border: 1px solid #d6d6d6; background-color: #eeeeee; box-sizing: border-box; font-size: 11pt; margin: 0px; border-collapse: collapse; line-height: 1.42857; vertical-align: top; width: 76px; background-position: initial initial; background-repeat: initial initial;">运算符</th><th style="padding: 5px; border: 1px solid #d6d6d6; background-color: #eeeeee; box-sizing: border-box; font-size: 11pt; margin: 0px; border-collapse: collapse; line-height: 1.42857; vertical-align: top; width: 331px; background-position: initial initial; background-repeat: initial initial;">描述</th><th style="padding: 5px; border: 1px solid #d6d6d6; background-color: #eeeeee; box-sizing: border-box; font-size: 11pt; margin: 0px; border-collapse: collapse; line-height: 1.42857; vertical-align: top; background-position: initial initial; background-repeat: initial initial;">示例</th></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">==</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">检查两个操作数的值是否相等，如果是的话那么条件为真。</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">(A == B) 不为 true.</td></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">!=</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">检查两个操作数的值是否相等，如果值不相等，则条件变为真。</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">(A != B) 为true.</td></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">&gt;</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">检查左边的操作数的值是否大于右操作数的值，如果是的话那么条件为真。</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">(A &gt; B)&nbsp;不为&nbsp;true.</td></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">&lt;</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">检查左边的操作数的值是否小于右操作数的值，如果是的话那么条件为真。</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">(A &lt; B)&nbsp;为&nbsp;true.</td></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">&gt;=</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">检查左边的操作数的值是否大于或等于右操作数的值，如果是的话那么条件为真。</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">(A &gt;= B)&nbsp;不为&nbsp;true.</td></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">&lt;=</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">检查左边的操作数的值是否小于或等于右操作数的值，如果是的话那么条件为真。</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">(A &lt;= B)&nbsp;为&nbsp;true</td></tr></tbody></table><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong><span style="font-family: 微软雅黑, sans-serif;">逻辑运算符</span></strong><br /></p><table width="604" style="clear: both; margin-bottom: 10px; border-collapse: collapse; word-break: break-all; color: #000000; font-family: 微软雅黑; font-size: 14px; widows: auto;"><tbody style="box-sizing: border-box; font-size: 16px;"><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">&amp;&amp;</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">所谓逻辑与运算符。如果两个操作数都非零，则条件变为真。</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">(A &amp;&amp; B) 为 false.</td></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">||</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">所谓的逻辑或操作。如果任何两个操作数是非零，则条件变为真。</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">(A || B)&nbsp;为&nbsp;true.</td></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">!</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">所谓逻辑非运算符。使用反转操作数的逻辑状态。如果条件为真，那么逻辑非操后结果为假。</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">!(A &amp;&amp; B)&nbsp;为&nbsp;true.</td></tr></tbody></table><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>位运算符</strong></p><table width="604" style="clear: both; margin-bottom: 10px; border-collapse: collapse; word-break: break-all; color: #000000; font-family: 微软雅黑; font-size: 14px; widows: auto;"><tbody style="box-sizing: border-box; font-size: 16px;"><tr style="box-sizing: border-box;"><th style="padding: 5px; border: 1px solid #d6d6d6; background-color: #eeeeee; box-sizing: border-box; font-size: 11pt; margin: 0px; border-collapse: collapse; line-height: 1.42857; vertical-align: top; width: 121px; background-position: initial initial; background-repeat: initial initial;">p</th><th style="padding: 5px; border: 1px solid #d6d6d6; background-color: #eeeeee; box-sizing: border-box; font-size: 11pt; margin: 0px; border-collapse: collapse; line-height: 1.42857; vertical-align: top; width: 121px; background-position: initial initial; background-repeat: initial initial;">q</th><th style="padding: 5px; border: 1px solid #d6d6d6; background-color: #eeeeee; box-sizing: border-box; font-size: 11pt; margin: 0px; border-collapse: collapse; line-height: 1.42857; vertical-align: top; width: 121px; background-position: initial initial; background-repeat: initial initial;">p &amp; q</th><th style="padding: 5px; border: 1px solid #d6d6d6; background-color: #eeeeee; box-sizing: border-box; font-size: 11pt; margin: 0px; border-collapse: collapse; line-height: 1.42857; vertical-align: top; width: 120px; background-position: initial initial; background-repeat: initial initial;">p | q</th><th style="padding: 5px; border: 1px solid #d6d6d6; background-color: #eeeeee; box-sizing: border-box; font-size: 11pt; margin: 0px; border-collapse: collapse; line-height: 1.42857; vertical-align: top; width: 120px; background-position: initial initial; background-repeat: initial initial;">p ^ q</th></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">0</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">0</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">0</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">0</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">0</td></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">0</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">1</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">0</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">1</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">1</td></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">1</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">1</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">1</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">1</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">0</td></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">1</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">0</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">0</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">1</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">1</td></tr></tbody></table><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">假设，如果A&nbsp;=60;且b&nbsp;=13;现在以二进制格式它们如下：</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">A&nbsp;=&nbsp;0011&nbsp;1100</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">B&nbsp;=&nbsp;0000&nbsp;1101</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">-----------------</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">A&amp;B&nbsp;=&nbsp;0000&nbsp;1100</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">A|B&nbsp;=&nbsp;0011&nbsp;1101</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">A^B&nbsp;=&nbsp;0011&nbsp;0001</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">~A&nbsp;&nbsp;=&nbsp;1100&nbsp;0011</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">C语言支持位运算符列在如下表。假设变量A=60和变量B=13，则</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>其它运算符</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">还有其他一些重要的运算符，包括sizeof和?:在Go语言中也支持。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>Go语言运算符优先级</strong></p><table width="604" style="clear: both; margin-bottom: 10px; border-collapse: collapse; word-break: break-all; color: #000000; font-family: 微软雅黑; font-size: 14px; widows: auto;"><tbody style="box-sizing: border-box; font-size: 16px;"><tr style="box-sizing: border-box;"><th style="padding: 5px; border: 1px solid #d6d6d6; background-color: #eeeeee; box-sizing: border-box; font-size: 11pt; margin: 0px; border-collapse: collapse; line-height: 1.42857; vertical-align: top; background-position: initial initial; background-repeat: initial initial;">分类&nbsp;</th><th style="padding: 5px; border: 1px solid #d6d6d6; background-color: #eeeeee; box-sizing: border-box; font-size: 11pt; margin: 0px; border-collapse: collapse; line-height: 1.42857; vertical-align: top; background-position: initial initial; background-repeat: initial initial;">运算符&nbsp;</th><th style="padding: 5px; border: 1px solid #d6d6d6; background-color: #eeeeee; box-sizing: border-box; font-size: 11pt; margin: 0px; border-collapse: collapse; line-height: 1.42857; vertical-align: top; background-position: initial initial; background-repeat: initial initial;">关联&nbsp;</th></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">后缀</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">() [] -&gt; . ++ - - &nbsp;</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">从左到右</td></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">一元</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">+ - ! ~ ++ - - (type)* &amp; sizeof&nbsp;</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">从右到左</td></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">乘法</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">* / %&nbsp;</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">从左到右</td></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">相加 &nbsp;</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">+ -&nbsp;</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">从左到右&nbsp;</td></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">移动</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">&lt;&lt; &gt;&gt;&nbsp;</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">从左到右</td></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">关系</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">&lt; &lt;= &gt; &gt;=&nbsp;</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">从左到右</td></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">相等</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">== !=&nbsp;</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">从左到右</td></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">按位与</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">&amp;&nbsp;</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">从左到右</td></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">按位异或</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">^&nbsp;</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">从左到右</td></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">按位或&nbsp;</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">|&nbsp;</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">从左到右</td></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">逻辑与&nbsp;</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">&amp;&amp;&nbsp;</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">从左到右</td></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">逻辑或&nbsp;</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">||&nbsp;</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">从左到右</td></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">条件&nbsp;</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">?:&nbsp;</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">从左到右</td></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">赋值</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">= += -= *= /= %=&gt;&gt;= &lt;&lt;= &amp;= ^= |=&nbsp;</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">从右到左</td></tr><tr style="box-sizing: border-box;"><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">逗号&nbsp;</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">,&nbsp;</td><td style="padding: 5px; border-style: solid; border-color: #d6d6d6; box-sizing: border-box; font-size: 11pt; line-height: 1.42857; vertical-align: top; border-collapse: collapse;">从左到右</td></tr></tbody></table><span style="font-family: 微软雅黑; widows: auto;">&#8203;</span><img src ="http://www.cppblog.com/zhiyewang/aggbug/213136.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhiyewang/" target="_blank">zhiye_wang</a> 2016-03-28 16:17 <a href="http://www.cppblog.com/zhiyewang/archive/2016/03/28/213136.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Go 语言教程笔记（一）</title><link>http://www.cppblog.com/zhiyewang/archive/2016/03/28/213135.html</link><dc:creator>zhiye_wang</dc:creator><author>zhiye_wang</author><pubDate>Mon, 28 Mar 2016 07:33:00 GMT</pubDate><guid>http://www.cppblog.com/zhiyewang/archive/2016/03/28/213135.html</guid><wfw:comment>http://www.cppblog.com/zhiyewang/comments/213135.html</wfw:comment><comments>http://www.cppblog.com/zhiyewang/archive/2016/03/28/213135.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhiyewang/comments/commentRss/213135.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhiyewang/services/trackbacks/213135.html</trackback:ping><description><![CDATA[<p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>Go&nbsp;语言教程笔记（一）</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>一 Go 语言环境设置&nbsp;</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>本地环境设置</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">在这里我们介绍设置Go编程语言环境，需要在你的计算机上的准备以下两个软件，(A)文本编</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">辑器和(B)Go编译器。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>文本编辑器</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">我的环境是ubuntu，直接用 vim</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>Go编译器</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">ubuntu下面直接安装</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"># apt-get install golang</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">例子：</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">#&nbsp;vim&nbsp;hello.go</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;1&nbsp;package&nbsp;main</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;2&nbsp;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;3&nbsp;import&nbsp;fmt&nbsp;"fmt"</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;4&nbsp;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;5&nbsp;func&nbsp;main()&nbsp;{</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;6&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fmt.Printf("Hello,&nbsp;world;&nbsp;or&nbsp;&#922;&#945;&#955;&#951;&#956;&#941;&#961;&#945;&nbsp;&#954;&#972;&#963;&#956;&#949;;&nbsp;or&nbsp;こんにちは&nbsp;世界 ");</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;7&nbsp;} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">go语言中如果有if等后面需要跟大括号的语句，强制使用大扩号跟if写在一行<span style="font-family: 微软雅黑, sans-serif;">。</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 微软雅黑, sans-serif;"><br /></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 微软雅黑, sans-serif;">编译运行：</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">#&nbsp;go&nbsp;run&nbsp;hello.go&nbsp;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">Hello,&nbsp;world;&nbsp;or&nbsp;&#922;&#945;&#955;&#951;&#956;&#941;&#961;&#945;&nbsp;&#954;&#972;&#963;&#956;&#949;;&nbsp;or&nbsp;こんにちは&nbsp;世界</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong><span style="font-family: 微软雅黑, sans-serif;">二 Go 语言程序结构&nbsp;</span></strong><span style="font-family: 微软雅黑, sans-serif;"><br /></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">在我们学习Go编程语言的基本构建模块，看看一个最低限度的Go程序结构，这样我们</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">就可以把它作为即将到来的章节的参考。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>Go 程序包含以下部分</strong>：<br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">包声明<br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">导入包</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">函数</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">变量</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">语句和表达式</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">注释</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">例如：<br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;1&nbsp;package&nbsp;main</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;2&nbsp;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;3&nbsp;import&nbsp;fmt&nbsp;"fmt"</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;4&nbsp;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;5&nbsp;func&nbsp;main()&nbsp;{</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;6&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fmt.Printf("Hello,&nbsp;world;&nbsp;or&nbsp;&#922;&#945;&#955;&#951;&#956;&#941;&#961;&#945;&nbsp;&#954;&#972;&#963;&#956;&#949;;&nbsp;or&nbsp;こんにちは&nbsp;世界 ");</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;7&nbsp;} &nbsp;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">第一行定义了这个程序包的名称。这是一个必须声明为Go程序运行在什么包。main包是起始</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">点来运行程序。每个包都有一个与之关联的路径和名称。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">下一行import&nbsp;"fmt"&nbsp;是告诉编译器去包含文件在包fmt的预处理命令。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">下一行&nbsp;func&nbsp;main()主要功能是为程序执行的开始。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">如果有注释，/*...*/会被编译器被忽略，它已被加入到程序添加注释。因此，这样的行称为程</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">序中的注释。注释也使用//类似于Java或C++注释。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">下一行&nbsp;fmt.Println（...）是提供另一种功能，使消息&#8220;Hello,&nbsp;World!&#8221;要显示在屏幕上。这</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">里fmt包已导出。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>三 Go 语言的基本语法</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>标识符</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">Go语言不允许标识符中的标点字符，如@，$和%。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">Go是一种区分大小写的编程语言</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">Manpower&nbsp;和&nbsp;manpower&nbsp;在Go中是两个不同的标识符</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">标识符开始以字母A到Z或a到z或下划线_后跟零个或多个字母，下划线和数字（0〜9）</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>关键词</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">下面的列表显示了Go的保留字。这些保留的字可以不被用作常量或变量，或任何其他的标识符</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">名称。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">break&nbsp;&nbsp;default&nbsp;&nbsp;func&nbsp;&nbsp;interface&nbsp;&nbsp;select</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">case&nbsp;&nbsp;defer&nbsp;&nbsp;go&nbsp;&nbsp;map&nbsp;&nbsp;struct</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">chan&nbsp;&nbsp;else&nbsp;&nbsp;goto&nbsp;&nbsp;package&nbsp;&nbsp;switch</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">const&nbsp;&nbsp;fallthrough&nbsp;&nbsp;if&nbsp;&nbsp;range&nbsp;&nbsp;type</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">continue&nbsp;&nbsp;for&nbsp;&nbsp;import&nbsp;&nbsp;return&nbsp;&nbsp;var</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>在 Go 中空白格</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">仅包含空格，可能与注释行，被称为一个空行，Go编译器完全忽略它。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">var&nbsp;age&nbsp;int;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">必须有至少一个空白字符（通常是一个空格）int和age之间的编译器，以便能够区分它们</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">fruit&nbsp;=&nbsp;apples&nbsp;+&nbsp;oranges;&nbsp;&nbsp;&nbsp;//&nbsp;get&nbsp;the&nbsp;total&nbsp;fruit</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">fruit和=之间或=和apples，虽然是自由的，如果想便于阅读的目的，最好包括一些空格</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>四 Go 语言数据类型</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">在Go编程语言，数据类型指用于声明变量或不同类型的功能的广泛的系统。变量的类型决定了</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">有多少空间占用的存储和存储方式的位模式将被解释。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">Go的数据类型可分类如下：</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">Boolean&nbsp;Types</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">它们是布尔类型，它由两个预定义的常量：(a)&nbsp;true&nbsp;(b)&nbsp;false</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">Numeric&nbsp;Types</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">它们是算术类型，表示整数类型或b。在整个程序浮点值</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">string&nbsp;types:</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">一个字符串类型代表组字符串值。它的值是一个字节序列。字符串是一次创造了稳定的类型，</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">这是不可能改变一个字符串的内容。预声明的字符串类型为字符串。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">Derived&nbsp;types:</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">它们包括：(a)指针类型，(b)数组类型，(c)结构类型，(d)联盟类型及(e)函数类型(f)切片类(g)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">函数类型(h)接口类型(i)地图类型(j)管道类型。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">数组类型和结构类型被统称为聚合类型。函数的类型指定的一组具有相同的参数和结果类型所</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">有函数。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>整数</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">1 uint8</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">8位无符号整数&nbsp;(0&nbsp;-&nbsp;255)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">2 uint16</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">16位无符号整数&nbsp;(0&nbsp;-&nbsp;65535)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">3 uint32</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">32位无符号整数&nbsp;(0&nbsp;-&nbsp;4294967295)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">4&nbsp;&nbsp;uint64</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">64位无符号整数&nbsp;(0&nbsp;-&nbsp;18446744073709551615)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">5&nbsp;&nbsp;int8</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">有符号8位整数&nbsp;(-128&nbsp;-&nbsp;127)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">6&nbsp;&nbsp;int16</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">有符号16位整数&nbsp;(-32768&nbsp;-&nbsp;32767)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">7&nbsp;&nbsp;int32</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">有符号的32位整数&nbsp;(-2147483648&nbsp;-&nbsp;2147483647)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">8&nbsp;&nbsp;int64</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">有符号的64位整数&nbsp;(-9223372036854775808&nbsp;-&nbsp;9223372036854775807)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>浮点类型</strong><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">1&nbsp;&nbsp;float32</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">IEEE-754&nbsp;32-bit&nbsp;浮点数</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">2&nbsp;&nbsp;float64</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">IEEE-754&nbsp;64-bit&nbsp;浮点数</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">3&nbsp;&nbsp;complex64</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">复数与float32实部和虚部</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">4&nbsp;&nbsp;complex128</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">复数与float64实部和虚部</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>其他数值类型</strong><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">1&nbsp;&nbsp;byte</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">相同于&nbsp;uint8</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">2&nbsp;&nbsp;rune</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">相同于&nbsp;int32</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">3&nbsp;&nbsp;uint</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">32&nbsp;或&nbsp;64&nbsp;位</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">4&nbsp;&nbsp;int</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">相同于&nbsp;uint&nbsp;的大小</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">5&nbsp;&nbsp;uintptr</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">一个无符号整数来存储指针值的解释的比特位</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>五 Go 语言变量</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">变量是什么，只不是给定到存储区域，我们的程序可以操纵的名称。在Go中每个变量具有特定</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">的类型，它确定的大小和可变的存储器的布局;能确定存储器内存储的值的范围;和组操作可以施</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">加到变量。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">一个变量名可以由字母，数字和下划线。它必须以字母或下划线。大写和小写字母是不同的，因</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">为Go是区分大小写的。基于该基本类型在前面的章节中说明的那样，将有以下基本变量类型：</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">byte&nbsp;&nbsp;通常单个字节(一个字节)，这是一个字节的类型</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">int&nbsp;&nbsp;整数最自然的尺寸的机器</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">float32&nbsp;&nbsp;单精度浮点值</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">Go编程语言也可以定义各种其他类型的变量，我们将在以后的章节列出，如：枚举，指针，数</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">组，结构，联合，等等。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>Go中变量定义</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">var&nbsp;variable_list&nbsp;optional_data_type;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">optional_data_type可以包括字节，整型，float32，complex64，布尔或任何用户定义的对象</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">等有效Go的数据类型，variable_list可以由用逗号分隔的一个或多个标识符名称。一些有效的声</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">明如下所示：</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">var&nbsp;&nbsp;&nbsp;&nbsp;i,&nbsp;j,&nbsp;k&nbsp;int;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">var&nbsp;&nbsp;&nbsp;c,&nbsp;ch&nbsp;byte;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">var&nbsp;&nbsp;f,&nbsp;salary&nbsp;float32;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">d&nbsp;=&nbsp;42;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">var&nbsp;i,&nbsp;j,&nbsp;k;&nbsp;既声明并定义了变量i，j和k;这指示编译器创建一个名为i，j和k的&nbsp;int类型变量。变量</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">可以再声明时候初始化。变量的类型是<span style="font-family: 微软雅黑, sans-serif;">由编译器自动根据传递给它的值判断。</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">variable_name&nbsp;=&nbsp;value;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">d&nbsp;=&nbsp;3,&nbsp;f&nbsp;=&nbsp;5;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;declaration&nbsp;of&nbsp;d&nbsp;and&nbsp;f.&nbsp;Here&nbsp;d&nbsp;and&nbsp;f&nbsp;are&nbsp;int&nbsp;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">对于没有初始化定义：具有静态存储时间变量的隐含零初始化(所有字节的值为0);所有其它变量</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">的初始值是它们的数据类型的零值。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>静态类型声明</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">package&nbsp;main</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">import&nbsp;"fmt"</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">func&nbsp;main()&nbsp;{</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;&nbsp;var&nbsp;x&nbsp;float64</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;&nbsp;x&nbsp;=&nbsp;20.0</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;&nbsp;fmt.Println(x)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;&nbsp;fmt.Printf("x&nbsp;is&nbsp;of&nbsp;type&nbsp;%T ",&nbsp;x)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">}</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">编译运行结果是：<br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">20</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">x&nbsp;is&nbsp;of&nbsp;type&nbsp;float64</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>动态类型声明/类型推断</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">示例</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">试试下面的例子，其中的变量已经声明没有任何类型的，并已确定在主函数中初始化。如果类型推断的，我们已经初始化的变量y使用:=运算符，x初始化使用=运算符。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">package&nbsp;main</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">import&nbsp;"fmt"</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">func&nbsp;main()&nbsp;{</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;&nbsp;var&nbsp;x&nbsp;float64&nbsp;=&nbsp;20.0</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;&nbsp;y&nbsp;:=&nbsp;42&nbsp;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;&nbsp;fmt.Println(x)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;&nbsp;fmt.Println(y)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;&nbsp;fmt.Printf("x&nbsp;is&nbsp;of&nbsp;type&nbsp;%T ",&nbsp;x)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;&nbsp;fmt.Printf("y&nbsp;is&nbsp;of&nbsp;type&nbsp;%T ",&nbsp;y)&nbsp;&nbsp;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">}</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">编译和运行上面的程序，这将产生以下结果：</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">20</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">42</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">x&nbsp;is&nbsp;of&nbsp;type&nbsp;float64</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">y&nbsp;is&nbsp;of&nbsp;type&nbsp;int</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>混合变量声明</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">不同类型的变量可以一步到位使用类型推断声明。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">package&nbsp;main</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">import&nbsp;"fmt"</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">func&nbsp;main()&nbsp;{</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;&nbsp;var&nbsp;a,&nbsp;b,&nbsp;c&nbsp;=&nbsp;3,&nbsp;4,&nbsp;"foo" &nbsp;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;&nbsp;fmt.Println(a)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;&nbsp;fmt.Println(b)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;&nbsp;fmt.Println(c)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;&nbsp;fmt.Printf("a&nbsp;is&nbsp;of&nbsp;type&nbsp;%T ",&nbsp;a)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;&nbsp;fmt.Printf("b&nbsp;is&nbsp;of&nbsp;type&nbsp;%T ",&nbsp;b)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;&nbsp;&nbsp;fmt.Printf("c&nbsp;is&nbsp;of&nbsp;type&nbsp;%T ",&nbsp;c)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">}</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">编译和运行上面的程序，这将产生以下结果：</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">3</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">4</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">foo</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">a&nbsp;is&nbsp;of&nbsp;type&nbsp;int</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">b&nbsp;is&nbsp;of&nbsp;type&nbsp;int</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">c&nbsp;is&nbsp;of&nbsp;type&nbsp;string</p><div></div><img src ="http://www.cppblog.com/zhiyewang/aggbug/213135.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhiyewang/" target="_blank">zhiye_wang</a> 2016-03-28 15:33 <a href="http://www.cppblog.com/zhiyewang/archive/2016/03/28/213135.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Rancher 快速上手指南操作(1)</title><link>http://www.cppblog.com/zhiyewang/archive/2016/03/17/213053.html</link><dc:creator>zhiye_wang</dc:creator><author>zhiye_wang</author><pubDate>Thu, 17 Mar 2016 03:03:00 GMT</pubDate><guid>http://www.cppblog.com/zhiyewang/archive/2016/03/17/213053.html</guid><wfw:comment>http://www.cppblog.com/zhiyewang/comments/213053.html</wfw:comment><comments>http://www.cppblog.com/zhiyewang/archive/2016/03/17/213053.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhiyewang/comments/commentRss/213053.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhiyewang/services/trackbacks/213053.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Rancher 快速上手指南操作(1)该指南知道用户如何快速的部署Rancher Server 管理容器。前提是假设你的机器已经安装好docker了。1 确认 docker 的版本，下面是 ubuntu 的输出[#63#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker version[sudo] password&nbsp;for&nbsp;cl...&nbsp;&nbsp;<a href='http://www.cppblog.com/zhiyewang/archive/2016/03/17/213053.html'>阅读全文</a><img src ="http://www.cppblog.com/zhiyewang/aggbug/213053.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhiyewang/" target="_blank">zhiye_wang</a> 2016-03-17 11:03 <a href="http://www.cppblog.com/zhiyewang/archive/2016/03/17/213053.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>第四章 使用Docker镜像和仓库(二)</title><link>http://www.cppblog.com/zhiyewang/archive/2016/03/17/213052.html</link><dc:creator>zhiye_wang</dc:creator><author>zhiye_wang</author><pubDate>Thu, 17 Mar 2016 03:01:00 GMT</pubDate><guid>http://www.cppblog.com/zhiyewang/archive/2016/03/17/213052.html</guid><wfw:comment>http://www.cppblog.com/zhiyewang/comments/213052.html</wfw:comment><comments>http://www.cppblog.com/zhiyewang/archive/2016/03/17/213052.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhiyewang/comments/commentRss/213052.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhiyewang/services/trackbacks/213052.html</trackback:ping><description><![CDATA[<p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><strong>第四章&nbsp;使用Docker镜像和仓库(二)</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><strong>回顾：</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">开始学习之前，我先pull下来ubuntu和fedora镜像</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">[#9#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker pull fedora</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Using default tag: latest</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">latest: Pulling from library/fedora</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">9bdb5101e5fc: Pull complete&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Digest: sha256:1fa98be10c550ffabde65246ed2df16be28dc896d6e370dab56b98460bd27823</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Status: Downloaded newer image for fedora:latest</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">[#10#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker pull ubuntu</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Using default tag: latest</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">latest: Pulling from library/ubuntu</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">ebe73f29e6e1: Pull complete&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">4976a0f2dc03: Pull complete&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">5c117067c385: Pull complete&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">001d664e2dd4: Pull complete&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Digest: sha256:7eb6ad74ec4fbe56ac194d8760063c88ca362f05a9038f2bc4f09a51849a4a53</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Status: Downloaded newer image for ubuntu:latest</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><img src="http://images2015.cnblogs.com/blog/622771/201603/622771-20160316114445568-136725133.png" alt="" style="border: 0px; max-width: 900px;" /></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><strong>4.5.6 Dockerfile 和构建缓存</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">想略过缓存功能，可以使用 docker build 的 --no-cache 标志</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">sudo docker build --no-cache -t="zhiyewang/static_web" .</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><strong>4.5.7 基于构建缓存的 Dockerfile 模板</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">FROM ubuntu:14.04</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">MAINTAINER zhiyewang "zhiye_wang@yeah.net"</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">ENV REFRESHED_AT 2016-03-16</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">RUN apt-get -qq update</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">这里要想重新构建 Dockerfile ，只需要将第三行的日期修改以下即可。将会更新 APT 包的缓</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">存。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><strong>4.5.8 查看新镜像</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><img src="http://images2015.cnblogs.com/blog/622771/201603/622771-20160316114454162-550959982.png" alt="" style="border: 0px; max-width: 900px;" /></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">查看镜像如何构建出来，可以使用 docker history 命令。可以看到新构建的 zhiyewang/stat</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp;ic_web 镜像的每一层。以及创建这些层的 Dcoekrfile 命令。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><img src="http://images2015.cnblogs.com/blog/622771/201603/622771-20160316114504240-1441564998.png" alt="" style="border: 0px; max-width: 900px;" /></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">4.5.9 从构建的新镜像启动容器</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">上一节成功使用 Dockerfile 命令构建出 zhiyewang/static_web 这个镜像。现在我们来试试</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">看镜像是否工作正常。z</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">基于新构建的镜像启动一个新容器。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">[#17#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker run -d -p 80 --name stati</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">c_web zhiyewang/static_web nginx -g "daemon off;"</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">d4d9024c688d267761dee792e0b0686a6b2d06dcf53e656c98d95408f4894974</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">这条命令基于方才构建的镜像名字，启动了一个名为 static_web 的新容器。 同时指定了 -d 选</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">项，告诉 Docker 以分离的方式在后台运行。同时也指定了在新容器中运行的命令: nginx -g "</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">daemon off;"。这将以前台的方式启动 Nginx。新标志 -p 用来控制 Docker 在运行时应该公</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">开哪些网络端口给外部（宿主机）。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">[#19#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker ps -l</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">CONTAINER ID &nbsp; &nbsp; &nbsp; &nbsp;IMAGE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;COMMAND &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PORTS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">d4d9024c688d &nbsp; &nbsp; &nbsp; &nbsp;zhiyewang/static_web &nbsp; "nginx -g 'daemon off" &nbsp; &nbsp; &nbsp; &nbsp; 0.0.0.0:32768-&gt;80/tcp&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">可以看到容器中的 80 端口被映射到宿主机的 32768 端口。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">也可以使用 docker port 查看端口的情况</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">[#20#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker port d4d9024c688d</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">80/tcp -&gt; 0.0.0.0:32768</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">这样的端口映射方式为随机的，我们也可以指定特定的端口映射</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">将容器的 80 端口绑定到本地宿主机的 80 端口</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">sudo docker run -d -p 80:80 --name static_web zhiyewang/static_web nginx -g "daemon off;"</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">将容器的 80 端口绑定到本地宿主机的 8080 端口</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">sudo docker run -d -p 8080:80 --name static_web zhiyewang/static_web nginx -g "daemon off;"</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">将容器的 80 端口绑定到本地宿主机的 127.0.0.1 这个 IP 的 80 端口</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">sudo docker run -d -p 127.0.0.1:80:80 --name static_web zhiyewang/static_web nginx -g "daemon off;"</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">将容器的 80 端口绑定到本地宿主机的 127.0.0.1 这个 IP 的随机端口</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">sudo docker run -d -p 127.0.0.1::80 --name static_web zhiyewang/static_web nginx -g "daemon off;"</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">对外公开端口，此命令可以将容器内的 80 端口对本地宿主机公开，并且绑定要宿主机的一个</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">随机端口。此命令同时也会将 Dockerfile 文件中 EXPOSE 指令指定的其他端口一并公开。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">sudo docker run -d -p --name static_web zhiyewang/static_web nginx -g "daemon off;"</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">这样我们就可以使用本地宿主机的 IP 地址或者 127.0.0.1 的 localhost 来连接到运行的容器，</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">查看 Web 服务器的内容了。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">[#33#cloudsoar@cloudsoar-virtual-machine ~]$curl localhost:32768</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Hi, I am in your container</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><strong>4.5.10 Dockerfile 指令</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><strong>1 CMD 指令</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">CMD 指令用于指定一个容器启动时候需要运行的指令。有点类似于 RUN 指令，区别是 RUN&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">指令是指定镜像被构建时候运行的指令，而 CMD 是容器被启动时运行的指令。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">命令行启动容器的 /bin/true</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">[#34#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker run -i -t zhiyewang/static_web /bin/true</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">可以使用 CMD 写在 Dockerfile 中：</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">CMD ["/bin/true"]</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">也可以为要运行的命令指定参数</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">CMD ["/bin/bash", "-l"]</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">需要注意的是 docker run 命令可以覆盖 CMD 指令。如果 dockerrun 中指定了命令，而CM</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp;D 中也指定了相同的命令，命令行中的指令会覆盖 Dockerfile 中的 CMD 指令。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">假设我们的 Dockerfile 中有如下命令</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">CMD [ "/bin/bash" ]</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">使用 docker build 命令构建一个新镜像，假设为 zhiyewang/test，并基于此镜像启动一个新</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">容器。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">[#35#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker run -t -i zhiyewang/test</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">root@7ec0a03d41fc:/#&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">可以看到 docker run 命令的末尾并没有指定 /bin/bash 指令，却进入了容器的 bash。实际上</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Docker 使用了 CMD 中指定的命令。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">但是 Dockerfile 中只能指定一条 CMD 指令。如果制定多个，只有最后一个会被调用。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><strong>2 ENTRYPOINT</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">ENTRYPONIT 指令提供的命令不会再容器启动时被命令行覆盖。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">ENTRYPONIT ["/usr/sbin/nginx"]</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">为 ENTRYPOINT 指定参数</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">ENTRYPONIT ["/usr/sbin/nginx", "-g", "daemon off;"]</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">如果需要 也可以在运行时通过 docker run 的 --entrypoint 标志覆盖 ENTRYPOINT 指令。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><strong>3 WORKDIR</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">WORKDIR /opt/webapp/db</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">RUN bundle install</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">WORKDIR /opt/webapp</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">ENTRYPOINT [ "rackup" ]</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">把目录切换到 /opt/webapp/db 指定了 bundle install 命令，然后又将工作目录切换为 /opt</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp;/webapp 最后这只了 ENTRYPOINT 指令来启动 rackup 命令。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><strong>4 ENV</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">ENV 可以用来在镜像构建过程中设置环境变量</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">ENV RMV_PATH /home/rvm/</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">这个环境变量设置后在后续的任何 RUN 中都可以使用</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">也可以在其他指令中直接使用这些环境变量&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">RVM_PATH=/home/rvm/ gem install unicorn</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">ENV 创建的环境变量也会被持久保存到从我们的镜像创建的任何容器中。比如在容器中运行</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">env 查看：</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">root@7ec0a03d41fc:/# env</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">...&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">RVM_PATH=/home/rvm/</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">运行时环境变量</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">sudo docker run -ti -e &nbsp;"WEB_PORT=8080" ubuntu env</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">可以讲容器的 WEB_PORT 环境变量设置为 8080</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><strong>5 USER</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">user 指令用来指定该镜像会以什么样的用户去执行</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">USER nginx</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">我们可以指定用户名或者 UID 以及组或者 GID，甚至是两者的组合。也可以在 docker run 命</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">令中通过 -u 选项来覆盖该指令的值。如果不通过 USER 指定特定用户，默认是 root 。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><strong>6 VOLUME</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">VOLUME ["/opt/project"]</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">这条指令将会为基于此镜像创建的任何容器创建一个名为 /opt/projiect 的挂载点。也可以通过</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">数组的方式指定多个卷。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">VOLUME ["/opt/project", "/data"]</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><strong>7 ADD</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">ADD 命令用来将构建环境下的文件和目录复制到镜像中。也可以指定URL。Docker 通过目的地</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">址的参数末尾的字符来判断文件源是目录还是文件。如果目的地址以 / 结尾，Docker 认为是一个</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">目录，如果不是的话，认为是文件。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">ADD&nbsp;<a href="http://wordpress.org/latest.zip" style="color: #3f3d3d; text-decoration: none;">http://wordpress.org/latest.zip</a>&nbsp;/root/wordpress.zip</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">ADD 在处理本地的归档文件（包括 gzip，bzip2，xv）指定为源文件时候，会自动将归档解压。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">ADD latest.tar.gz /var/www/wordpress/</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><strong>8 COPY</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">COPY 和 ADD 的本质区别是 COPY 只关心在构建上下文中复制本地文件，而不会去做文件提</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">取和解压的工作。COPY 的文件源路径必须是一个与当前构建环境相对的文件或目录，本地文</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">件都放到和 Dockerfile 同一个目录下。不能复制该目录之外的任何文件。目的为止必须是容器</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">内部的一个绝对路径。该指令创建的文件或者目录的 UID 和 GID 都会被设置为 0 。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">COPY conf.d/ /etc/apache2/</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><strong>9 ONBUILD</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><strong>4.6 将镜像退送到 Docker Hub</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">[#37#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker push zhiyewang/static_web</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">The push refers to a repository [docker.io/zhiyewang/static_web] (len: 1)</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">e97eb7ef0136: Pushed&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">6a7a53f6e78a: Pushed&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">ddc8935b098a: Pushed&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">40fa5cd1c3d2: Pushed&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">c5aed3a8ff95: Pushed&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">0b427fcc4cbb: Pushed&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">9d89fd8f8a3e: Pushed&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">073de23ee32b: Pushed&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">latest: digest: sha256:152eb2d70e0f795fbe1b8f8c9eea09e7832a8b01e953cc051cd07832732da0ed size: 14731</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">现在可以在 Docker Hub 上看到我们的镜像了。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><img src="http://images2015.cnblogs.com/blog/622771/201603/622771-20160316114522474-1576018091.png" alt="" style="border: 0px; max-width: 900px;" /></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><strong>自动构建</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><strong>4.7 删除镜像</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">如果不需要一个镜像了 可以使用 docker rmi 来删除</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">[#41#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker rmi zhiyewang/static_web</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Error response from daemon: conflict: unable to remove repository reference "zhiyewang/static_web" (must force) - container 7ec0a03d41fc is using its referenced image e97eb7ef0136</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Error: failed to remove images: [zhiyewang/static_web]</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">可以看到这个镜像被一个容器&nbsp;7ec0a03d41fc&nbsp;使用着，首先删除掉容器即可。&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">[#53#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker rm 7ec0a03d41fc</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">7ec0a03d41fc</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">[#62#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker rmi e97eb7ef0136</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Deleted: e97eb7ef013619e503bd729596a06e46ee85786619d95950e54f5a74c6fc2694</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Deleted: 6a7a53f6e78a802ea932a5914e63d217acf4a47ddbedb80dab042d55297573a5</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Deleted: ddc8935b098a0b449a3335286b2b0e555b3d44bd5d92dea305c57f6f7c846fae</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Deleted: 40fa5cd1c3d2ae9c80763e9b787c5c9b4848a34164fc2138d2c160830505466d</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Deleted: c5aed3a8ff9508b42644cef59c2b44c249628c54130fa1a030f3f2b299124ecc</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">这里删除的是本地的镜像。每一个 Deleted: 行都代表一个镜像层被删除。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><strong>4.8 运行自己的 Docker Registry</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">这个可以自己试试了。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">到此为止，第四章学习完毕。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">下一篇学习在测试中使用 Docker，用Docker 测试一个静态网站，用 Docker 创建并测试一个</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">WEB 应用，用 Docker 用于持续集成</p><img src ="http://www.cppblog.com/zhiyewang/aggbug/213052.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhiyewang/" target="_blank">zhiye_wang</a> 2016-03-17 11:01 <a href="http://www.cppblog.com/zhiyewang/archive/2016/03/17/213052.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>第四章 使用Docker镜像和仓库</title><link>http://www.cppblog.com/zhiyewang/archive/2016/03/17/213051.html</link><dc:creator>zhiye_wang</dc:creator><author>zhiye_wang</author><pubDate>Thu, 17 Mar 2016 03:00:00 GMT</pubDate><guid>http://www.cppblog.com/zhiyewang/archive/2016/03/17/213051.html</guid><wfw:comment>http://www.cppblog.com/zhiyewang/comments/213051.html</wfw:comment><comments>http://www.cppblog.com/zhiyewang/archive/2016/03/17/213051.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhiyewang/comments/commentRss/213051.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhiyewang/services/trackbacks/213051.html</trackback:ping><description><![CDATA[<p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><strong>第4章 使用Docker镜像和仓库</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">回顾：</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">回顾如何使用 docker run 创建最基本的容器</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">$sudo docker run -i -t --name another_container_mum ubuntu /bin/bash</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">root@3d49f5830c81:/#&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">这条命令会启动一个新的名为 another_container_mum 的容器，这个容器基于ubuntu镜</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">像并且会启动Bash Shell</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">---------------------------------------------------------------------------------------------</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><strong>4.1 什么是 Docker 镜像</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><strong>4.2 列出镜像</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><img src="http://images2015.cnblogs.com/blog/622771/201603/622771-20160315184735428-1896463575.png" alt="" style="border: 0px; max-width: 900px;" /><br />可以看出我这里有三个镜像。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">其中 ubuntu 是我上午下载的 ubuntu 基础镜像，paulcos11/docker-tutorial 是下载的另外</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">一个用户上传的镜像。但是不知道 CREATED 这一栏的时间怎么不准。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">镜像从仓库下载下来。镜像保存在仓库中。而仓库存在于Registry中。默认的Registry是由Do</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">cker公司运营的公共 Registry 服务，即是 Dcoker Hub。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">每个镜像库都可以存放很多镜像，例如我们查看一下ubuntu仓库中的其他镜像，</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">可是使用 docker images 查看所有的 ubuntu docker 镜像</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">sudo docker images</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">可以使用docker pull下载某个基础镜像</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">[#12#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker pull ubuntu</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Docker Hun 中有两种类型仓库：用户仓库和顶层仓库。用户仓库的镜像都是有Docker用户</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">创建的，而顶层仓库则是由Docker内部的人来管理的。用户仓库的命名由用户名和仓库名两</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">部分组成，如：paulcos11/docker-tutorial，用户名：paulcos11，仓库名：docker-tutor</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">ial，与其相对的顶层仓库只包含仓库名部分，例如 ubuntu，fedora。顶层仓库由Docker公</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">司和由选定的能提供优质基础镜像的厂商管理。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><strong>4.3 拉取镜像</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">使用docker images可以查看本地Docker宿主机上面的镜像。如果希望能在镜像列表中只看</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">到某个镜像的内容，例如 fedora，可以通过在 docker images 命令后面跟指定的镜像名来实</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">现，例如：&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><img src="http://images2015.cnblogs.com/blog/622771/201603/622771-20160315184805256-1955200557.png" alt="" style="border: 0px; max-width: 900px;" /></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">使用 docker pull 拉取镜像</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">[#15#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker pull fedora</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Using default tag: latest</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">latest: Pulling from library/fedora</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">6888fc827a3f: Pull complete&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">9bdb5101e5fc: Downloading [===================&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ] 28.63 MB/74.33 MB</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><strong>4.4 查找镜像</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">我本地的镜镜像有：</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">[#1#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker images</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">[sudo] password for cloudsoar:&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">REPOSITORY &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TAG &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IMAGE ID &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CREATED &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VIRTUAL SIZE</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">paulcos11/docker-tutorial &nbsp; latest &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;e37931352714 &nbsp; &nbsp; &nbsp; &nbsp;8 days ago &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;587.8 MB</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">ubuntu &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;latest &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;8ed581e3fa7a &nbsp; &nbsp; &nbsp; &nbsp;11 days ago &nbsp; &nbsp; &nbsp; &nbsp; 188 MB</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">此时 fedora 镜像还是没有下载完毕的。不着急。我们先练习其他的操作，在&nbsp;paulcos11/do</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">cker-tutorial 镜像中使用 docker run 命令来从&nbsp;docker-tutorial&nbsp;创建一个容器。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">[#2#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker run -i -t paulcos11/docker-tutorial /bin/bash</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">root@869a3b2049ad:/#</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">可以看到，已经从&nbsp;paulcos11/docker-tutorial&nbsp;镜像启动了一个新的容器。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><strong>4.5 构建镜像</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">构建 Docker 镜像有以下两种方法：</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">使用 docker commit 命令。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">使用 docker build 命令和 Dockerfile 文件。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><strong>4.5.1 创建 Docker Hub 账号</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">在 hub.docker.com 创建一个自己的账号，注册之后通过收到的确认邮件激活，下面就可以测</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">试刚才注册的账号是否可以工作了。要登录到 docker hub，可以使用 docker login 命令。如</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><img src="http://images2015.cnblogs.com/blog/622771/201603/622771-20160315184831006-1715345304.png" alt="" style="border: 0px; max-width: 900px;" /></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">这里看到我是注册成功了，用户名只能是字母或者数组的组合。下面使用 docker login 来验证</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">我的账号：</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">[#4#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker login</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Username:&nbsp;zhiyewang</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Password:&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Email:&nbsp;zhiye_wang@yeah.net&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">WARNING: login credentials saved in /home/cloudsoar/.docker/config.json</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Login Succeeded</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">可以看到我的账号登录成功了。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><strong>4.5.2 使用 Docker 的 commit 命令创建镜像</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">这里我基于前面下载的 ubuntu 镜像来创建一个新镜像。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">首先我在这个基础镜像中启动一个容器</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">[#5#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker run -i -t ubuntu /bin/bash</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">root@460f5a1ac42a:/#&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">在容器中安装个 Apache 作为一个 web 服务器来运行。这样每次使用 Apache 的时候不用再</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">重新安装 Apache 了。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">root@460f5a1ac42a:/#&nbsp;apt-get -y install apache2</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Reading package lists... Done</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Building dependency tree &nbsp; &nbsp; &nbsp;&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">root@460f5a1ac42a:/#&nbsp;apt-get -y install vim</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">我喜欢用vim，同时又安装了一个 vim，哇咔咔。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">为了完成此项工作，需要先退出 exit ，然后执行 docker commit 命令。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">这里需要注意的是， docker commit 提交的是创建容器的镜像与容器的当前状态之间有差异</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">的部分，这使得该更新非常轻量。这里可以看到我创建的结果。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><img src="http://images2015.cnblogs.com/blog/622771/201603/622771-20160315184856771-1572993164.png" alt="" style="border: 0px; max-width: 900px;" /><br />如果像从刚才创建的镜像运行一个容器，可以使用 docker run 命令</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">[#16#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker run -t -i zhiyewang/apache2 /bin/bash</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">root@bb634a313bf2:/#&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><strong>4.5.3 使用 Dockerfile 构建镜像</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">事实上所有资料都不推介使用 docker commit 的方法构建镜像，而是使用 Dockerfile 的定义</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">文件和 docker build 命令来构建镜像。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><strong>我们的第一个 Dockerfile</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">现在我们创建一个目录，并在里面创建初始的 Dockerfile，我们将创建一个包含简单 Web 服</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">服务器的 Docker 镜像。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><img src="http://images2015.cnblogs.com/blog/622771/201603/622771-20160315184920428-1804659877.png" alt="" style="border: 0px; max-width: 900px;" /><br />这里我们创建了一个名为 static_web 的目录用来保存 Dockerfile，这个目录就是我们的构建环</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">境（build environment），Docker 则称此环境为上下文（context）或者构建上下文（build</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp;context）。Docker 会在构建镜像时候，将构建上下文和该上下文中的文件和目录上传到 doc</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp;ker 守护进程。这样 Docker 守护进程就能直接访问你想在镜像中存储的任何代码。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">下面是一个 Dockerfile 的例子，用 Dockerfile 构建一个能作为 Web 服务器的 Docker 镜像。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp; 1 # version: 0.0.1</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp; 2 FROM ubuntu:14.04</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp; 3 MAINTAINER zhiyewang "zhiye_wang@yeah.net"</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp; 4 RUN apt-get update</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp; 5 RUN apt-get install -y nginx</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp; 6 RUN echo 'Hi, I am in your container' \</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp; 7 &nbsp; &nbsp; &gt;/usr/share/nginx/html/index.html</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp; 8 EXPOSE 80 &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">命令解释：</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Dockerfile 由一系列指令和参数组成。每条指令都是大写，而且后面需要跟一个参数。Docker</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp;file 会按照顺序从上往下执行。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Dockerfile 支持注释，所有以井号开头的都是注释。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">FROM ubuntu:14.04 指定了 ubuntu 14.04 作为基础镜像，每</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">执行一条指令，对容器做出修改。自动会再指定类似 docker commit 的操作，提交一个新镜像</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">层，继续执行下一条指令。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">MAINTAINER 指令会告诉 Docker 该镜像的作者是谁，以及作者的电子邮件地址。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">接下来我们执行了三条 RUN 指令，RUN 指令会在当前镜像中运行指定的命令。我们通过RUN明</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">令更新了 APT 仓库，安装了 nginx 包，之后创建了 &nbsp;/usr/share/nginx/html/index.html 文件，</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">默认情况，RUN执行会在 shell 里使用 /bin/sh -c 来执行。如果再不支持 shell 或者不想再 shell</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">中运行，可以使用 exec 格式的 RUN</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">RUN [ "apt-get", "install", "-y", "nginx" ]</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">接下来的 EXPOSE 指令，告诉 Docker 该容器内的应用程序将会使用 Docker 的指定端口。但是</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Docker 并不会自动打开此端口，而是需要再使用 docker run 运行容器时候指定需要打开那些端</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">口。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><strong>4.5.4 基于 Dockerfile 构建新镜像</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">运行 Dockerfile&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">[#34#cloudsoar@cloudsoar-virtual-machine ~]$cd static_web/</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">[#35#cloudsoar@cloudsoar-virtual-machine ~/static_web]$sudo docker build -t="zhiyewang/static_web" .</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Sending build context to Docker daemon 2.048 kB</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Step 1 : FROM ubuntu:14.04</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">14.04: Pulling from library/ubuntu</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Digest: sha256:45b23dee08af5e43a7fea6c4cf9c25ccf269ee113168c19722f87876677c5cb2</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Status: Downloaded newer image for ubuntu:14.04</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp;---&gt; 8ed581e3fa7a</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Step 2 : MAINTAINER zhiyewang "zhiye_wang@yeah.net"</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp;---&gt; Running in 7806118624b7</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp;---&gt; c5aed3a8ff95</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Removing intermediate container 7806118624b7</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">...</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Processing triggers for sgml-base (1.26+nmu4ubuntu1) ...</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp;---&gt; ddc8935b098a</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Removing intermediate container c81405d28e41</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Step 5 : RUN echo 'Hi, I am in your container' &nbsp;&gt;/usr/share/nginx/html/index.html</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp;---&gt; Running in f0049e284208</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp;---&gt; 6a7a53f6e78a</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Removing intermediate container f0049e284208</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Step 6 : EXPOSE 80</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp;---&gt; Running in 2a0714253002</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp;---&gt; e97eb7ef0136</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Removing intermediate container 2a0714253002</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Successfully built e97eb7ef0136</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">这里使用 -t 参数为新镜像设置了仓库和名称。仓库为 zhiyewang，名称为 static_web，也可</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">以构建过程中为镜像添加一个标签，方法为&#8220;镜像名：标签&#8221;<br />sudo docker build -t="zhiyewang/static_web:v1" .</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">如果没有定制任何标签，Docker 会自动为镜像设置一个 latest 标签。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">上面命令最后的 . 告诉我们去当前路径去找 Dockerfile 文件。也可以指定一个 Git 仓库的源地</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">址来指定 Dockerfile 的位置。例如</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">sudo docker build -t="zhiyewang/static_web:v1" \</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp;git@github.com:zhiyewang/docker-static_web</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">这里假设的在 Git 仓库的目录下存在 Dockerfile 文件。我也没有注册 Git 账号去执行过。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">查看 docker build 过程发现，构建上下文已经上传到了 Docker 守护进程：</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Sending build context to Docker daemon 2.048 kB</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">之后，可以看到 Dockerfile 中的每条明令都被顺序执行，而且构建过程的最终结果返回了新的</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">镜像的 ID，即&nbsp;e97eb7ef0136&nbsp;，并且 Docker 会提交每一步的执行结果。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"><strong>4.5.4 指令失败时候会怎样</strong></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">例如我们将上面的第 4 步的包名&nbsp;nginx&nbsp;写成 ngin</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">[#41#cloudsoar@cloudsoar-virtual-machine ~/static_web]$sudo docker build -t="zhiyewang/static_web" .</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">[sudo] password for cloudsoar:&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Sending build context to Docker daemon 2.048 kB</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Step 1 : FROM ubuntu:14.04</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp;---&gt; 8ed581e3fa7a</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Step 2 : MAINTAINER zhiyewang "zhiye_wang@yeah.net"</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp;---&gt; Using cache</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp;---&gt; c5aed3a8ff95</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Step 3 : RUN apt-get update</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp;---&gt; Using cache</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp;---&gt; 40fa5cd1c3d2</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Step 4 : RUN apt-get install -y ngin</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">&nbsp;---&gt; Running in 86e3dbaadf20</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Reading package lists...</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Building dependency tree...</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Reading state information...</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">E: Unable to locate package ngin</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">The command '/bin/sh -c apt-get install -y ngin' returned a non-zero code: 100</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">发现会出错。我们来调试一下失败原因。用 docker run 明令来基于这次构建到目前为止已经</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">成功的最后一步创建一个容器，它的 ID 是&nbsp;40fa5cd1c3d2 ，如下代码：</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">[#42#cloudsoar@cloudsoar-virtual-machine ~/static_web]$sudo docker run -t -i 40fa5cd1c3d2 /bin/bash</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">root@b978996f25f3:/#</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">这时我们在此容器中运行第 4 步：</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">root@b978996f25f3:/#&nbsp;apt-get install -y ngin</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Reading package lists... Done</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Building dependency tree &nbsp; &nbsp; &nbsp;&nbsp;</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Reading state information... Done</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">E: Unable to locate package ngin</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">发现包名错误。</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">我们可以在这个容器中再次运行&nbsp;apt-get install -y nginx，这次输入正确的包名，来定位问题</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">，如果一旦解决了这个问题，就可以退出容器，用正确的包名修改 Dockerfile 文件，之后再次</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">构建即可。<br /><br /></p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">这一篇到这里。下一篇继续学习Dockerfile 和构建缓存。</p><img src ="http://www.cppblog.com/zhiyewang/aggbug/213051.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhiyewang/" target="_blank">zhiye_wang</a> 2016-03-17 11:00 <a href="http://www.cppblog.com/zhiyewang/archive/2016/03/17/213051.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>第三章 Docker 入门</title><link>http://www.cppblog.com/zhiyewang/archive/2016/03/15/213023.html</link><dc:creator>zhiye_wang</dc:creator><author>zhiye_wang</author><pubDate>Tue, 15 Mar 2016 05:43:00 GMT</pubDate><guid>http://www.cppblog.com/zhiyewang/archive/2016/03/15/213023.html</guid><wfw:comment>http://www.cppblog.com/zhiyewang/comments/213023.html</wfw:comment><comments>http://www.cppblog.com/zhiyewang/archive/2016/03/15/213023.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhiyewang/comments/commentRss/213023.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhiyewang/services/trackbacks/213023.html</trackback:ping><description><![CDATA[<p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong><span style="font-family: 微软雅黑, 'Microsoft YaHei';">第三章 docker 入门</span></strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong><span style="font-family: 微软雅黑, 'Microsoft YaHei';">3.1 确保docker已经就绪</span></strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 微软雅黑, 'Microsoft YaHei';">首先查看docker程序是否存在，功能是否正常</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 微软雅黑, 'Microsoft YaHei'; color: #ff0000;"><span style="color: #000000;">[#3#cloudsoar@cloudsoar-virtual-machine ~]$</span>sudo docker info</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 微软雅黑, 'Microsoft YaHei';">[sudo] password for cloudsoar:</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 微软雅黑, 'Microsoft YaHei';">Containers: 11</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 微软雅黑, 'Microsoft YaHei';">Images: 16</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 微软雅黑, 'Microsoft YaHei';">Server Version: 1.9.1</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 微软雅黑, 'Microsoft YaHei';">Storage Driver: aufs</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 微软雅黑, 'Microsoft YaHei';">&nbsp;Root Dir: /var/lib/docker/aufs</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 微软雅黑, 'Microsoft YaHei';">&nbsp;Backing Filesystem: extfs</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 微软雅黑, 'Microsoft YaHei';">&nbsp;Dirs: 38</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 微软雅黑, 'Microsoft YaHei';"><br /></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff; font-family: 微软雅黑, 'Microsoft YaHei';">3.2 运行我们的第一个容器</span></span></strong><span style="background-color: #d8d8d8;"><br /></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff; font-family: 微软雅黑, 'Microsoft YaHei';">docker run 命令提供了Docker容器的创建到启动的功能</span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 微软雅黑, 'Microsoft YaHei';">书上使用此命令创建第一个容器</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 微软雅黑, 'Microsoft YaHei';"><span style="color: #ff0000;"><span style="color: #000000;">[#4#cloudsoar@cloudsoar-virtual-machine ~]$</span>sudo docker run -i -t ubuntu /bin/bash&nbsp;</span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;Unable to find image 'ubuntu:latest' locally</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">latest: Pulling from library/ubuntu</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">073de23ee32b: Downloading 48.09 MB/65.69 MB</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 微软雅黑, 'Microsoft YaHei';">...</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 微软雅黑, 'Microsoft YaHei';">可以看到有大概66MB，等待慢慢下载完成。其实Ubuntu镜像也是一个基础镜像。</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 微软雅黑, 'Microsoft YaHei';"><span style="color: #ff0000;">sudo docker run -i -t ubuntu /bin/bash&nbsp;</span></span><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 微软雅黑, 'Microsoft YaHei';">这句命令我的理解就是，sudo 临时切换管理员权限，使用 docker run 命令，启动镜像</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 微软雅黑, 'Microsoft YaHei';">ubuntu 中的 bash。</span><span style="font-family: 微软雅黑, 'Microsoft YaHei';">docker run 会检查本地是否有ubuntu这个基础镜像，如果没有，</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 微软雅黑, 'Microsoft YaHei';">则去docker hub registry&nbsp;去检测并下载此镜像，下载完毕再执行。</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 微软雅黑, 'Microsoft YaHei';">由于容器有自己的ip，自己的shell，是独立的。因此加上 -i 参数来打开容器的标准输入</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 微软雅黑, 'Microsoft YaHei';">STDIN，使用 -t 给容器配一个交互式终端。当新容器下载完毕后，会自动以root用户登</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 微软雅黑, 'Microsoft YaHei';">录到新的容器中。界面显示的是：root@容器ID 。容器的主机名就是容器的ID。</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">假设说我们想要给这个容器安装一个curl，或者是 ping，可以通过如下命令</span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">sudo docker run ubuntu apt-get install -y curl</span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">docker run 容器名 命令&nbsp;</span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;"><br /></span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">等了半个小时，还没下载完毕，由于这个基础镜像下载实在太慢，我换了一个镜像</span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 微软雅黑, 'Microsoft YaHei';">我之前pull了一个名为paulcos11/docker-tutorial的镜像，容器有11个</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">如果你想下载其他的镜像，可以使用&nbsp;docker search tutorial&nbsp;首先查看可用镜像。然后使用</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">docker pull 把它拉下来。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: #ff0000;"><span style="color: #000000;">root@cloudsoar-virtual-machine:/home/cloudsoar#</span>&nbsp;docker search tutorial</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">NAME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DESCRIPTION &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; STARS &nbsp; &nbsp; OFFICIAL &nbsp; AUTOMATED</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">georgeyord/reactjs-tutorial &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;This is the backend of the React comment b... &nbsp; 2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[OK]</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">mhausenblas/kairosdb-tutorial &nbsp; &nbsp; &nbsp; &nbsp;GitHub fetcher for KairosDB tutorial &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[OK]</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">odk211/spree-tutorial &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[OK]</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: #ff0000;">paulcos11/docker-tutorial &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;docker tutorial &nbsp; &nbsp;&nbsp;</span>&nbsp;&nbsp;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: #ff0000;"><span style="color: #000000;">root@cloudsoar-virtual-machine:/home/cloudsoar#</span>&nbsp;docker pull&nbsp;paulcos11/docker-tutorial</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">Using default tag: latest</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">latest: Pulling from paulcos11/docker-tutorial</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">044ffdf80f70: Pull complete&nbsp;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">...</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">Digest: sha256:8effcf1f4eac7096ba4eaf4a90261580657605d159946372c12ae28b7e5e74f1</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">Status: Downloaded newer image for paulcos11/docker-tutorial:latest</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">到这里，我成功下载了容器镜像<span style="color: #ff0000;">paulcos11/docker-tutorial</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: #ff0000;"><br /></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: #ff0000;"><span style="color: #000000;">继续流程，进入我们的容器</span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: #ff0000;"><span style="color: #000000;">root@cloudsoar-virtual-machine:~#</span>&nbsp;docker run -i -t paulcos11/docker-tutorial /bin/bash</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: #ff0000;">root@537739299f24:/#&nbsp;</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">执行完毕可以看到进入了容器的bash，用户是root，容器ID是<span style="color: #ff0000;">537739299f24，</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;"><br /></span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">3.3 使用容器</span></span></strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">可以使用 hostname 查看容器的主机名<br /></span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">root@537739299f24:/#&nbsp;hostname</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">537739299f24</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">可以看到容器的ID就是容器的主机名。<br /></span></span>下一步看看 /etc/hosts 文件</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">root@537739299f24:/#&nbsp;<span style="color: #ff0000;">cat /etc/hosts</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">172.17.0.2<span style="white-space: pre;">	</span>537739299f24</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">127.0.0.1<span style="white-space: pre;">	</span>localhost</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">::1<span style="white-space: pre;">	</span>localhost ip6-localhost ip6-loopback</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">fe00::0<span style="white-space: pre;">	</span>ip6-localnet</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">ff00::0<span style="white-space: pre;">	</span>ip6-mcastprefix</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">ff02::1<span style="white-space: pre;">	</span>ip6-allnodes</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">ff02::2<span style="white-space: pre;">	</span>ip6-allrouters</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">可以看到docker 已经在host文件中为该容器的ijp增加了一条主机配置项</span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">再看看IP<br /></span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">root@537739299f24:/#&nbsp;<span style="color: #ff0000;">ip a</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">1: lo: &lt;LOOPBACK,UP,LOWER_UP&gt; mtu 65536 qdisc noqueue state UNKNOWN group default&nbsp;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp; &nbsp; link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp; &nbsp; inet 127.0.0.1/8 scope host lo</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp; &nbsp; &nbsp; &nbsp;valid_lft forever preferred_lft forever</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp; &nbsp; inet6 ::1/128 scope host&nbsp;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp; &nbsp; &nbsp; &nbsp;valid_lft forever preferred_lft forever</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">30: eth0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc noqueue state UP group default&nbsp;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp; &nbsp; link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp; &nbsp; inet 172.17.0.2/16 scope global eth0</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp; &nbsp; &nbsp; &nbsp;valid_lft forever preferred_lft forever</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp; &nbsp; inet6 fe80::42:acff:fe11:2/64 scope link&nbsp;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp; &nbsp; &nbsp; &nbsp;valid_lft forever preferred_lft forever</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">可以看到这里有lo的回环接口。还有ip是172.17.0.2的标准eth0接口，和宿主机完全一样。</span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">查看容器 中的进程</span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">root@537739299f24:/#&nbsp;<span style="color: #ff0000;">ps -aux</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">USER &nbsp; &nbsp; &nbsp; &nbsp;PID %CPU %MEM &nbsp; &nbsp;VSZ &nbsp; RSS TTY &nbsp; &nbsp; &nbsp;STAT START &nbsp; TIME COMMAND</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">root &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp;0.2 &nbsp;0.1 &nbsp;21944 &nbsp;3528 ? &nbsp; &nbsp; &nbsp; &nbsp;Ss &nbsp; 03:03 &nbsp; 0:01 /bin/bash</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">root &nbsp; &nbsp; &nbsp; &nbsp; 12 &nbsp;0.0 &nbsp;0.0 &nbsp;19188 &nbsp;2336 ? &nbsp; &nbsp; &nbsp; &nbsp;R+ &nbsp; 03:14 &nbsp; 0:00 ps -aux</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">接下来，我想给容器安装一个vim</span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">这里我先更新下容器的源再安装。<br /></span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">root@537739299f24:/#&nbsp;<span style="color: #ff0000;">apt-get update &amp;&amp; apt-get install vim</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">安装完毕使用 exit，即可退出到宿主机系统。这时容器也会停止，因为一旦退出容器，</span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">容器也随之停止了运行。但是容器仍然存在，可以用 docker ps -a 查看容器列表。</span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">root@537739299f24:/# exit</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">exit</p><div><img src="http://www.cppblog.com/images/cppblog_com/zhiyewang/11.png" width="724" height="73" alt="" /></div><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">docker ps 可以查看运行中的容器，加上 -a 参数可以查看所有的容器<br /></span></span><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>3.4 容器命名</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">可以使用如下命令为容器指定一个合法有意义的名字。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">root@cloudsoar-virtual-machine:~#&nbsp;<span style="color: #ff0000;">docker run --name wzy_the_container -i -t paulcos11/docker-tutorial /bin/bash</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">root@0a09dfd688ea:/#&nbsp;<span style="color: #ff0000;">exit</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">exit</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">3.5 重新启动已经停止的容器</span></span></strong><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;"><br /></span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">此时，wzy_the_container 已经停止了。可以使用如下命令重启一个已经停止的容器。<br /></span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">root@cloudsoar-virtual-machine:~#&nbsp;<span style="color: #ff0000;">docker start wzy_the_container</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">wzy_the_container</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">也可以使用容器ID替换容器名。</span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;"><br /></span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">3.6 附着到容器上</span></span></strong><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;"><br /></span></span>容器启动的时候会按照docker run指定的参数来运行。因此这个容器启动后会启动一个交互</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">的shell。我可以使用 docker attach 直接附着到此容器。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">root@cloudsoar-virtual-machine:~#&nbsp;<span style="color: #ff0000;">docker attach wzy_the_container</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: #ff0000;">root@0a09dfd688ea:/#</span>&nbsp;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">可以看到重新会到了容器的Bash提示符界面。<br /></span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">如果退出容器的shell，容器也会随之停止运行。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>3.7 创建守护式容器</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">除了交互式运行的容器，我们也可以创建守护容器。可以长期的运行应用程序或者服务。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">大多时候需要创建守护式容器。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">[#10#cloudsoar@cloudsoar-virtual-machine ~]$<span style="color: #ff0000;">sudo docker run --name daemon_dave -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">cf5636f0499fa0105b845b5dfa61913ff7d5fccfb1b19c16b09ed2752233794d</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">由于此时第 3.2 步我下载的 Ubuntu 基础镜像Downloading 48.09 MB/65.69 MB 刚好下载</span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">完毕，所以此时我开始使用ubuntu容器镜像操作了。</span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">docker run 的参数 -d 是放到后台执行。此时我们并没有像上一个容器直接附着到新容器的</span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">shell会话上，而是返回了一个容器ID而已。此时使用 docker ps 可以看到正在运行的容器。</span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><img src="http://www.cppblog.com/images/cppblog_com/zhiyewang/12.png" width="711" height="66" alt="" /><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">ubuntu就是我刚刚下载完毕的基础镜像。 paulcos11/docker-tutorial 是我自己下载的其他的镜像。<br /></span></span><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>3.8 容器内部再搞什么</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">使用 docker logs 获取容器日志</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">[#16#cloudsoar@cloudsoar-virtual-machine ~]$<span style="color: #ff0000;">sudo docker logs daemon_dave</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">hello world</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">hello world</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">hello world</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">hello world</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">hello world</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">hello world</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">或者可以加上-f 实时监控容器日志。</span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">sudo docker logs -f daemon_dave</span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">也可以追踪日志某一片段，只需要tail命令加入 -f --lines标志即可。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">例如： docker logs --tail 10 daemon_dave 获取最后10行日志即可。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp;docker logs --tail 0 -f daemon_dave 获取最新日志。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">如果要查看日志的时间，可以加上时间戳。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">[#18#cloudsoar@cloudsoar-virtual-machine ~]$<span style="color: #ff0000;">sudo docker logs -ft daemon_dave</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">2016-03-15T03:45:28.457991514Z hello world</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">2016-03-15T03:45:29.472422582Z hello world</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">2016-03-15T03:45:30.475130491Z hello world</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;"><br /></span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">3.9 查看容器进程</span></span></strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">查看容器内的进程可以使用 docker top 命令。<br /></span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">[#20#cloudsoar@cloudsoar-virtual-machine ~]$<span style="color: #ff0000;">sudo docker top daemon_dave</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">UID &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PID &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PPID &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;C &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; STIME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TTY &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TIME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CMD</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">root &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;25381 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 22325 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 11:45 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ? &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 00:00:00 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/bin/sh -c while true; do echo hello world; sleep 1; done</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;"><br /></span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">3.10 在容器内部运行进程</span></span></strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">我们可以使用docker exec 命令在容器内部额外启动新进程，可以运行的进程有两种类型，</span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">交互式任务和后台任务。首先我们使用 touch 命令创建一个空文件。</span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff; color: #ff0000;">$sudo docker exec -d daemon_dave touch /etc/new_config_file</span></span><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">-d 表示是后台进程。</span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">或者打开一个shell</span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: #ff0000;">$sudo docker exec -t -i daemon_dave /bin/bash</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">root@cf5636f0499f:/#</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;"><br /></span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">3.11 停止守护式容器</span></span></strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">$<span style="color: #ff0000;">sudo docker stop daemon_dave</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">daemon_dave</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">3.12 自动重启容器</span></span></strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">如果由于某种错误导致容器停止，可以通过 --restart 标志让docker自动重新启动容器。</span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="font-family: 微软雅黑, sans-serif;">$<span style="color: #ff0000;">sudo docker run --restart=on-failure:5 --name daemon_dave -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"</span></span><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">Error response from daemon: Conflict. The name "daemon_dave" is already in use by container cf5636f0499f. You have to remove (or rename) that container to be able to reuse that name.</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">这里我的运行不起来，因为 -- restart 标示是docker 2.0 引入的。我的是docker 1.9版本。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>3.13 深入容器</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">除了使用docker ps 查看容器信息，还可以使用 docker inspect 查看更多信息</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">$<span style="color: #ff0000;">sudo docker inspect daemon_dave</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">[sudo] password for cloudsoar:</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">[</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">{</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp; &nbsp; "Id": "cf5636f0499fa0105b845b5dfa61913ff7d5fccfb1b19c16b09ed2752233794d",</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp; &nbsp; "Created": "2016-03-15T03:45:28.124542139Z",</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp; &nbsp; "Path": "/bin/sh",</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp; &nbsp; "Args": [</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp; &nbsp; &nbsp; &nbsp; "-c",</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp; &nbsp; &nbsp; &nbsp; "while true; do echo hello world; sleep 1; done"</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp; &nbsp; ],</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp; &nbsp; "State": {</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp; &nbsp; &nbsp; &nbsp; "Status": "exited",</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp; &nbsp; &nbsp; &nbsp; "Running": false,</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp; &nbsp; &nbsp; &nbsp; "Paused": false,</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp; &nbsp; &nbsp; &nbsp; "Restarting": false,</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp; &nbsp; &nbsp; &nbsp; "OOMKilled": false,</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp; &nbsp; &nbsp; &nbsp; "Dead": false,</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp; &nbsp; &nbsp; &nbsp; "Pid": 0,</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp; &nbsp; &nbsp; &nbsp; "ExitCode": 137,</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp; &nbsp; &nbsp; &nbsp; "Error": "",</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp; &nbsp; &nbsp; &nbsp; "StartedAt": "2016-03-15T03:45:28.455126857Z",</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp; &nbsp; &nbsp; &nbsp; "FinishedAt": "2016-03-15T04:05:11.945521026Z"</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp; &nbsp; },</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&nbsp; &nbsp;...</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">也可使用 -f 或者 --format 标志来选定查看结果。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: #ff0000;">$sudo docker inspect --format='{{ .State.Running}}' daemon_dave</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">false</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">以及查看其他信息</span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: #ff0000;">$sudo docker start daemon_dave</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">daemon_dave</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: #ff0000;">$sudo docker inspect --format '{{ .NetworkSettings.IPAddress }}' daemon_dave</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">172.17.0.3</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;"><br /></span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">3.14 删除容器</span></span></strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">如果容器不在使用，可以使用 docker rm 命令删除。</span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"><span style="background-color: #ffffff;">这里假设我要删除 daemon_dave 这个容器，他的容器ID是&nbsp;cf5636f0499f</span></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">$<span style="color: #ff0000;">sudo docker rm cf5636f0499f</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">Error response from daemon: Conflict, You cannot remove a running container. Stop the container before attempting removal or use -f</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">Error: failed to remove containers: [cf5636f0499f]</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">$<span style="color: #ff0000;">sudo docker stop cf5636f0499f</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">cf5636f0499f</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="background-color: #d8d8d8;"></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><span style="color: #ff0000;">$sudo docker rm cf5636f0499f</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">cf5636f0499f</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">由于不能删除正在运行的容器，所以需要先停止。</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">&#8203;<br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;"><strong>3.15 小结</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微软雅黑; widows: auto;">下一章学习如何构建自己的Docker镜像，以及如何使用Docker 仓库和 Docker Registry.</p><img src ="http://www.cppblog.com/zhiyewang/aggbug/213023.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhiyewang/" target="_blank">zhiye_wang</a> 2016-03-15 13:43 <a href="http://www.cppblog.com/zhiyewang/archive/2016/03/15/213023.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>