Hello Docker入门实例

1.  什么是docker

一个分布式应用的开放平台,相比VM更加轻量级,可以不经任何更改运行在任意平台,任意终端

2  与VM的比较


Hello Docker入门实例_第1张图片


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="583469" snippet_file_name="blog_20150119_6_6989701" name="code" class="plain">[plcm@ngmadev ~]$  sudo docker run -i -t centos /bin/bash
[root@b71bd0592d60 /]# 
</pre><pre code_snippet_id="583469" snippet_file_name="blog_20150119_8_1145350" name="code" class="plain">[root@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

[root@33303b62503b /]# echo "hello  docker"
hello  docker

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

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

可以利用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文件

[root@91f2108f31ee /]# touch kerry.log
[root@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="add log file"  -a="kerry" 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: [email protected]
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等常用命令


你可能感兴趣的:(Hello Docker入门实例)