??xml version="1.0" encoding="utf-8" standalone="yes"?>999久久久国产精品,国产高潮国产高潮久久久91 ,国产精品无码久久综合http://www.shnenglu.com/zhiyewang/向星IZԒ望的深Q越发现自己的ؓ?/description>zh-cnTue, 06 May 2025 23:00:51 GMTTue, 06 May 2025 23:00:51 GMT60ubuntu安装OpenLDAP(? 配置LDAPhttp://www.shnenglu.com/zhiyewang/archive/2016/04/20/213312.htmlzhiye_wangzhiye_wangWed, 20 Apr 2016 09:31:00 GMThttp://www.shnenglu.com/zhiyewang/archive/2016/04/20/213312.htmlhttp://www.shnenglu.com/zhiyewang/comments/213312.htmlhttp://www.shnenglu.com/zhiyewang/archive/2016/04/20/213312.html#Feedback0http://www.shnenglu.com/zhiyewang/comments/commentRss/213312.htmlhttp://www.shnenglu.com/zhiyewang/services/trackbacks/213312.htmlubuntu安装OpenLDAP(? 配置LDAP

上一博文中Q我们成功的安装了OpenLDAP-2.4.4到系l中Q这文章介l如何配|一个我

们自qldap服务器来使用?/p>

1 配置ldap的dc和cn

vim /usr/local/etc/openldap/slapd.conf

修改其中的下面两行:

suffix      "dc=example,dc=com"

rootdn      "cn=Manager,dc=example,dc=com"

我修改后的效果如图:


启动slapd

# su root -c /usr/local/libexec/slapd

验证一下能不能使用Q?/span>

# ldapsearch -x -b '' -s base'(objectclass=*)'


说明ldap服务器已l可以用了?/span>

3 新徏理账号
建立一?/span>cloudsoar.ldif 文g

# vim cloudsoar.ldif

dn: dc=cloudsoar,dc=com

objectclass: dcObject

objectclass: organization

o: cloudsoar company

dc: cloudsoar

 

dn:cn=Manager,dc=cloudsoar,dc=com

objectclass:organizationalRole

cn: Manager

文件中的内Ҏ入数据库

ldapadd -x -D "cn=Manager,dc=cloudsoar,dc=com"-W -f cloudsoar.ldif 

如果需要密码,我们的默认密码是Q?/span>secretQ位?/span>/usr/local/etc/openldap/slapd.confQ?/span>


q里可以看到节点已经插入?/span>ldap了。这里的cloudsoar是我L域名Q您也可以根据需要设|一个自己喜Ƣ的域名?/span>

查询一下:

# ldapsearch -x -b 'dc=cloudsoar,dc=com''(objectclass=*)'


q里我还是不攑ֿQ从另外一台安装了windows操作pȝ的电脑上Q?/span>LDAPAdmin.exe 来连接试试?/span>



可以看到我的ldap已经可以使用了?/span>




zhiye_wang 2016-04-20 17:31 发表评论
]]>
ubuntu安装OpenLDAP(附错误的详细解决办法)http://www.shnenglu.com/zhiyewang/archive/2016/04/20/213311.htmlzhiye_wangzhiye_wangWed, 20 Apr 2016 09:27:00 GMThttp://www.shnenglu.com/zhiyewang/archive/2016/04/20/213311.htmlhttp://www.shnenglu.com/zhiyewang/comments/213311.htmlhttp://www.shnenglu.com/zhiyewang/archive/2016/04/20/213311.html#Feedback0http://www.shnenglu.com/zhiyewang/comments/commentRss/213311.htmlhttp://www.shnenglu.com/zhiyewang/services/trackbacks/213311.html

ubuntu安装OpenLDAP(附错误的详细解决办法)

下蝲OpenLDAP源码


 http://www.openldap.org/software/download/

或?/p>

 ftp://ftp.openldap.org/pub/OpenLDAP/openldap-release.tgz


解压到本?/span>

 # gunzip-c openldap-VERSION.tgz | tar xf -

# cd openldap-2.4.44

# ./configure

 configure: error: BDB/HDB: BerkeleyDB notavailable

提示本地没有安装BerkeleyDB数据?/span>

安装BerkeleyDB

 ?/span>Oracle官网下蝲Q?/span>

 http://www.oracle.com/technetwork/database/database-technologies/berkeleydb/downloads/index.html

解压到本?/span>


切换?/span>build_unix 目录

# cd build_unix

# ../dist/configure

# make

# make install

root@cloudsoar-virtual-machine:/home/cloudsoar/db-6.2.23/build_unix#make install

Installing DB include files:/usr/local/BerkeleyDB.6.2/include ...

Installing DB library:/usr/local/BerkeleyDB.6.2/lib ...

libtool: install: cp -p .libs/libdb-6.2.so/usr/local/BerkeleyDB.6.2/lib/libdb-6.2.so

libtool: install: cp -p .libs/db_upgrade/usr/local/BerkeleyDB.6.2/bin/db_upgrade

libtool: install: cp -p .libs/db_verify/usr/local/BerkeleyDB.6.2/bin/db_verify

Installing documentation:/usr/local/BerkeleyDB.6.2/docs ...

查看安装


讄到系l变?/span>

不设|的话,{下安装OpenLDAP时候执?/span>./configure查时候还会报同样的错

# vim /etc/ld.so.conf

d一行:/usr/local/BerkeleyDB.6.2/lib/

保存退出?/span>

执行生效Q?span style="color: #ff0000;"># ldconfig –v

l箋安装openldap-2.4.44


切换?/span>openldap的源码目录下Q重新执?/span>configure文g

./configure CPPFLAGS="-I/usr/local/BerkeleyDB.6.2/include-D_GNU_SOURCE" LDFLAGS="-L/usr/local/BerkeleyDB.6.2/lib"

执行后报?/span>

error: BerkeleyDB version incompatible withBDB/HDB backends

q里找不到原因,后来查看openladp-2.4.44,目录?/span>README文g发现如下内容Q?/span>


郁闷Q原?/span>openldap-2.4.44要求?/span>Oracle Berkeley 4.4-4.8或?/span>5.0-5.1版本的,而我用的?/span>db-6.2.23.tar.gz版本的?/span>

没办法,只有重新?/span>Oracle官网下蝲一?/span>db-5.1.29.tar.gz版本的?/span>

按照安装Berkeley 6.2的方法再ơ安?/span>Berkeley DB 5.1?/span>

# cd build_unix

# ../dist/configure

# make

# make install

# vim /etc/ld.so.conf

d一行:/usr/local/BerkeleyDB.5.1/lib/

保存退出?/span>

执行生效Q?/span>ldconfig –v

 

安装openldap          

切换?/span>openldap-2.4.44目录

./configure CPPFLAGS="-I/usr/local/BerkeleyDB.5.1/include-D_GNU_SOURCE" LDFLAGS="-L/usr/local/BerkeleyDB.5.1/lib"

执行完毕可以看到提示我执?/span>make depand?/span>


~译软g

Ҏhttp://www.openldap.org/doc/admin24/install.html的安装步骤l执?/span>

执行命oQ?/span># make depend


执行命oQ?/span># make

10 试软g

Ҏ官方文档说明Q?/span>Once the software has been properly configured and successfullymade, you should run the test suite to verify the build.

我们也需要测试一下安装是否成功,执行命oQ?/span># make test


说明安装环境是没问题了?/span>

11 安装openldap到系l?/span>

Ҏ官方文档说明Q?/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.

我们可以?/span>–prefix 参数来指定我们自己想要安装的位置。默认是安装?/span> /usr/local目录下?/span>

执行命oQ?/span># su root -c 'make install'


到这?/span>openldap-2.4.44已经成功的安装到我的pȝ当中。默认的配置文g?/span> /usr/local/etc/openldap 下?/span>




zhiye_wang 2016-04-20 17:27 发表评论
]]>
CentOS安装q行mongo docker镜像http://www.shnenglu.com/zhiyewang/archive/2016/04/13/213258.htmlzhiye_wangzhiye_wangWed, 13 Apr 2016 03:42:00 GMThttp://www.shnenglu.com/zhiyewang/archive/2016/04/13/213258.htmlhttp://www.shnenglu.com/zhiyewang/comments/213258.htmlhttp://www.shnenglu.com/zhiyewang/archive/2016/04/13/213258.html#Feedback0http://www.shnenglu.com/zhiyewang/comments/commentRss/213258.htmlhttp://www.shnenglu.com/zhiyewang/services/trackbacks/213258.html阅读全文

