设为首页 收藏本站
查看: 1413|回复: 0

[经验分享] Hello Docker入门实例

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-10-13 12:18:10 | 显示全部楼层 |阅读模式
  1.  什么是docker
  一个分布式应用的开放平台,相比VM更加轻量级,可以不经任何更改运行在任意平台,任意终端
  2  与VM的比较
  


DSC0000.jpg





  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,如下所示:
  

[plcm@ngmadev ~]$ sudo yum  install docker
[sudo] 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
================================================================================
Install  1 Package
Upgrade             ( 5 Dependent packages)
Total size: 8.5 M
Total download size: 5.6 M
Is this ok [y/d/N]: y
Downloading packages:
docker-1.3.2-4.el7.centos.x86_64.rpm                55% [======================-                  ]  39 kB/s | 3.1 MB  00: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 MB  00: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!

[plcm@ngmadev ~]$ cat /etc/redhat-release

CentOS Linux release 7.0.1406 (Core)
  
  


  启动 docker daemon
  

[plcm@ngmadev ~]$ sudo  service docker start
[sudo] password for plcm:
Redirecting to /bin/systemctl start  docker.service
[plcm@ngmadev ~]$ ps  -ef |grep docker
root     20011     1  2 00:28 ?        00:00:00 /usr/bin/docker -d --selinux-enabled -H fd://
plcm     20108 19566  0 00:28 pts/1    00:00:00 grep --color=auto docker

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

[plcm@ngmadev ~]$ 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
[plcm@ngmadev ~]$ 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;>[plcm@ngmadev ~]$  sudo docker run -i -t centos /bin/bash
[iyunv@b71bd0592d60 /]# </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;>[iyunv@b71bd0592d60 /]# df
Filesystem                                                                                       1K-blocks    Used Available Use% Mounted on
/dev/mapper/docker-253:1-522936-b71bd0592d6006b4592c63d0ecded912b83135e9833e75fc656ac475ab06bb37  10190100  265636   9383792   3% /
tmpfs                                                                                              1941740       0   1941740   0% /dev
shm                                                                                                  65536       0     65536   0% /dev/shm
/dev/mapper/centos-root                                                                           17938820 9497724   7506800  56% /etc/hosts
tmpfs                                                                                              1941740       0   1941740   0% /run/secrets
tmpfs                                                                                              1941740       0   1941740   0% /proc/kcore


[iyunv@33303b62503b /]# echo &quot;hello  docker&quot;

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

[plcm@ngmadev ~]$ sudo  docker 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停止运行,结果为空[plcm@ngmadev ~]$ sudo  docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

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


  

[plcm@ngmadev ~]$  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:
  

[plcm@ngmadev ~]$ sudo  docker  ps
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;>[plcm@ngmadev ~]$ sudo docker inspect  kickass_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:
  

[plcm@ngmadev ~]$ 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文件
  

[iyunv@91f2108f31ee /]# touch kerry.log
[iyunv@91f2108f31ee /]# ls
bin  etc   kerry.log  lib64       media  opt   root  sbin     srv  tmp  var
dev  home  lib        lost+found  mnt    proc  run   selinux  sys  usr

commit改变到image,此时仍未local  image  
  

[plcm@ngmadev ~]$ sudo docker commit  -m=&quot;add log file&quot;  -a=&quot;kerry&quot; 91f2108f31ee    kerry/centos:v2ef5dbe35d8460043f360a392c32e4fb2e8b8bccfe223241bc47401035c02ff15[plcm@ngmadev ~]$ 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 docker  push 更新image到hub  
  

[plcm@ngmadev ~]$ sudo docker push  kerry/centos
The push refers to a repository [kerry/centos] (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 [kerry/centos] (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之间可以建立link  skip, 详见:https://docs.docker.com/userguide/dockerlinks/
  8  管理container的数据,skip
  详见: https://docs.docker.com/userguide/dockervolumes/
  


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



版权声明:本文为博主原创文章,未经博主允许不得转载。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-126267-1-1.html 上篇帖子: Openshift V3简介 下篇帖子: 在Centos 7中使用 Docker搭建MySQL异地双向复制环境
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表