youli3 发表于 2015-10-13 12:18:10

Hello Docker入门实例

  1.什么是docker
  一个分布式应用的开放平台,相比VM更加轻量级,可以不经任何更改运行在任意平台,任意终端
  2与VM的比较
  








  3. Docker 构成组件
  


[*]Docker image:   一个只读的template 文件,很多base的template可以从hub获取。一个image可以包含一个基本的ubuntu 操作系统和你的web appliation
[*]Docker registry : 管理template文件的仓库,可以pull(获取),修改后,push template文件进行update
[*]Docker container: 从template launch的运行容器,类似于目录,包含application运行需要的一切东西,可以被启动,停止,运行,删除,移动等操作


  
  4. Docker 安装
  在CentOS 7 上安装Docker,如下所示:
  

$ sudo yuminstall docker
password for plcm:
Loaded plugins: fastestmirror
WandiscoSVN-CentOS6                                    |951 B   00:00   
base                                                   | 3.6 kB   00:00   
extras                                                   | 3.4 kB   00:00   
google-chrome                                          |951 B   00:00   
updates                                                | 3.4 kB   00:00   
zulu                                                   | 2.9 kB   00:00   
Loading mirror speeds from cached hostfile
* base: mirrors.neusoft.edu.cn
* epel: mirrors.vinahost.vn
* extras: mirrors.neusoft.edu.cn
* updates: mirror.neu.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package docker.x86_64 0:1.3.2-4.el7.centos will be installed
--> Processing Dependency: systemd-units >= 208-11.el7_0.5 for package: docker-1.3.2-4.el7.centos.x86_64
--> Running transaction check
---> Package systemd.x86_64 0:208-11.el7_0.2 will be updated
--> Processing Dependency: systemd = 208-11.el7_0.2 for package: systemd-python-208-11.el7_0.2.x86_64
--> Processing Dependency: systemd = 208-11.el7_0.2 for package: systemd-sysv-208-11.el7_0.2.x86_64
--> Processing Dependency: systemd = 208-11.el7_0.2 for package: libgudev1-208-11.el7_0.2.x86_64
---> Package systemd.x86_64 0:208-11.el7_0.6 will be an update
--> Processing Dependency: systemd-libs = 208-11.el7_0.6 for package: systemd-208-11.el7_0.6.x86_64
--> Running transaction check
---> Package libgudev1.x86_64 0:208-11.el7_0.2 will be updated
---> Package libgudev1.x86_64 0:208-11.el7_0.6 will be an update
---> Package systemd-libs.x86_64 0:208-11.el7_0.2 will be updated
---> Package systemd-libs.x86_64 0:208-11.el7_0.6 will be an update
---> Package systemd-python.x86_64 0:208-11.el7_0.2 will be updated
---> Package systemd-python.x86_64 0:208-11.el7_0.6 will be an update
---> Package systemd-sysv.x86_64 0:208-11.el7_0.2 will be updated
---> Package systemd-sysv.x86_64 0:208-11.el7_0.6 will be an update
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package            Arch         Version                  Repository   Size
================================================================================
Installing:
docker               x86_64       1.3.2-4.el7.centos       extras      5.6 M
Updating for dependencies:
libgudev1            x86_64       208-11.el7_0.6         updates      48 k
systemd            x86_64       208-11.el7_0.6         updates       2.6 M
systemd-libs         x86_64       208-11.el7_0.6         updates       154 k
systemd-python       x86_64       208-11.el7_0.6         updates      83 k
systemd-sysv         x86_64       208-11.el7_0.6         updates      36 k
Transaction Summary
================================================================================
Install1 Package
Upgrade             ( 5 Dependent packages)
Total size: 8.5 M
Total download size: 5.6 M
Is this ok : y
Downloading packages:
docker-1.3.2-4.el7.centos.x86_64.rpm                55% [======================-                  ]39 kB/s | 3.1 MB00:01:06 ETA docker-1.3.2-4.el7.centos.x86_64.rpm                55% [======================-docker-1.3.2-4.el7.centos.x86_64.rpm                                                                         | 5.6 MB00:02:31   
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Updating   : systemd-libs-208-11.el7_0.6.x86_64                                                                              1/11
Updating   : systemd-208-11.el7_0.6.x86_64                                                                                 2/11
Updating   : systemd-sysv-208-11.el7_0.6.x86_64                                                                              3/11
Updating   : systemd-python-208-11.el7_0.6.x86_64                                                                            4/11
Installing : docker-1.3.2-4.el7.centos.x86_64                                                                              5/11
Updating   : libgudev1-208-11.el7_0.6.x86_64                                                                                 6/11
Cleanup    : systemd-sysv-208-11.el7_0.2.x86_64                                                                              7/11
Cleanup    : libgudev1-208-11.el7_0.2.x86_64                                                                                 8/11
Cleanup    : systemd-python-208-11.el7_0.2.x86_64                                                                            9/11
Cleanup    : systemd-208-11.el7_0.2.x86_64                                                                                  10/11
Cleanup    : systemd-libs-208-11.el7_0.2.x86_64                                                                           11/11
Verifying: systemd-sysv-208-11.el7_0.6.x86_64                                                                              1/11
Verifying: systemd-python-208-11.el7_0.6.x86_64                                                                            2/11
Verifying: docker-1.3.2-4.el7.centos.x86_64                                                                              3/11
Verifying: libgudev1-208-11.el7_0.6.x86_64                                                                                 4/11
Verifying: systemd-libs-208-11.el7_0.6.x86_64                                                                              5/11
Verifying: systemd-208-11.el7_0.6.x86_64                                                                                 6/11
Verifying: systemd-208-11.el7_0.2.x86_64                                                                                 7/11
Verifying: systemd-sysv-208-11.el7_0.2.x86_64                                                                              8/11
Verifying: systemd-python-208-11.el7_0.2.x86_64                                                                            9/11
Verifying: libgudev1-208-11.el7_0.2.x86_64                                                                              10/11
Verifying: systemd-libs-208-11.el7_0.2.x86_64                                                                           11/11
Installed:
docker.x86_64 0:1.3.2-4.el7.centos                                                                                                
Dependency Updated:
libgudev1.x86_64 0:208-11.el7_0.6            systemd.x86_64 0:208-11.el7_0.6            systemd-libs.x86_64 0:208-11.el7_0.6      
systemd-python.x86_64 0:208-11.el7_0.6       systemd-sysv.x86_64 0:208-11.el7_0.6      
Complete!

$ cat /etc/redhat-release

CentOS Linux release 7.0.1406 (Core)
  
  


  启动 docker daemon
  

$ sudoservice docker start
password for plcm:
Redirecting to /bin/systemctl startdocker.service
$ ps-ef |grep docker
root   20011   12 00:28 ?      00:00:00 /usr/bin/docker -d --selinux-enabled -H fd://
plcm   20108 195660 00:28 pts/1    00:00:00 grep --color=auto docker

为了测试docker运行正常,我们download一个最新的centos image 试试,如下命令:  
  

$ sudo docker pull centos
Pulling repository centos
8efe422e6104: Pulling image (latest) from centos, endpoint: https://registry-1.d8efe422e6104: Download complete
511136ea3c5a: Download complete
5b12ef8fd570: Download complete
Status: Downloaded newer image for centos:latest
$ sudo docker images centos
REPOSITORY          TAG               IMAGE ID            CREATED             VIRTUAL SIZE
centos            centos7             8efe422e6104      13 days ago         224 MB
centos            latest            8efe422e6104      13 days ago         224 MB
centos            7                   8efe422e6104      13 days ago   
</pre>运行bash shell 验证是否work,执行成功后,我们进入了一个交互性的container:<p></p><p></p><pre code_snippet_id=&quot;583469&quot; snippet_file_name=&quot;blog_20150119_6_6989701&quot; name=&quot;code&quot; class=&quot;plain&quot;>$sudo docker run -i -t centos /bin/bash
# </pre><pre code_snippet_id=&quot;583469&quot; snippet_file_name=&quot;blog_20150119_8_1145350&quot; name=&quot;code&quot; class=&quot;plain&quot;># df
Filesystem                                                                                       1K-blocks    Used Available Use% Mounted on
/dev/mapper/docker-253:1-522936-b71bd0592d6006b4592c63d0ecded912b83135e9833e75fc656ac475ab06bb3710190100265636   9383792   3% /
tmpfs                                                                                              1941740       0   1941740   0% /dev
shm                                                                                                65536       0   65536   0% /dev/shm
/dev/mapper/centos-root                                                                           17938820 9497724   750680056% /etc/hosts
tmpfs                                                                                              1941740       0   1941740   0% /run/secrets
tmpfs                                                                                              1941740       0   1941740   0% /proc/kcore


# echo &quot;hellodocker&quot;

hellodocker
  
  一旦我们输入exit后者ctrl&#43;z终止后,container也将停止。如下图,我们通过dockerps 命令查询运行的container,一旦exit退出后,container也会停止运行
  

$ sudodocker ps
CONTAINER ID      IMAGE               COMMAND             CREATED             STATUS            PORTS               NAMES
33303b62503b      centos:7            &quot;/bin/bash&quot;         5 minutes ago       Up 5 minutes                            angry_elion         运行exit命令后,container停止运行,结果为空$ sudodocker ps
CONTAINER ID      IMAGE               COMMAND             CREATED             STATUS            PORTS               NAMES

5.在container上运行一个复杂点的web应用  
  


  

$sudo docker run -d -P training/webapp python app.py
Unable to find image 'training/webapp' locally
Pulling repository training/webapp
31fa814ba25a: Pulling image (latest) from training/webapp, endpoint: https://reg31fa814ba25a: Pulling dependent layers
511136ea3c5a: Download complete
f10ebce2c0e1: Downloading [======================>                            ]17.3 MB/39.17 MB 1m39s这个web应用为docker官方提供,本地没有,所以自动去仓库下载,很方便的实现image,code 共享,并且到处运行。  
  image都是分层的,所以更改后构建比较快速。




  此web 应用成功运行,访问页面如下:http://10.220.209.10:49153/



  查看运行的container:
  

$ sudodockerps
CONTAINER ID      IMAGE                  COMMAND             CREATED             STATUS            PORTS                     NAMES
dac947a0be14      training/webapp:latest   &quot;python app.py&quot;   4 minutes ago       Up 4 minutes      0.0.0.0:49153->5000/tcp   kickass_pasteur </pre><pre code_snippet_id=&quot;583469&quot; snippet_file_name=&quot;blog_20150119_15_3099315&quot; name=&quot;code&quot; class=&quot;html&quot;>$ sudo docker inspectkickass_pasteur
[{
    &quot;AppArmorProfile&quot;: &quot;&quot;,
    &quot;Args&quot;: [
      &quot;app.py&quot;
    ],
    &quot;Config&quot;: {
      &quot;AttachStderr&quot;: false,
      &quot;AttachStdin&quot;: false,
      &quot;AttachStdout&quot;: false,
      &quot;Cmd&quot;: [
            &quot;python&quot;,
            &quot;app.py&quot;
      ],
      &quot;CpuShares&quot;: 0,
      &quot;Cpuset&quot;: &quot;&quot;,
      &quot;Domainname&quot;: &quot;&quot;,
      &quot;Entrypoint&quot;: null,
      &quot;Env&quot;: [
            &quot;HOME=/&quot;,
            &quot;PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin&quot;
      ],
      &quot;ExposedPorts&quot;: {
            &quot;5000/tcp&quot;: {}
      },
      &quot;Hostname&quot;: &quot;dac947a0be14&quot;,
      &quot;Image&quot;: &quot;training/webapp&quot;,
      &quot;Memory&quot;: 0,
      &quot;MemorySwap&quot;: 0,
      &quot;NetworkDisabled&quot;: false,
      &quot;OnBuild&quot;: null,
      &quot;OpenStdin&quot;: false,
      &quot;PortSpecs&quot;: null,
      &quot;StdinOnce&quot;: false,
      &quot;Tty&quot;: false,
      &quot;User&quot;: &quot;&quot;,
      &quot;Volumes&quot;: null,
      &quot;WorkingDir&quot;: &quot;/opt/webapp&quot;
    },
    &quot;Created&quot;: &quot;2015-01-19T06:13:47.740587544Z&quot;,
    &quot;Driver&quot;: &quot;devicemapper&quot;,
    &quot;ExecDriver&quot;: &quot;native-0.2&quot;,
    &quot;HostConfig&quot;: {
      &quot;Binds&quot;: null,
      &quot;CapAdd&quot;: null,
      &quot;CapDrop&quot;: null,
      &quot;ContainerIDFile&quot;: &quot;&quot;,
      &quot;Devices&quot;: [],
      &quot;Dns&quot;: null,
      &quot;DnsSearch&quot;: null,
      &quot;ExtraHosts&quot;: null,
      &quot;Links&quot;: null,
      &quot;LxcConf&quot;: [],
      &quot;NetworkMode&quot;: &quot;bridge&quot;,
      &quot;PortBindings&quot;: {},
      &quot;Privileged&quot;: false,
      &quot;PublishAllPorts&quot;: true,
      &quot;RestartPolicy&quot;: {
            &quot;MaximumRetryCount&quot;: 0,
            &quot;Name&quot;: &quot;&quot;
      },
      &quot;SecurityOpt&quot;: null,
      &quot;VolumesFrom&quot;: null
    },
    &quot;HostnamePath&quot;: &quot;/var/lib/docker/containers/dac947a0be14ff0c1fceb1a5af7c6b82af6f6b5599bdc13ce4f9f7ead5d8665d/hostname&quot;,
    &quot;HostsPath&quot;: &quot;/var/lib/docker/containers/dac947a0be14ff0c1fceb1a5af7c6b82af6f6b5599bdc13ce4f9f7ead5d8665d/hosts&quot;,
    &quot;Id&quot;: &quot;dac947a0be14ff0c1fceb1a5af7c6b82af6f6b5599bdc13ce4f9f7ead5d8665d&quot;,
    &quot;Image&quot;: &quot;31fa814ba25ae3426f8710df7a48d567d4022527ef2c14964bb8bc45e653417c&quot;,
    &quot;MountLabel&quot;: &quot;&quot;,
    &quot;Name&quot;: &quot;/kickass_pasteur&quot;,
    &quot;NetworkSettings&quot;: {
      &quot;Bridge&quot;: &quot;docker0&quot;,
      &quot;Gateway&quot;: &quot;172.17.42.1&quot;,
      &quot;IPAddress&quot;: &quot;172.17.0.5&quot;,
      &quot;IPPrefixLen&quot;: 16,
      &quot;MacAddress&quot;: &quot;02:42:ac:11:00:05&quot;,
      &quot;PortMapping&quot;: null,
      &quot;Ports&quot;: {
            &quot;5000/tcp&quot;: [
                {
                  &quot;HostIp&quot;: &quot;0.0.0.0&quot;,
                  &quot;HostPort&quot;: &quot;49153&quot;
                }
            ]
      }
    },
    &quot;Path&quot;: &quot;python&quot;,
    &quot;ProcessLabel&quot;: &quot;&quot;,
    &quot;ResolvConfPath&quot;: &quot;/var/lib/docker/containers/dac947a0be14ff0c1fceb1a5af7c6b82af6f6b5599bdc13ce4f9f7ead5d8665d/resolv.conf&quot;,
    &quot;State&quot;: {
      &quot;ExitCode&quot;: 0,
      &quot;FinishedAt&quot;: &quot;0001-01-01T00:00:00Z&quot;,
      &quot;Paused&quot;: false,
      &quot;Pid&quot;: 21678,
      &quot;Restarting&quot;: false,
      &quot;Running&quot;: true,
      &quot;StartedAt&quot;: &quot;2015-01-19T06:13:48.540050644Z&quot;
    },
    &quot;Volumes&quot;: {},
    &quot;VolumesRW&quot;: {}
}   

可以利用container名称进行application的启动和停止  
  

$ sudo docker start nostalgic_morse
nostalgic_morse

  
  6. 如何自定义image
  以上我们都是利用hub上已经存在的image进行操作,如何自定义image呢?继续研究。
  


  查询localhost拥有的image,先前建立起来的image:
  

$ sudo docker images
REPOSITORY          TAG               IMAGE ID            CREATED             VIRTUAL SIZE
centos            7                   8efe422e6104      13 days ago         224 MB
centos            centos7             8efe422e6104      13 days ago         224 MB
centos            latest            8efe422e6104      13 days ago         224 MB
training/webapp   latest            31fa814ba25a      7 months ago      278.6 MB

第一种方法,从image创建container后,更新container,commit结果到image  
  第二种方法是利用dockerfile指定指令创建image
  


  先测试第一个方法:
  修改我们之前的centos image,增加一个文件,名字为kerry.log文件
  

# touch kerry.log
# ls
binetc   kerry.loglib64       mediaopt   rootsbin   srvtmpvar
devhomelib      lost+foundmnt    procrun   selinuxsysusr

commit改变到image,此时仍未localimage  
  

$ sudo docker commit-m=&quot;add log file&quot;-a=&quot;kerry&quot; 91f2108f31ee    kerry/centos:v2ef5dbe35d8460043f360a392c32e4fb2e8b8bccfe223241bc47401035c02ff15$ sudo docker images
REPOSITORY          TAG               IMAGE ID            CREATED             VIRTUAL SIZE
kerry/centos      v2                  ef5dbe35d846      54 seconds ago      224 MB
centos            7                   8efe422e6104      13 days ago         224 MB
centos            centos7             8efe422e6104      13 days ago         224 MB
centos            latest            8efe422e6104      13 days ago         224 MB
training/webapp   latest            31fa814ba25a      7 months ago      278.6 MB



利用 sudo dockerpush 更新image到hub  
  

$ sudo docker pushkerry/centos
The push refers to a repository (len: 1)
Sending image list
Please login prior to push:
Username: kerry
Password:
Email: kerry@gmail.com
Account created. Please use the confirmation link we sent to your e-mail to activate it.
The push refers to a repository (len: 1)
Sending image list
2015/01/19 01:45:40 Error: Status 403 trying to push repository kerry/centos: Account is not Active


  7 Container之间可以建立linkskip, 详见:https://docs.docker.com/userguide/dockerlinks/
  8管理container的数据,skip
  详见: https://docs.docker.com/userguide/dockervolumes/
  


  9.docker注册账户,上传,下载image等常用命令
  



版权声明:本文为博主原创文章,未经博主允许不得转载。
页: [1]
查看完整版本: Hello Docker入门实例