zhiye_wang 2016-04-13 11:42 发表评论
]]>
Go 语言教程W记Q三Q?/title><link>http://www.shnenglu.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.shnenglu.com/zhiyewang/archive/2016/03/28/213139.html</guid><wfw:comment>http://www.shnenglu.com/zhiyewang/comments/213139.html</wfw:comment><comments>http://www.shnenglu.com/zhiyewang/archive/2016/03/28/213139.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/zhiyewang/comments/commentRss/213139.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/zhiyewang/services/trackbacks/213139.html</trackback:ping><description><![CDATA[<p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><strong><span style="font-family: 微Y雅黑, sans-serif;">Go 语言教程W记Q?/span><span style="font-family: 微Y雅黑, sans-serif;">?/span><span style="font-family: 微Y雅黑, sans-serif;">Q?/span></strong><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><strong>一 Go语言决策</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><strong>if 语句</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">if语句包含一个布表辑ּ后跟一个或多个语句</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">语法</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">if语句在Go~程语言的语法是Q?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">if(boolean_expression)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">{</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">   /* statement(s) will execute if the boolean expression is true */</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">} </p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">如果布尔表达式的gؓ trueQ那么if语句里面代码块将被执行。如果if语句的结?叛_括号?</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">布尔表达式的gؓfalseQ那么语句之后第一行代码会被执行?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">例子Q?/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("http://www.yiibai.com/go/images/try-it.jpg") !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 < 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', 微Y雅黑, STHeiti, 'WenQuanYi Micro Hei', SimSun, sans-serif, sans-serif; font-size: 16px; color: #535b60; line-height: 22px;">让我们编译和q行上面的程序,q将产生以下l果Q?/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: 微Y雅黑; widows: auto;"><strong>if else 语句</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">if语句可以跟着一个可选的else语句Q布表辑ּ是假时它被执行?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">语法</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">在Go~程语言中的if ... else语句的语法是Q?/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: 微Y雅黑; widows: auto;">如果布尔表达式的gؓtrueQ那么if代码块将被执行,否则else代码块将被执?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">例子Q?/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("http://www.yiibai.com/go/images/try-it.jpg") !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 < 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: 微Y雅黑; widows: auto;">当上qC码被~译和执行时Q它产生了以下结果:</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: 微Y雅黑; widows: auto;"><strong>if...else if...else 语句</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">if语句可以跟着一个可选的else if ... else语句Q这是非常有用的使用单个 if...else if 语句声明试各种条g?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">当用if , else if , else语句有几点要C使用Q?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">if可以有零或一个elseQ它必须跟从else if后面?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">一个if可以有零C多else ifq且它们必须在else之前?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">一旦一个else if试成功Q其它Q何剩余else if不会被试?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">语法</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">if...else 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: 微Y雅黑; widows: auto;">例子Q?/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("http://www.yiibai.com/go/images/try-it.jpg") !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: 微Y雅黑; widows: auto;">让我们编译和q行上面的程序,q将产生以下l果</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: 微Y雅黑; widows: auto;"><strong>switch 语句</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">switch语句可以让一个变量对反对值的列表q等q行试。每个DUCؓ一个的情况(case)Q?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">变量被接通检查每个开关盒(switch case)?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">在Go~程Qswitch有两U类型?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"> <span style="font-family: 微Y雅黑, sans-serif;">表达式Switch - 在表辑ּswitchQcase包含相比较,switch表达式的倹{?/span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"> <span style="font-family: 微Y雅黑, sans-serif;">cdSwitch - 在这cdswitchQ此时含有进行比较特D注明开兌辑ּ的类型?/span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><strong> <span style="font-family: 微Y雅黑, sans-serif;">表达式Switch</span></strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; 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: 微Y雅黑; widows: auto;">以下规则适用于switch语句Q?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">在switch语句中用的表达式必d有整体或布尔表达式,或者是一个类型,其中所q类h</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">一个单一的{换函敎ͼ以一个整体或布尔倹{如果表达不通过Q默认值是true?span style="font-family: 微Y雅黑, sans-serif;">可以有Q意数</span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><span style="font-family: 微Y雅黑, sans-serif;">量的case语句在switch内。每个case后跟D行比较,以及一个冒受?/span><span style="font-family: 微Y雅黑, sans-serif;">constant-expression </span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><span style="font-family: 微Y雅黑, sans-serif;">的情况,必须是相同的数据cdQ在switch的变量,它必L一个常?/span><span style="font-family: 微Y雅黑, sans-serif;">或文字?/span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">当变量被接通等于case的|以下case中将执行语句。在case语句中break不是必需?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">switch语句可以有一个可选默认情况下Q它必须出现在开关结束。缺省情况下Q可用于执行?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">务时没有的case为true。则case在默认情况下也不是必ȝ?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; 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("http://www.yiibai.com/go/images/try-it.jpg") !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: 微Y雅黑; widows: auto;">当上qC码被~译和执行时Q它产生了以下结果:</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: 微Y雅黑; widows: auto;"><strong>cdSwitch</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; 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: 微Y雅黑; widows: auto;">以下规则适用于switch语句Q?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">在switch语句中用必L接口的变量表辑ּ{}输入?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">在switch内可以有L数量case语句。每一Ucase后跟的D行比较,以及一个冒受?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">case cd必须是相同的数据cdQ在switch的变量,它必L一个有效的数据cd?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">当变量被接通等于某一case中的|以下case语句执行。在case语句块的break不是必需的?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">switch语句可以有一个可选默认caseQ它必须出现在switch的结束。缺省情况下Q可用于执行</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">d时没有匹配case时。default不是必需的?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; 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("http://www.yiibai.com/go/images/try-it.jpg") !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: 微Y雅黑; widows: auto;">让我们编译和q行上面的程序,q将产生以下l果Q?/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 :<nil></pre><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><strong> select语句</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; 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: 微Y雅黑; widows: auto;">以下规则适用于select语句Q?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">可以有Q意数量的范围内选择一个case语句。每一U情况下后跟的D行比较,以及一个冒受?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">对于case的类型必L一个通信通道操作?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">当通道q行下面发生的语句这U情况将执行。在case语句中break不是必需的?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">select语句可以有一个可选默认caseQ它必须出现在select的结束前。缺省情况下Q可用于执行</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">d时没有的情况下是真实的。在默认情况下break不是必需的?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">例如Q?/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("http://www.yiibai.com/go/images/try-it.jpg") !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 = <-c1: fmt.Printf("received ", i1, " from c1\n") case c2 <- i2: fmt.Printf("sent ", i2, " to c2\n") case i3, ok := (<-c3): // same as: i3, ok := <-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: 微Y雅黑; widows: auto;">让我们编译和q行上面的程序,q将产生以下l果Q?/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: 微Y雅黑; widows: auto;">​</p><img src ="http://www.shnenglu.com/zhiyewang/aggbug/213139.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/zhiyewang/" target="_blank">zhiye_wang</a> 2016-03-28 16:54 <a href="http://www.shnenglu.com/zhiyewang/archive/2016/03/28/213139.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Go 语言教程W记Q二http://www.shnenglu.com/zhiyewang/archive/2016/03/28/213136.htmlzhiye_wangzhiye_wangMon, 28 Mar 2016 08:17:00 GMThttp://www.shnenglu.com/zhiyewang/archive/2016/03/28/213136.htmlhttp://www.shnenglu.com/zhiyewang/comments/213136.htmlhttp://www.shnenglu.com/zhiyewang/archive/2016/03/28/213136.html#Feedback0http://www.shnenglu.com/zhiyewang/comments/commentRss/213136.htmlhttp://www.shnenglu.com/zhiyewang/services/trackbacks/213136.htmlGo 语言教程W记Q二Q?/strong>

一 Go 语言帔R

帔R是指该程序可能无法在其执行期间改变的固定倹{这些固定g被称为文字?/p>

帔R可以是Q何像一个整型常量,一个Q点常量,字符帔R或字W串文字的基本数据类型。还

有枚丑ָ量?span style="font-family: 微Y雅黑, sans-serif;">帔R是一P只是它们的g能自己定义后q行修改常规变量处理?/span>

整Ş帔R

一个整数文字也可以有一个后~为U和L的组合,分别为无W号和长整型。后~可以是大写或?/p>

写,q且可以以Q意顺序?span style="font-family: 微Y雅黑, sans-serif;">可以是十q制Q八q制Q或十六q制常数。前~指定基或基数Q?x

?0X 的十六进Ӟ0 表示八进Ӟq没有十q制?/span>

整数帔R的一些例子:

212         /* Legal */

215u        /* Legal */

0xFeeL      /* Legal */

078         /* Illegal: 8 is not an octal digit */

032UU       /* Illegal: cannot repeat a suffix */

不同cd的整型常量的例子Q?/p>

85         /* decimal */

0213       /* octal */

0x4b       /* hexadecimal */

30         /* int */

30u        /* unsigned int */

30l        /* long */

30ul       /* unsigned long */

点文本(帔R)

点字面h一个整数部分,一个小数点Q一个小数部分,和一个指数部分。你可以表示十进

制Ş式或指数形式点文字?span style="font-family: 微Y雅黑, sans-serif;">同时采用十进制Ş式表C,则必d括小数点Q指敎ͼ或两者ƈ

用而指数Ş式表C,则必d括整数部分,数部分Q或者两者兼而有之。有W号的指敎ͼ?/span>

qe或E表示?/span>

下面是Q炚w值的一些例子:

3.14159       /* Legal */

314159E-5L    /* Legal */

510E          /* Illegal: incomplete exponent */

210f          /* Illegal: no decimal or exponent */

.e55          /* Illegal: missing integer or fraction */

字符串文?/strong>

字符串文字或帔R用双引号“”。一个字W串包含cM于字W文字字W:普通字W,转义序列

和通用字符?span style="font-family: 微Y雅黑, sans-serif;">可以使用字符串和分隔使用I格打破一个长行成多行?/span>

"hello, dear"

"hello, 

dear"

"hello, " "d" "ear"

const 关键?/strong>

package main

import "fmt"

func main() {

   const LENGTH int = 10

   const WIDTH int = 5   

   var area int

   area = LENGTH * WIDTH

   fmt.Printf("value of area : %d", area)   

}

习惯大写定义帔R是一个良好的~程习惯?/span>

?Go 语言q算W?/strong>

Go语言有丰富的内置q算W和q算W:

术q算W?/p>

关系q算W?/p>

逻辑q算W?/p>

位运符

赋D符

其它q算W?/p>


术q算W?/strong>

q算W?/th>描述CZ
+两个操作数相?/td>A + B = 30
-W一个操作数减第二操作数A - B = -10
*两个操作数相?/td>A * B = 200
/通过d子除以分?/td>B / A = 2
%模运和整数除法后的余数B % A = 0
++q算W递增整数值增加一A++ = 11
--q算W递减整数值减一A-- = 9

关系q算W?/strong>

q算W?/th>描述CZ
==查两个操作数的值是否相{,如果是的话那么条件ؓ真?/td>(A == B) 不ؓ true.
!=查两个操作数的值是否相{,如果g相等Q则条g变ؓ真?/td>(A != B) 为true.
>查左边的操作数的值是否大于右操作数的|如果是的话那么条件ؓ真?/td>(A > B) 不ؓ true.
<查左边的操作数的值是否小于右操作数的|如果是的话那么条件ؓ真?/td>(A < B) ?nbsp;true.
>=查左边的操作数的值是否大于或{于x作数的|如果是的话那么条件ؓ真?/td>(A >= B) 不ؓ true.
<=查左边的操作数的值是否小于或{于x作数的|如果是的话那么条件ؓ真?/td>(A <= B) ?nbsp;true

逻辑q算W?/span>

&&所谓逻辑与运符。如果两个操作数都非Ӟ则条件变为真?/td>(A && B) ?false.
||所谓的逻辑或操作。如果Q何两个操作数是非Ӟ则条件变为真?/td>(A || B) ?nbsp;true.
!所谓逻辑非运符。用反转操作数的逻辑状态。如果条件ؓ真,那么逻辑非操后结果ؓ假?/td>!(A && B) ?nbsp;true.

位运符

pqp & qp | qp ^ q
00000
01011
11110
10011

假设Q如果A =60;且b =13;现在以二q制格式它们如下Q?/p>

A = 0011 1100

B = 0000 1101

-----------------

A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

~A  = 1100 0011

C语言支持位运符列在如下表。假讑֏量A=60和变量B=13Q则

其它q算W?/strong>

q有其他一些重要的q算W,包括sizeof?:在Go语言中也支持?/p>

Go语言q算W优先

分类 q算W?nbsp;兌 
后缀() [] -> . ++ - -  从左到右
一?/td>+ - ! ~ ++ - - (type)* & sizeof 从右到左
乘法* / % 从左到右
相加  + - 从左到右 
Ud<< >> 从左到右
关系< <= > >= 从左到右
相等== != 从左到右
按位?/td>从左到右
按位异或从左到右
按位?nbsp;从左到右
逻辑?nbsp;&& 从左到右
逻辑?nbsp;|| 从左到右
条g ?: 从左到右
赋?/td>= += -= *= /= %=>>= <<= &= ^= |= 从右到左
逗号 从左到右


zhiye_wang 2016-03-28 16:17 发表评论
]]>
Go 语言教程W记Q一Q?/title><link>http://www.shnenglu.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.shnenglu.com/zhiyewang/archive/2016/03/28/213135.html</guid><wfw:comment>http://www.shnenglu.com/zhiyewang/comments/213135.html</wfw:comment><comments>http://www.shnenglu.com/zhiyewang/archive/2016/03/28/213135.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/zhiyewang/comments/commentRss/213135.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/zhiyewang/services/trackbacks/213135.html</trackback:ping><description><![CDATA[<p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><strong>Go 语言教程W记Q一Q?/strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><strong>一 Go 语言环境讄 </strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><strong>本地环境讄</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">在这里我们介l设|Go~程语言环境Q需要在你的计算Z的准备以下两个YӞ(A)文本~?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">辑器?B)Go~译器?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><strong>文本~辑?/strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">我的环境是ubuntuQ直接用 vim</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><strong>Go~译?/strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">ubuntu下面直接安装</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"># apt-get install golang</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">例子Q?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"># vim hello.go</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">  1 package main</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">  2 </p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">  3 import fmt "fmt"</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">  4 </p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">  5 func main() {</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">  6     fmt.Printf("Hello, world; or Καλημέρα κόσμε; or こんにち?nbsp;世界 ");</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">  7 }                                                                        </p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">go语言中如果有if{后面需要跟大括L语句Q强制用大扩号跟if写在一?span style="font-family: 微Y雅黑, sans-serif;">?/span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><span style="font-family: 微Y雅黑, sans-serif;"><br /></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><span style="font-family: 微Y雅黑, sans-serif;">~译q行Q?/span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"># go run hello.go </p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">Hello, world; or Καλημέρα κόσμε; or こんにち?nbsp;世界</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><strong><span style="font-family: 微Y雅黑, sans-serif;">?Go 语言E序l构 </span></strong><span style="font-family: 微Y雅黑, sans-serif;"><br /></span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">在我们学习Go~程语言的基本构建模块,看看一个最低限度的GoE序l构Q这h?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">可以把它作为即到来的章节的参考?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><strong>Go E序包含以下部分</strong>Q?br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">包声?br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">导入?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">函数</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">变量</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">语句和表辑ּ</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">注释</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">例如Q?br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"> 1 package main</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">  2 </p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">  3 import fmt "fmt"</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">  4 </p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">  5 func main() {</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">  6     fmt.Printf("Hello, world; or Καλημέρα κόσμε; or こんにち?nbsp;世界 ");</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">  7 }  </p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">W一行定义了q个E序包的名称。这是一个必d明ؓGoE序q行在什么包。main包是起始</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">Ҏq行E序。每个包都有一个与之关联的路径和名U?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">下一行import "fmt" 是告诉编译器d含文件在包fmt的预处理命o?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">下一?nbsp;func main()主要功能是ؓE序执行的开始?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">如果有注释,/*...*/会被~译器被忽略Q它已被加入到程序添加注释。因此,q样的行UCؓE?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">序中的注释。注释也使用//cM于Java或C++注释?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">下一?nbsp;fmt.PrintlnQ?..Q是提供另一U功能,使消?#8220;Hello, World!”要显C在屏幕上。这</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">里fmt包已导出?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><strong>?Go 语言的基本语?/strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><strong>标识W?/strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">Go语言不允许标识符中的标点字符Q如@Q???/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">Go是一U区分大写的编E语a</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">Manpower ?nbsp;manpower 在Go中是两个不同的标识符</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">标识W开始以字母A到Z或a到z或下划线_后跟零个或多个字母,下划U和数字Q??Q?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><strong>关键?/strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">下面的列表显CZGo的保留字。这些保留的字可以不被用作常量或变量Q或M其他的标识符</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">名称?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">break  default  func  interface  select</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">case  defer  go  map  struct</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">chan  else  goto  package  switch</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">const  fallthrough  if  range  type</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">continue  for  import  return  var</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><strong>?Go 中空白格</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">仅包含空|可能与注释行Q被UCؓ一个空行,Go~译器完全忽略它?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">var age int;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">必须有至一个空白字W(通常是一个空|int和age之间的编译器Q以便能够区分它?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">fruit = apples + oranges;   // get the total fruit</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">fruit?之间?和applesQ虽然是自由的,如果想便于阅ȝ目的Q最好包括一些空?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><strong>?Go 语言数据cd</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">在Go~程语言Q数据类型指用于声明变量或不同类型的功能的广泛的pȝ。变量的cd军_?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">有多空间占用的存储和存储方式的位模式将被解释?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">Go的数据类型可分类如下Q?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">Boolean Types</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">它们是布类型,它由两个预定义的帔RQ?a) true (b) false</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">Numeric Types</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">它们是算术类型,表示整数cd或b。在整个E序点?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">string types:</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">一个字W串cd代表l字W串倹{它的值是一个字节序列。字W串是一ơ创造了E_的类型,</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">q是不可能改变一个字W串的内宏V预声明的字W串cd为字W串?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">Derived types:</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">它们包括Q?a)指针cdQ?b)数组cdQ?c)l构cdQ?d)联盟cd?e)函数cd(f)切片c?g)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">函数cd(h)接口cd(i)地图cd(j)道cd?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">数组cd和结构类型被l称合类型。函数的cd指定的一l具有相同的参数和结果类型所</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">有函数?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><strong>整数</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">1 uint8</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">8位无W号整数 (0 - 255)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">2 uint16</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">16位无W号整数 (0 - 65535)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">3 uint32</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">32位无W号整数 (0 - 4294967295)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">4  uint64</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">64位无W号整数 (0 - 18446744073709551615)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">5  int8</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">有符?位整?nbsp;(-128 - 127)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">6  int16</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">有符?6位整?nbsp;(-32768 - 32767)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">7  int32</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">有符L32位整?nbsp;(-2147483648 - 2147483647)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">8  int64</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">有符L64位整?nbsp;(-9223372036854775808 - 9223372036854775807)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><strong>点cd</strong><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">1  float32</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">IEEE-754 32-bit 点?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">2  float64</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">IEEE-754 64-bit 点?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">3  complex64</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">复数与float32实部和虚?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">4  complex128</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">复数与float64实部和虚?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><strong>其他数值类?/strong><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">1  byte</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">相同?nbsp;uint8</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">2  rune</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">相同?nbsp;int32</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">3  uint</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">32 ?nbsp;64 ?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">4  int</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">相同?nbsp;uint 的大?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">5  uintptr</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">一个无W号整数来存储指针值的解释的比特位</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><strong>?Go 语言变量</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">变量是什么,只不是给定到存储区域Q我们的E序可以操纵的名U。在Go中每个变量具有特?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">的类型,它确定的大小和可变的存储器的布局;能确定存储器内存储的值的范围;和组操作可以?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">加到变量?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">一个变量名可以由字母,数字和下划线。它必须以字母或下划Uѝ大写和写字母是不同的Q因</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">为Go是区分大写的。基于该基本cd在前面的章节中说明的那样Q将有以下基本变量类型:</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">byte  通常单个字节(一个字?Q这是一个字节的cd</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">int  整数最自然的尺寸的机器</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">float32  单精度Q点?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">Go~程语言也可以定义各U其他类型的变量Q我们将在以后的章节列出Q如Q枚举,指针Q数</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">l,l构Q联合,{等?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><strong>Go中变量定?/strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">var variable_list optional_data_type;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">optional_data_type可以包括字节Q整型,float32Qcomplex64Q布或M用户定义的对?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">{有效Go的数据类型,variable_list可以q逗号分隔的一个或多个标识W名U。一些有效的?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">明如下所C:</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">var    i, j, k int;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">var   c, ch byte;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">var  f, salary float32;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">d = 42;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">var i, j, k; 既声明ƈ定义了变量iQj和k;q指C编译器创徏一个名为iQj和k?nbsp;intcd变量。变?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">可以再声明时候初始化。变量的cd?span style="font-family: 微Y雅黑, sans-serif;">q译器自动Ҏ传递给它的值判断?/span></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">variable_name = value;</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">d = 3, f = 5;    // declaration of d and f. Here d and f are int </p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">对于没有初始化定义:h静态存储时间变量的隐含零初始化(所有字节的gؓ0);所有其它变?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">的初始值是它们的数据类型的零倹{?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><strong>静态类型声?/strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">package main</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">import "fmt"</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">func main() {</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">   var x float64</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">   x = 20.0</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">   fmt.Println(x)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">   fmt.Printf("x is of type %T ", x)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">}</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">~译q行l果是:<br /></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">20</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">x is of type float64</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><strong>动态类型声?cd推断</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">CZ</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">试试下面的例子,其中的变量已l声明没有Q何类型的Qƈ已确定在dC初始化。如果类型推断的Q我们已l初始化的变量y使用:=q算W,x初始化?q算W?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">package main</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">import "fmt"</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">func main() {</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">   var x float64 = 20.0</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">   y := 42 </p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">   fmt.Println(x)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">   fmt.Println(y)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">   fmt.Printf("x is of type %T ", x)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">   fmt.Printf("y is of type %T ", y)  </p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">}</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">~译和运行上面的E序Q这生以下结果:</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">20</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">42</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">x is of type float64</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">y is of type int</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;"><strong>混合变量声明</strong></p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">不同cd的变量可以一步到位用类型推断声明?/p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">package main</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">import "fmt"</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">func main() {</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">   var a, b, c = 3, 4, "foo"  </p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">   fmt.Println(a)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">   fmt.Println(b)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">   fmt.Println(c)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">   fmt.Printf("a is of type %T ", a)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">   fmt.Printf("b is of type %T ", b)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">   fmt.Printf("c is of type %T ", c)</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">}</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">~译和运行上面的E序Q这生以下结果:</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">3</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">4</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">foo</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">a is of type int</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">b is of type int</p><p style="margin-right: 0px; margin-left: 0px; font-family: 微Y雅黑; widows: auto;">c is of type string</p><div></div><img src ="http://www.shnenglu.com/zhiyewang/aggbug/213135.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/zhiyewang/" target="_blank">zhiye_wang</a> 2016-03-28 15:33 <a href="http://www.shnenglu.com/zhiyewang/archive/2016/03/28/213135.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Rancher 快速上手指南操?1)http://www.shnenglu.com/zhiyewang/archive/2016/03/17/213053.htmlzhiye_wangzhiye_wangThu, 17 Mar 2016 03:03:00 GMThttp://www.shnenglu.com/zhiyewang/archive/2016/03/17/213053.htmlhttp://www.shnenglu.com/zhiyewang/comments/213053.htmlhttp://www.shnenglu.com/zhiyewang/archive/2016/03/17/213053.html#Feedback0http://www.shnenglu.com/zhiyewang/comments/commentRss/213053.htmlhttp://www.shnenglu.com/zhiyewang/services/trackbacks/213053.html阅读全文

zhiye_wang 2016-03-17 11:03 发表评论
]]>
W四?使用Docker镜像和仓??http://www.shnenglu.com/zhiyewang/archive/2016/03/17/213052.htmlzhiye_wangzhiye_wangThu, 17 Mar 2016 03:01:00 GMThttp://www.shnenglu.com/zhiyewang/archive/2016/03/17/213052.htmlhttp://www.shnenglu.com/zhiyewang/comments/213052.htmlhttp://www.shnenglu.com/zhiyewang/archive/2016/03/17/213052.html#Feedback0http://www.shnenglu.com/zhiyewang/comments/commentRss/213052.htmlhttp://www.shnenglu.com/zhiyewang/services/trackbacks/213052.htmlW四?nbsp;使用Docker镜像和仓??

回顾Q?/strong>

开始学习之前,我先pull下来ubuntu和fedora镜像

[#9#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker pull fedora

Using default tag: latest

latest: Pulling from library/fedora

9bdb5101e5fc: Pull complete 

Digest: sha256:1fa98be10c550ffabde65246ed2df16be28dc896d6e370dab56b98460bd27823

Status: Downloaded newer image for fedora:latest

[#10#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker pull ubuntu

Using default tag: latest

latest: Pulling from library/ubuntu

ebe73f29e6e1: Pull complete 

4976a0f2dc03: Pull complete 

5c117067c385: Pull complete 

001d664e2dd4: Pull complete 

Digest: sha256:7eb6ad74ec4fbe56ac194d8760063c88ca362f05a9038f2bc4f09a51849a4a53

Status: Downloaded newer image for ubuntu:latest

4.5.6 Dockerfile 和构建缓?/strong>

想略q缓存功能,可以使用 docker build ?--no-cache 标志

sudo docker build --no-cache -t="zhiyewang/static_web" .

4.5.7 Z构徏~存?Dockerfile 模板

FROM ubuntu:14.04

MAINTAINER zhiyewang "zhiye_wang@yeah.net"

ENV REFRESHED_AT 2016-03-16

RUN apt-get -qq update

q里要想重新构徏 Dockerfile Q只需要将W三行的日期修改以下卛_。将会更?APT 包的~?/p>

存?/p>

4.5.8 查看新镜?/strong>

查看镜像如何构徏出来Q可以?docker history 命o。可以看到新构徏?zhiyewang/stat

 ic_web 镜像的每一层。以及创些层?Dcoekrfile 命o?/p>

4.5.9 从构建的新镜像启动容?/p>

上一节成功?Dockerfile 命o构徏?zhiyewang/static_web q个镜像。现在我们来试试

看镜像是否工作正常。z

Z新构建的镜像启动一个新容器?/p>

 

[#17#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker run -d -p 80 --name stati

c_web zhiyewang/static_web nginx -g "daemon off;"

d4d9024c688d267761dee792e0b0686a6b2d06dcf53e656c98d95408f4894974

q条命oZҎ构徏的镜像名字,启动了一个名?static_web 的新容器?同时指定?-d ?/p>

,告诉 Docker 以分ȝ方式在后台运行。同时也指定了在新容器中q行的命? nginx -g "

daemon off;"。这以前台的方式启?Nginx。新标志 -p 用来控制 Docker 在运行时应该?/p>

开哪些|络端口l外部(宿主机)?/p>

 

[#19#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker ps -l

CONTAINER ID        IMAGE                            COMMAND                           PORTS                  

d4d9024c688d        zhiyewang/static_web   "nginx -g 'daemon off"         0.0.0.0:32768->80/tcp 

可以看到容器中的 80 端口被映到宿主机的 32768 端口?/p>

也可以?docker port 查看端口的情?/p>

[#20#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker port d4d9024c688d

80/tcp -> 0.0.0.0:32768

q样的端口映方式ؓ随机的,我们也可以指定特定的端口映射

容器的 80 端口l定到本地宿L?80 端口

sudo docker run -d -p 80:80 --name static_web zhiyewang/static_web nginx -g "daemon off;"

容器的 80 端口l定到本地宿L?8080 端口

sudo docker run -d -p 8080:80 --name static_web zhiyewang/static_web nginx -g "daemon off;"

容器的 80 端口l定到本地宿L?127.0.0.1 q个 IP ?80 端口

sudo docker run -d -p 127.0.0.1:80:80 --name static_web zhiyewang/static_web nginx -g "daemon off;"

容器的 80 端口l定到本地宿L?127.0.0.1 q个 IP 的随机端?/p>

sudo docker run -d -p 127.0.0.1::80 --name static_web zhiyewang/static_web nginx -g "daemon off;"

对外公开端口Q此命o可以容器内?80 端口Ҏ地宿L公开Qƈ且绑定要宿主机的一?/p>

随机端口。此命o同时也会?Dockerfile 文g?EXPOSE 指o指定的其他端口一q公开?/p>

sudo docker run -d -p --name static_web zhiyewang/static_web nginx -g "daemon off;"

q样我们可以用本地宿L?IP 地址或?127.0.0.1 ?localhost 来连接到q行的容器,

查看 Web 服务器的内容了?/p>

[#33#cloudsoar@cloudsoar-virtual-machine ~]$curl localhost:32768

Hi, I am in your container

4.5.10 Dockerfile 指o

1 CMD 指o

CMD 指o用于指定一个容器启动时候需要运行的指o。有点类g RUN 指oQ区别是 RUN 

指o是指定镜像被构徏时候运行的指oQ?CMD 是容器被启动时运行的指o?/p>

命o行启动容器的 /bin/true

[#34#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker run -i -t zhiyewang/static_web /bin/true

可以使用 CMD 写在 Dockerfile 中:

CMD ["/bin/true"]

也可以ؓ要运行的命o指定参数

CMD ["/bin/bash", "-l"]

需要注意的?docker run 命o可以覆盖 CMD 指o。如?dockerrun 中指定了命oQ而CM

 D 中也指定了相同的命oQ命令行中的指o会覆?Dockerfile 中的 CMD 指o?/p>

假设我们?Dockerfile 中有如下命o

CMD [ "/bin/bash" ]

使用 docker build 命o构徏一个新镜像Q假设ؓ zhiyewang/testQƈZ此镜像启动一个新

容器?/p>

[#35#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker run -t -i zhiyewang/test

root@7ec0a03d41fc:/# 

可以看到 docker run 命o的末ƈ没有指定 /bin/bash 指oQ却q入了容器的 bash。实际上

Docker 使用?CMD 中指定的命o?/p>

但是 Dockerfile 中只能指定一?CMD 指o。如果制定多个,只有最后一个会被调用?/p>

2 ENTRYPOINT

ENTRYPONIT 指o提供的命令不会再容器启动时被命o行覆盖?/p>

ENTRYPONIT ["/usr/sbin/nginx"]

?ENTRYPOINT 指定参数

ENTRYPONIT ["/usr/sbin/nginx", "-g", "daemon off;"]

如果需?也可以在q行旉过 docker run ?--entrypoint 标志覆盖 ENTRYPOINT 指o?/p>

3 WORKDIR

WORKDIR /opt/webapp/db

RUN bundle install

WORKDIR /opt/webapp

ENTRYPOINT [ "rackup" ]

把目录切换到 /opt/webapp/db 指定?bundle install 命oQ然后又工作目录切换ؓ /opt

 /webapp 最后这只了 ENTRYPOINT 指o来启?rackup 命o?/p>

4 ENV

ENV 可以用来在镜像构E中讄环境变量

ENV RMV_PATH /home/rvm/

q个环境变量讄后在后箋的Q?RUN 中都可以使用

也可以在其他指o中直接用这些环境变?nbsp;

RVM_PATH=/home/rvm/ gem install unicorn

ENV 创徏的环境变量也会被持久保存C我们的镜像创建的M容器中。比如在容器中运?/p>

env 查看Q?/p>

root@7ec0a03d41fc:/# env

... 

RVM_PATH=/home/rvm/

q行时环境变?/p>

sudo docker run -ti -e  "WEB_PORT=8080" ubuntu env

可以讲容器的 WEB_PORT 环境变量讄?8080

5 USER

user 指o用来指定该镜像会以什么样的用户去执行

USER nginx

我们可以指定用户名或?UID 以及l或?GIDQ甚x两者的l合。也可以?docker run ?/p>

令中通过 -u 选项来覆盖该指o的倹{如果不通过 USER 指定特定用户Q默认是 root ?/p>

6 VOLUME

VOLUME ["/opt/project"]

q条指o会为基于此镜像创徏的Q何容器创Z个名?/opt/projiect 的挂载点。也可以通过

数组的方式指定多个卷?/p>

VOLUME ["/opt/project", "/data"]

7 ADD

ADD 命o用来构建环境下的文件和目录复制到镜像中。也可以指定URL。Docker 通过目的?/p>

址的参数末字符来判断文件源是目录还是文件。如果目的地址?/ l尾QDocker 认ؓ是一?/p>

目录Q如果不是的话,认ؓ是文件?/p>

ADD http://wordpress.org/latest.zip /root/wordpress.zip

ADD 在处理本地的归档文gQ包?gzipQbzip2QxvQ指定ؓ源文件时候,会自动将归档解压?/p>

ADD latest.tar.gz /var/www/wordpress/

8 COPY

COPY ?ADD 的本质区别是 COPY 只关心在构徏上下文中复制本地文gQ而不会去做文件提

取和解压的工作。COPY 的文件源路径必须是一个与当前构徏环境相对的文件或目录Q本地文

仉攑ֈ?Dockerfile 同一个目录下。不能复制该目录之外的Q何文件。目的ؓ止必L容器

内部的一个绝对\径。该指o创徏的文件或者目录的 UID ?GID 都会被设|ؓ 0 ?/p>

COPY conf.d/ /etc/apache2/

9 ONBUILD

 

4.6 镜像退送到 Docker Hub

[#37#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker push zhiyewang/static_web

The push refers to a repository [docker.io/zhiyewang/static_web] (len: 1)

e97eb7ef0136: Pushed 

6a7a53f6e78a: Pushed 

ddc8935b098a: Pushed 

40fa5cd1c3d2: Pushed 

c5aed3a8ff95: Pushed 

0b427fcc4cbb: Pushed 

9d89fd8f8a3e: Pushed 

073de23ee32b: Pushed 

latest: digest: sha256:152eb2d70e0f795fbe1b8f8c9eea09e7832a8b01e953cc051cd07832732da0ed size: 14731

现在可以?Docker Hub 上看到我们的镜像了?/p>

 

自动构徏

 

4.7 删除镜像

如果不需要一个镜像了 可以使用 docker rmi 来删?/p>

[#41#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker rmi zhiyewang/static_web

Error response from daemon: conflict: unable to remove repository reference "zhiyewang/static_web" (must force) - container 7ec0a03d41fc is using its referenced image e97eb7ef0136

Error: failed to remove images: [zhiyewang/static_web]

可以看到q个镜像被一个容?nbsp;7ec0a03d41fc 使用着Q首先删除掉容器卛_?nbsp;

[#53#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker rm 7ec0a03d41fc

7ec0a03d41fc

[#62#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker rmi e97eb7ef0136

Deleted: e97eb7ef013619e503bd729596a06e46ee85786619d95950e54f5a74c6fc2694

Deleted: 6a7a53f6e78a802ea932a5914e63d217acf4a47ddbedb80dab042d55297573a5

Deleted: ddc8935b098a0b449a3335286b2b0e555b3d44bd5d92dea305c57f6f7c846fae

Deleted: 40fa5cd1c3d2ae9c80763e9b787c5c9b4848a34164fc2138d2c160830505466d

Deleted: c5aed3a8ff9508b42644cef59c2b44c249628c54130fa1a030f3f2b299124ecc

q里删除的是本地的镜像。每一?Deleted: 行都代表一个镜像层被删除?/p>

4.8 q行自己?Docker Registry

q个可以自己试试了?/p>

 

到此为止Q第四章学习完毕?/p>

下一学习在试中?DockerQ用Docker 试一个静态网站,?Docker 创徏q测试一?/p>

WEB 应用Q用 Docker 用于持箋集成



zhiye_wang 2016-03-17 11:01 发表评论
]]>
W四?使用Docker镜像和仓?/title><link>http://www.shnenglu.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.shnenglu.com/zhiyewang/archive/2016/03/17/213051.html</guid><wfw:comment>http://www.shnenglu.com/zhiyewang/comments/213051.html</wfw:comment><comments>http://www.shnenglu.com/zhiyewang/archive/2016/03/17/213051.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/zhiyewang/comments/commentRss/213051.html</wfw:commentRss><trackback:ping>http://www.shnenglu.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>W??使用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;">回顾Q?/p><p style="margin-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:/# </p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">q条命o会启动一个新的名?another_container_mum 的容器,q个容器Zubuntu?/p><p style="margin-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 基础镜像Qpaulcos11/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 q一栏的旉怎么不准?/p><p style="margin-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公司q营的公?Registry 服务Q即?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;">每个镜像库都可以存放很多镜像Q例如我们查看一下ubuntu仓库中的其他镜像Q?/p><p style="margin-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;"> </p><p style="margin-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 中有两种cd仓库Q用户仓库和层仓库。用户仓库的镜像都是有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内部的h来管理的。用户仓库的命名q户名和仓库名?/p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">部分l成Q如Qpaulcos11/docker-tutorialQ用户名Qpaulcos11Q仓库名Qdocker-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;">ialQ与其相对的层仓库只包含仓库名部分Q例?ubuntuQfedora。顶层仓库由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宿主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;">到某个镜像的内容Q例?fedoraQ可以通过?docker images 命o后面跟指定的镜像名来?/p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">玎ͼ例如Q?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 </p><p style="margin-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 [===================>                               ] 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;">我本地的镜镜像有Q?/p><p style="margin-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: </p><p style="margin-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                         TAG                 IMAGE ID               CREATED             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   latest              e37931352714        8 days ago          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                                  latest              8ed581e3fa7a        11 days ago         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 镜像q是没有下蝲完毕的。不着急。我们先l习其他的操作,?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 命o来从 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;">[#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;">可以看到Q已l从 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;"><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 镜像有以下两U方法:</p><p style="margin-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 命o?/p><p style="margin-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 命o?Dockerfile 文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;"><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 创徏一个自q账号Q注册之后通过收到的确认邮件激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;">试刚才注册的账号是否可以工作了。要d?docker hubQ可以?docker login 命o。如</p><p style="margin-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;">q里看到我是注册成功了,用户名只能是字母或者数l的l合。下面?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;">我的账号Q?/p><p style="margin-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: 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: </p><p style="margin-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: 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;">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;">可以看到我的账号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;"><strong>4.5.2 使用 Docker ?commit 命o创徏镜像</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;">q里我基于前面下载的 ubuntu 镜像来创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;">首先我在q个基础镜像中启动一个容?/p><p style="margin-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:/# </p><p style="margin-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 服务器来q行。这hơ?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:/# 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       </p><p style="margin-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:/# 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;">我喜Ƣ用vimQ同时又安装了一?vimQ哇咔咔?/p><p style="margin-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完成此项工作Q需要先退?exit Q然后执?docker commit 命o?/p><p style="margin-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;">q里需要注意的是, 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;">的部分,q得该更新非常轻量。这里可以看到我创徏的结果?/p><p style="margin-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 命o</p><p style="margin-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:/# </p><p style="margin-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;">文g?docker build 命o来构建镜像?/p><p style="margin-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;">现在我们创徏一个目录,q在里面创徏初始?DockerfileQ我们将创徏一个包含简?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 />q里我们创徏了一个名?static_web 的目录用来保?DockerfileQ这个目录就是我们的构徏?/p><p style="margin-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 environmentQ,Docker 则称此环境ؓ上下文(contextQ或者构Z下文Qbuild</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;"> contextQ。Docker 会在构徏镜像时候,构Z下文和该上下文中的文件和目录上传?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;"> ker 守护q程。这?Docker 守护q程p直接讉K你想在镜像中存储的Q何代码?/p><p style="margin-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;">  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;">  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;">  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;">  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;">  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;">  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;">  7     >/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;">  8 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;">命o解释Q?/p><p style="margin-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 ׃pd指o和参数组成。每条指令都是大写,而且后面需要跟一个参数。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;"> 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 支持注释Q所有以井号开头的都是注释?/p><p style="margin-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 作ؓ基础镜像Q每</p><p style="margin-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攏V自动会再指定类?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;">层,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;">MAINTAINER 指o会告?Docker 该镜像的作者是谁,以及作者的电子邮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;">接下来我们执行了三条 RUN 指oQRUN 指o会在当前镜像中运行指定的命o。我们通过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;">令更C APT 仓库Q安装了 nginx 包,之后创徏? /usr/share/nginx/html/index.html 文gQ?/p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">默认情况QRUN执行会在 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 指oQ告?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 q不会自动打开此端口,而是需要再使用 docker run q行容器时候指定需要打开那些?/p><p style="margin-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 Z 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;">q行 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;">[#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;"> ---> 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;"> ---> 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;"> ---> 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;"> ---> 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'  >/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;"> ---> 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;"> ---> 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;"> ---> 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;"> ---> 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;"> </p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">q里使用 -t 参数为新镜像讄了仓库和名称。仓库ؓ zhiyewangQ名UCؓ static_webQ也?/p><p style="margin-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中为镜像添加一个标{,Ҏ?#8220;镜像名:标签”<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;">如果没有定制M标签QDocker 会自动ؓ镜像讄一?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;">上面命o最后的 . 告诉我们d前\径去?Dockerfile 文g。也可以指定一?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;"> 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;">q里假设的在 Git 仓库的目录下存在 Dockerfile 文g。我也没有注?Git 账号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 build q程发现Q构Z下文已经上传C Docker 守护q程Q?/p><p style="margin-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;">之后Q可以看?Dockerfile 中的每条明o都被序执行Q而且构徏q程的最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;">镜像?IDQ即 e97eb7ef0136 Qƈ?Docker 会提交每一步的执行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;"><strong>4.5.4 指op|时候会怎样</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;">例如我们上面的W?4 步的包名 nginx 写成 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: </p><p style="margin-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;"> ---> 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;"> ---> 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;"> ---> 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;"> ---> 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;"> ---> 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;"> ---> 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 明o来基于这ơ构建到目前为止已经</p><p style="margin-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个容器,它的 ID ?nbsp;40fa5cd1c3d2 Q如下代码:</p><p style="margin-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;">q时我们在此容器中运行第 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:/# 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       </p><p style="margin-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;">我们可以在这个容器中再次q行 apt-get install -y nginxQ这ơ输入正的包名Q来定位问题</p><p style="margin-top: 10px; margin-bottom: 10px; font-family: Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px; background-color: #e0dfe3;">Q如果一旦解决了q个问题Q就可以退出容器,用正的包名修改 Dockerfile 文gQ之后再?/p><p style="margin-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;">q一到q里。下一l学习Dockerfile 和构建缓存?/p><img src ="http://www.shnenglu.com/zhiyewang/aggbug/213051.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/zhiyewang/" target="_blank">zhiye_wang</a> 2016-03-17 11:00 <a href="http://www.shnenglu.com/zhiyewang/archive/2016/03/17/213051.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>W三?Docker 入门http://www.shnenglu.com/zhiyewang/archive/2016/03/15/213023.htmlzhiye_wangzhiye_wangTue, 15 Mar 2016 05:43:00 GMThttp://www.shnenglu.com/zhiyewang/archive/2016/03/15/213023.htmlhttp://www.shnenglu.com/zhiyewang/comments/213023.htmlhttp://www.shnenglu.com/zhiyewang/archive/2016/03/15/213023.html#Feedback0http://www.shnenglu.com/zhiyewang/comments/commentRss/213023.htmlhttp://www.shnenglu.com/zhiyewang/services/trackbacks/213023.htmlW三?docker 入门

3.1 保docker已经qA

首先查看dockerE序是否存在Q功能是否正?/span>

[#3#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker info

[sudo] password for cloudsoar:

Containers: 11

Images: 16

Server Version: 1.9.1

Storage Driver: aufs

 Root Dir: /var/lib/docker/aufs

 Backing Filesystem: extfs

 Dirs: 38


3.2 q行我们的第一个容?/span>

docker run 命o提供了Docker容器的创建到启动的功?/span>

书上使用此命令创建第一个容?/span>

[#4#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker run -i -t ubuntu /bin/bash 

 Unable to find image 'ubuntu:latest' locally

latest: Pulling from library/ubuntu

073de23ee32b: Downloading 48.09 MB/65.69 MB

...

可以看到有大?6MBQ等待慢慢下载完成。其实Ubuntu镜像也是一个基镜像?/span>

sudo docker run -i -t ubuntu /bin/bash 

q句命o我的理解是Qsudo 临时切换理员权限,使用 docker run 命oQ启动镜?/span>

ubuntu 中的 bash?/span>docker run 会检查本地是否有ubuntuq个基础镜像Q如果没有,

则去docker hub registry Lƈ下蝲此镜像,下蝲完毕再执行?/span>

׃容器有自qipQ自qshellQ是独立的。因此加?-i 参数来打开容器的标准输?/span>

STDINQ?-t l容器配一个交互式l端。当新容器下载完毕后Q会自动以root用户?/span>

录到新的容器中。界面显C的是:root@容器ID 。容器的L名就是容器的ID?/span>

假设说我们想要给q个容器安装一个curlQ或者是 pingQ可以通过如下命o

sudo docker run ubuntu apt-get install -y curl

docker run 容器?命o 


{了半个时Q还没下载完毕,׃q个基础镜像下蝲实在太慢Q我换了一个镜?/span>

我之前pull了一个名为paulcos11/docker-tutorial的镜像,容器?1?/span>

如果你想下蝲其他的镜像,可以使用 docker search tutorial 首先查看可用镜像。然后?/p>

docker pull 把它拉下来?/p>

root@cloudsoar-virtual-machine:/home/cloudsoar# docker search tutorial

NAME                                 DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED

georgeyord/reactjs-tutorial          This is the backend of the React comment b...   2                    [OK]

mhausenblas/kairosdb-tutorial        GitHub fetcher for KairosDB tutorial            1                    [OK]

odk211/spree-tutorial                                                                1                    [OK]

paulcos11/docker-tutorial            docker tutorial       


root@cloudsoar-virtual-machine:/home/cloudsoar# docker pull paulcos11/docker-tutorial

Using default tag: latest

latest: Pulling from paulcos11/docker-tutorial

044ffdf80f70: Pull complete 

...

Digest: sha256:8effcf1f4eac7096ba4eaf4a90261580657605d159946372c12ae28b7e5e74f1

Status: Downloaded newer image for paulcos11/docker-tutorial:latest

到这里,我成功下载了容器镜像paulcos11/docker-tutorial


l箋程Q进入我们的容器

root@cloudsoar-virtual-machine:~# docker run -i -t paulcos11/docker-tutorial /bin/bash

root@537739299f24:/# 

执行完毕可以看到q入了容器的bashQ用hrootQ容器ID?span style="color: #ff0000;">537739299f24Q?/span>


3.3 使用容器

可以使用 hostname 查看容器的主机名

root@537739299f24:/# hostname

537739299f24

可以看到容器的ID是容器的主机名?br />下一步看?/etc/hosts 文g

root@537739299f24:/# cat /etc/hosts

172.17.0.2 537739299f24

127.0.0.1 localhost

::1 localhost ip6-localhost ip6-loopback

fe00::0 ip6-localnet

ff00::0 ip6-mcastprefix

ff02::1 ip6-allnodes

ff02::2 ip6-allrouters

可以看到docker 已经在host文g中ؓ该容器的ijp增加了一条主机配|项

再看看IP

root@537739299f24:/# ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

       valid_lft forever preferred_lft forever

    inet6 ::1/128 scope host 

       valid_lft forever preferred_lft forever

30: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 

    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff

    inet 172.17.0.2/16 scope global eth0

       valid_lft forever preferred_lft forever

    inet6 fe80::42:acff:fe11:2/64 scope link 

       valid_lft forever preferred_lft forever

可以看到q里有lo的回环接口。还有ip?72.17.0.2的标准eth0接口Q和宿主机完全一栗?/span>

查看容器 中的q程

root@537739299f24:/# ps -aux

USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND

root          1  0.2  0.1  21944  3528 ?        Ss   03:03   0:01 /bin/bash

root         12  0.0  0.0  19188  2336 ?        R+   03:14   0:00 ps -aux

接下来,我想l容器安装一个vim

q里我先更新下容器的源再安装?br />

root@537739299f24:/# apt-get update && apt-get install vim

安装完毕使用 exitQ即可退出到宿主机系l。这时容器也会停止,因ؓ一旦退出容器,

容器也随之停止了q行。但是容器仍然存在,可以?docker ps -a 查看容器列表?/span>

root@537739299f24:/# exit

exit

docker ps 可以查看q行中的容器Q加?-a 参数可以查看所有的容器

3.4 容器命名

可以使用如下命o为容器指定一个合法有意义的名字?/p>

root@cloudsoar-virtual-machine:~# docker run --name wzy_the_container -i -t paulcos11/docker-tutorial /bin/bash

root@0a09dfd688ea:/# exit

exit


3.5 重新启动已经停止的容?/span>

此时Qwzy_the_container 已经停止了。可以用如下命令重启一个已l停止的容器?br />

root@cloudsoar-virtual-machine:~# docker start wzy_the_container

wzy_the_container

也可以用容器ID替换容器名?/span>


3.6 附着到容器上
容器启动的时候会按照docker run指定的参数来q行。因此这个容器启动后会启动一个交?/p>

的shell。我可以使用 docker attach 直接附着到此容器?/p>

root@cloudsoar-virtual-machine:~# docker attach wzy_the_container

root@0a09dfd688ea:/# 

可以看到重新会到了容器的Bash提示W界面?br />

如果退出容器的shellQ容器也会随之停止运行?/p>


3.7 创徏守护式容?/strong>

除了交互式运行的容器Q我们也可以创徏守护容器。可以长期的q行应用E序或者服务?/p>

大多时候需要创建守护式容器?/p>

[#10#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker run --name daemon_dave -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"

cf5636f0499fa0105b845b5dfa61913ff7d5fccfb1b19c16b09ed2752233794d

׃此时W?3.2 步我下蝲?Ubuntu 基础镜像Downloading 48.09 MB/65.69 MB 刚好下蝲

完毕Q所以此时我开始用ubuntu容器镜像操作了?/span>

docker run 的参?-d 是放到后台执行。此时我们ƈ没有像上一个容器直接附着到新容器?/span>

shell会话上,而是q回了一个容器ID而已。此时?docker ps 可以看到正在q行的容器?/span>


ubuntu是我刚刚下载完毕的基础镜像?paulcos11/docker-tutorial 是我自己下蝲的其他的镜像?br />

3.8 容器内部再搞什?/strong>

使用 docker logs 获取容器日志

[#16#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker logs daemon_dave

hello world

hello world

hello world

hello world

hello world

hello world

或者可以加?f 实时监控容器日志?/span>

sudo docker logs -f daemon_dave

也可以追t日志某一片段Q只需要tail命o加入 -f --lines标志卛_?/p>

例如Q?docker logs --tail 10 daemon_dave 获取最?0行日志即可?/p>

 docker logs --tail 0 -f daemon_dave 获取最新日志?/p>

如果要查看日志的旉Q可以加上时间戳?/p>

[#18#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker logs -ft daemon_dave

2016-03-15T03:45:28.457991514Z hello world

2016-03-15T03:45:29.472422582Z hello world

2016-03-15T03:45:30.475130491Z hello world


3.9 查看容器q程

查看容器内的q程可以使用 docker top 命o?br />

[#20#cloudsoar@cloudsoar-virtual-machine ~]$sudo docker top daemon_dave

UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD

root                25381               22325               0                   11:45               ?                   00:00:00            /bin/sh -c while true; do echo hello world; sleep 1; done


3.10 在容器内部运行进E?/span>

我们可以使用docker exec 命o在容器内部额外启动新q程Q可以运行的q程有两U类型,

交互式Q务和后台d。首先我们?touch 命o创徏一个空文g?/span>

$sudo docker exec -d daemon_dave touch /etc/new_config_file

-d 表示是后台进E?/span>

或者打开一个shell

$sudo docker exec -t -i daemon_dave /bin/bash

root@cf5636f0499f:/#


3.11 停止守护式容?/span>

$sudo docker stop daemon_dave

daemon_dave


3.12 自动重启容器

如果׃某种错误D容器停止Q可以通过 --restart 标志让docker自动重新启动容器?/span>

$sudo docker run --restart=on-failure:5 --name daemon_dave -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"

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.

q里我的q行不v来,因ؓ -- restart 标示是docker 2.0 引入的。我的是docker 1.9版本?/p>


3.13 深入容器

除了使用docker ps 查看容器信息Q还可以使用 docker inspect 查看更多信息


$sudo docker inspect daemon_dave

[sudo] password for cloudsoar:

[

{

    "Id": "cf5636f0499fa0105b845b5dfa61913ff7d5fccfb1b19c16b09ed2752233794d",

    "Created": "2016-03-15T03:45:28.124542139Z",

    "Path": "/bin/sh",

    "Args": [

        "-c",

        "while true; do echo hello world; sleep 1; done"

    ],

    "State": {

        "Status": "exited",

        "Running": false,

        "Paused": false,

        "Restarting": false,

        "OOMKilled": false,

        "Dead": false,

        "Pid": 0,

        "ExitCode": 137,

        "Error": "",

        "StartedAt": "2016-03-15T03:45:28.455126857Z",

        "FinishedAt": "2016-03-15T04:05:11.945521026Z"

    },

   ...

也可使用 -f 或?--format 标志来选定查看l果?/p>

$sudo docker inspect --format='{{ .State.Running}}' daemon_dave

false

以及查看其他信息

$sudo docker start daemon_dave

daemon_dave

$sudo docker inspect --format '{{ .NetworkSettings.IPAddress }}' daemon_dave

172.17.0.3


3.14 删除容器

如果容器不在使用Q可以?docker rm 命o删除?/span>

q里假设我要删除 daemon_dave q个容器Q他的容器ID?nbsp;cf5636f0499f

$sudo docker rm cf5636f0499f

Error response from daemon: Conflict, You cannot remove a running container. Stop the container before attempting removal or use -f

Error: failed to remove containers: [cf5636f0499f]

$sudo docker stop cf5636f0499f

cf5636f0499f

$sudo docker rm cf5636f0499f

cf5636f0499f

׃不能删除正在q行的容器,所以需要先停止?/p>


3.15 结

下一章学习如何构qDocker镜像Q以及如何用Docker 仓库?Docker Registry.



zhiye_wang 2016-03-15 13:43 发表评论
]]>
97þ㽶߿ۿ| 99þþƷ⿴һ| ݺɫþۺ| þùƷ| þù˾Ʒ| 91Ʒþþþþ| þþþAVվ| һһþaaۺϾƷ| ޾Ʒþþþȥq| þþƷþһ| ޹һɾþþƷۺ | þþþó˾ƷĻ| ޾Ʒþþþϼ| þþþòҰ¸߳| Ʒպþ| һþۺ³³ŷһ | Aݺݾþɫ| Ʒþþþ㽶| þþþþþۺձ| þþþӰԺŮ | ŷպƷþþѹۿ| ŷƷһþ| þۺϹɫ88þþƷۺ | ձƷþ| þAV뾫Ʒɫҹ| ɫۺϾþþĻ| þþƷƷapp| ݺ޾þþþþۺ| vvaþ| vavavaþ| þrоƷƵ| þþþþAŷAV | 99þ99ֻѷѾƷ| ŷþһ| þþþþüƷþþ | þþƷŷպƷ| þҹɫtvվ| þֻǾƷ66| þùƷþùƷ| ޹һɾþþƷۺ| ŷAŷaþ|