Docker初探(一)-有關docker的介紹和簡單使用

先分享一下構建好的image(源碼部署的Nginx)         https://hub.docker.com/r/xxbandy123/centos/  
php

本地docker image http://pan.baidu.com/s/1dDtux6h  java

爲何要提起docker呢?在當前大數據盛行的環境下,數據的處理量越來越多,如何可以讓應用快速的部署,消耗的資源更少,這些將是互聯網企業須要考慮的問題。隨而就興起了相關雲計算平臺。
 衆所周知的是雲計算平臺無非分爲三個方向,IAAS,PAAS,SAAs,。那麼國內,在SAAS平臺上能夠說已經
作到了。好比QQ,微信,等等包括當前的一些企業應用均可以看作SAAS的應用。在IAAS,和PAAS一直沒有太多的關注,而在中國也沒有太大的發展。
近兩年來,國內IAAS環境作的還算不錯,畢竟做爲基礎設施即服務層,當前的阿里雲,青雲等等國內基礎設施仍是作的比較到位。能夠爲小中型企業提供至關完備的相關基礎設施服務。
而做爲PAAS平臺,最近一輛發展的也比較火,最爲受歡迎的當數容器平臺docker了。
node


那麼什麼是docker呢?docker又能爲咱們帶來什麼呢?mysql

Docker 是一個開源的應用容器引擎,讓開發者能夠打包他們的應用以及依賴包到一個可移植的容器中,而後發佈到任何流行的 Linux 機器上,也能夠實現虛擬化。容器是徹底使用沙箱機制,相互之間不會有任何接口(相似 iPhone 的 app)。幾乎沒有性能開銷,能夠很容易地在機器和數據中心中運行。最重要的是,他們不依賴於任何語言、框架包括系統。
linux

什麼是容器?有哪些現有的容器技術?Docker與其餘容器技術的不一樣是什麼?
從宏觀角度來看,容器更像是輕量化的虛擬機。你能夠在容器中安裝任何你想要的軟件,獨立且不影響其餘容器或者宿主環境。每個容器有它本身的網絡 堆棧、進程空間、文件系統等。同時,他們的影響要遠小於虛擬機:容器啓動更快、佔用更少的內存和硬盤空間。從底層角度來看,這都是由於容器只是宿主機上的 一個進程,利用內核特徵如命名空間和組管理來提供這種隔離。啓動一個容器只是啓動了一個普通的UNIX進程;建立一個容器只是複製了一個copy-on- wirte文件系統的鏡像。
nginx

Docker與其餘容器技術不一樣,由於它不僅是一個容器引擎。Docker是一個平臺,整合了Docker引擎、Docker Hub以及一系列的生態工具,如Docker Compose、Docker Machine、Docker Swarm等。這些都採用開發API。git

Docker用到的技術:web

傳統虛擬化技術和Docker技術:sql

Docker與其餘hypervisor虛擬技術的不一樣之處?
有人說Docker是是一個「容器的hypervisor」,可是許多人並不這麼認爲,這是由於hypervisor一般是管理虛擬機,而 Docker是管理容器。在深層技術細節也是不一樣的。當一個hypervisor啓動虛擬機,它建立虛擬硬件,並利用特定CPU基礎結構和特性,如VT- x,AMD-x或者權限層級。當Docker建立一個容器,它利用的是內核的特性,如命名空間、羣組管理,而不依賴硬件特性。
這意味着容器在某種意義上說更具備可移植性,由於它們能夠同時在物理機或者虛擬機上運行;可是他們從另外某種意義上來講,也更不容易移植,由於容 器必須使用宿主的內核。這意味着,你不能運行一個Windows容器在Linux內核上(除非linux內核能夠執行Windows二進制程序)。docker


Docker的重要組成部分:

Docker image(鏡像):相似於虛擬機鏡像,存放一個虛擬機配置信息的只讀模板文件(json格式),且包含文件系統!鏡像是建立Docker容器的基礎。經過版本管理和增量的文件系統,Docker提供了一套十分簡單的機制來建立和更新現有的鏡像,用戶甚至能夠從網上下載一個已經作好的應用鏡像,並經過簡單的命令就能夠直接使用。

Docker container(容器):相似於一個輕量級的沙箱,Docker利用容器來運行和隔離應用,所以每個容器裏的應用都是相互隔離的,每個容器也都是一個獨立的進程,不可見的,只有宿主機可以管理和查看相關鏡像和容器。容器是從鏡像建立的應用運行實例,能夠將其啓動、開始、中止、刪除。鏡像自身是隻讀的。容器從鏡像啓動的時候,Docker會在鏡像的最上層建立一個可寫層,鏡像自己將保持不變。

Docker registry(註冊中心):相似於代碼倉庫,是Docker集中存放鏡像文件的場所,也就是dockerHub(其實docker在設計的時候就採用git的思路,好比commit,pull,push以及Hub等操做,所以,在使用過git後,很容易理解啦)。

根據所存儲的鏡像公開分享與否,Docker倉庫能夠分爲公開倉庫(Public)和私有倉庫(Private)兩種形式。
目前,最大的公開倉庫是Docker Hub,存放了數量龐大的鏡像供用戶下載。國內的公開倉庫包括Docker Pool等,能夠提供穩定的國內訪問。

固然,本身也能夠在本地建立一個本身的私有倉庫咯!


Docker實戰:

1、Docker的安裝部署:
$ cat >/etc/yum.repos.d/docker.repo <<-EOF
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/7
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg
EOF

[root@xuxuebiao yum.repos.d]#yum install docker-engine  libcgroup zx -y
[root@xuxuebiao yum.repos.d]# /etc/init.d/docker restart
Stopping docker:                                           [FAILED]
Starting cgconfig service:                                 [  OK  ]
Starting docker:                        [  OK  ]
                                                          
測試docker:
[root@xuxuebiao yum.repos.d]# docker run hello-world    運行hello-world鏡像,由於沒有回自動pull最新鏡像
。。。。。。。。。。
[root@xuxuebiao yum.repos.d]# docker images        查看到最新下載的鏡像
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
hello-world         latest              af340544ed62        9 days ago          960 B

查看hello-world相關的鏡像:(官方自帶測試鏡像)
[root@xuxuebiao yum.repos.d]# docker search hello-world
NAME                                     DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
hello-world                              Hello World! (an example of minimal Docker...   17        [OK]       
tutum/hello-world                        Image to test docker deployments. Has Apac...   11                   [OK]
vegasbrianc/docker-hello-world                                                           1                    [OK]
sbasyal/java-hello-world                                                                 1                    [OK]
joshuaconner/hello-world-docker-bottle                                                   1                    [OK]
bonomat/nodejs-hello-world                                                               1                    [OK]
alexwelch/hello-world                                                                    0                    [OK]
rcarun/hello-world                                                                       0                    [OK]
chunyunchen/ruby-hello-world                                                             0                    [OK]
keithchambers/docker-hello-world                                                         0                    [OK]
kocopepo/docker-hello-world                                                              0                    [OK]
lanaplexus/docker-hello-world                                                            0                    [OK]
sabya1979/java-hello-world                                                               0                    [OK]
adamkdean/hello-world                                                                    0                    [OK]
dockerman99/hello-world                                                                  0                    [OK]
vasia/docker-hello-world                 rhrthrth                                        0                    [OK]
kisp/docker-hello-world                                                                  0                    [OK]
gardleopard/docker-hello-world                                                           0                    [OK]
openshift/ruby-hello-world                                                               0                    [OK]
fermayo/hello-world                                                                      0                    [OK]
nirmata/hello-world                                                                      0                    [OK]
davelesser/hello-world                                                                   0                    [OK]
marcells/aspnet-hello-world              ASP.NET vNext - Hello World                     0                    [OK]
crccheck/hello-world                     Hello World web server in under 2.5 MB          0                    [OK]
milkyway/java-hello-world                Java Hello World Test Docker Image              0                    [OK]

2、docker簡單使用:
鏡像的獲取,能夠從官方進行下載最新的版本,也能夠在第三方docker的images共享上進行pull。
http://help.aliyun.com/knowledge_detail.htm?knowledgeId=5974865   (ali 的docker鏡像資源。汗,是基於本身的雲上的資源,外人不能下載。。。。)
國內dockerHub下載地址:http://dockerpool.com/downloads 

查看centos鏡像相關鏡像
[root@xuxuebiao yum.repos.d]# docker search centos
NAME                            DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
centos                          The official build of CentOS.                   1201      [OK]       (最新的centos鏡像了,因此就使用這個咯!)
ansible/centos7-ansible         Ansible on Centos7                              48                   [OK]
jdeathe/centos-ssh-apache-php   CentOS-6 6.6 x86_64 / Apache / PHP / PHP m...   11                   [OK]
blalor/centos                   Bare-bones base CentOS 6.5 image                9                    [OK]
openshift/wildfly-8-centos      DEPRECATED - see openshift/wildfly-81-centos7   6                    [OK]
torusware/speedus-centos        Always updated official CentOS docker imag...   6                    [OK]
million12/centos-supervisor     Base CentOS-7 with supervisord launcher, h...   5                    [OK]
nimmis/java-centos              This is docker images of CentOS 7 with dif...   4                    [OK]
jdeathe/centos-ssh              CentOS-6 6.6 x86_64 / EPEL/IUS Repos / Ope...   4                    [OK]
tcnksm/centos-node              Dockerfile for CentOS packaging node            2                    [OK]
jdeathe/centos-ssh-mysql        CentOS-6 6.6 x86_64 / MySQL.                    2                    [OK]
lighthopper/orientdb-centos     A Dockerfile for creating an OrientDB imag...   1                    [OK]
nathonfowlie/centos-jre         Latest CentOS image with the JRE pre-insta...   1                    [OK]
layerworx/centos                CentOS container with etcd, etcdctl, confd...   1                    [OK]
nathonfowlie/centos-jira        JIRA running on the latest version of CentOS    1                    [OK]
yajo/centos-epel                CentOS with EPEL and fully updated              1                    [OK]
centos/mariadb55-centos7                                                        1                    [OK]
insaneworks/centos              CentOS 6.5 x86_64 + @update                     0                    [OK]
lighthopper/openjdk-centos      A Dockerfile for creating an OpenJDK image...   0                    [OK]
jasonish/centos-suricata        Suricata base image based on CentOS 7.          0                    [OK]
pdericson/centos                Docker image for CentOS                         0                    [OK]
akroh/centos                    Centos 6 container that has been updated w...   0                    [OK]
dmglab/centos                   CentOS with superpowers!                        0                    [OK]
jsmigel/centos-epel             Docker base image of CentOS w/ EPEL installed   0                    [OK]
blacklabelops/centos            Blacklabelops Centos 7.1.503 base image wi...   0                    [OK]

注意:以上有官方的信息以及相關image的名稱,以及星級(受歡迎度)

獲取centos最新鏡像(不加後面的:latest默認下載最新鏡像)
[root@xuxuebiao yum.repos.d]# docker pull centos:latest
latest: Pulling from centos
f1b10cd84249: Pull complete
c852f6d61e65: Pull complete
7322fbe74aa5: Pull complete
Digest: sha256:90305c9112250c7e3746425477f1c4ef112b03b4abe78c612e092037bfecc3b7
Status: Downloaded newer image for centos:latest

查看全部鏡像
[root@xuxuebiao yum.repos.d]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
hello-world         latest              af340544ed62        9 days ago          960 B
centos              latest              7322fbe74aa5        8 weeks ago         172.2 MB

運行docker進入一個shell,其實如今就已經開啓了一個shell的容器了。
[root@xuxuebiao yum.repos.d]# docker run -i -t centos /bin/bash        進入docker的shell(直接啓動一個容器並進入)
[root@465d13a11ffa /]#

注意;本章使用的是docker1.7版本,所以能夠直接切換到shell中
(在2015年8月14號好像已經升級到1.8了,不過須要內核的支持哦)


測試這個容器和外網聯通,如今你就能夠想在真正的linux裏面來操做了,想裝什麼就裝什麼
[root@465d13a11ffa yum.repos.d]# ping baidu.com
PING baidu.com (220.181.57.217) 56(84) bytes of data.
64 bytes from 220.181.57.217: icmp_seq=1 ttl=48 time=42.1 ms

那麼如今就可使用centos這個鏡像啓動的容器(465d13a11ffa)進行搭建一個nginx服務器了。(http://my.oschina.net/xxbAndy/blog/493184  )


查看當前運行的容器,以及對應運行的相關鏡像和命令(須要注意的是查看container的ID)
[root@xuxuebiao ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
465d13a11ffa        centos              "/bin/bash"         5 minutes ago       Up 5 minutes                                    pensive_lovelace    
5a2f72348903        hello-world         "/hello"            32 minutes ago      Exited (0) 32 minutes ago                       elated_davinci   
表示如今有兩個容器!分別由centos鏡像和hello-world鏡像生成


將剛纔本身構建好的Nginx源碼容器(465d13a11ffa)打包成新的images(注意了,必須得在容器沒有退出的時候進行建立新的image,不然退出後以前的image仍是以前的)
[root@xuxuebiao ~]# docker commit 465d13a11ffa centos-nginx_by-xuxuebiao
0e76b29fa3b66db2f3d1354d123c7094122083b2289dc1b5cfe1dabdc89cefdf

也就是說如今300M構建了Nginx服務器!的鏡像
[root@xuxuebiao ~]# docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos-nginx_by-xuxuebiao   latest              0e76b29fa3b6        26 seconds ago      301.2 MB
hello-world                 latest              af340544ed62        9 days ago          960 B
centos                      latest              7322fbe74aa5        8 weeks ago         172.2 MB

給本身的image打標籤
[root@xuxuebiao ~]# docker tag centos:latest mytestlinux:base
[root@xuxuebiao ~]# docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos-nginx_by-xuxuebiao   latest              0e76b29fa3b6        3 minutes ago       301.2 MB
hello-world                 latest              af340544ed62        9 days ago          960 B
centos                      latest              7322fbe74aa5        8 weeks ago         172.2 MB(這兩個實際上是一個軟連接的過程哦!)
mytestlinux                 base                7322fbe74aa5        8 weeks ago         172.2 MB

查看鏡像的詳細信息,其實就是一個json文件(只讀的系統模板)而已!(而使用dockefile其實應該就是用json寫)
#docker inspect image-ID    查看鏡像的詳細信息
[root@xuxuebiao ~]# docker inspect 465d13a11ffa(鏡像id) | less
[
{
    "Id": "465d13a11ffa2d6a9d3a594b98c9903cce751fb9c9f58d6ee142b26aabdb772f",
    "Created": "2015-08-16T06:20:36.95544219Z",
    "Path": "/bin/bash",
    "Args": [],
    "State": {
        "Running": true,
        "Paused": false,
        "Restarting": false,
        "OOMKilled": false,
        "Dead": false,
        "Pid": 1971,
        "ExitCode": 0,
        "Error": "",
        "StartedAt": "2015-08-16T06:20:37.781513999Z",
        "FinishedAt": "0001-01-01T00:00:00Z"
    },
    "Image": "7322fbe74aa5632b33a400959867c8ac4290e9c5112877a7754be70cfe5d66e9",
    "NetworkSettings": {
        "Bridge": "",
        "EndpointID": "2ad4474d8540c045a3c14e2d2e2fe324d9ef51c512cee1ac671a39c19c6c43a6",
        "Gateway": "10.0.42.1",
        "GlobalIPv6Address": "",
        "GlobalIPv6PrefixLen": 0,
        "HairpinMode": false,
        "IPAddress": "10.0.0.2",
        "IPPrefixLen": 16,
        "IPv6Gateway": "",
        "LinkLocalIPv6Address": "",
        "LinkLocalIPv6PrefixLen": 0,
        "MacAddress": "02:42:0a:00:00:02",
        "NetworkID": "3a7feff16f069e0f1ced69b41b8cc12f3d9c4be55455f2680a5ce4a823a553e3",
        "PortMapping": null,
        "Ports": {},
:
[root@465d13a11ffa nginx]# exit
exit
退出由centos:latest建立的/bin/shell容器。
使用docker鏡像建立容器和使用容器完畢!


3、Docker對容器和鏡像進行管理
怎樣對容器和鏡像進行管理呢?
刪除鏡像:docker rmi
[root@xuxuebiao ~]#  docker rmi -f 465d13a11ffa    (xxbandy/latest)   最好不要建議使用docker rmi -f 強制刪除
注意:有的鏡像是打了tag的,若是刪除的時候必定要留下一個最終的image,不然數據也就刪除咯!


建立鏡像的三種方法:
    1.基於已有鏡像的容器建立(commit)(以上實驗中作的)
    2.基於本地模板導入
    3.基於dockerfile文件建立


1.基於已有鏡像建立:#docker commit  選項 描述 鏡像信息
[root@xuxuebiao ~]# docker commit -m "Nginx-test" -a "xuxuebiao" 6d0be722dcb1(容器ID) centos:nginx    基於已經運行的容器進行建立image
712d266e4b2ef6554d001a53d2b6969f4e6a67ed22816b10b422ffa2c0c80973
-m 提交信息    -a做者信息   -p 提交時暫停容器運行
[root@xuxuebiao ~]# docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos                      nginx               712d266e4b2e        56 seconds ago      301.2 MB
centos-nginx_by-xuxuebiao   latest              0e76b29fa3b6        20 minutes ago      301.2 MB
hello-world                 latest              af340544ed62        9 days ago          960 B
centos                      latest              7322fbe74aa5        8 weeks ago         172.2 MB
mytestlinux                 base                7322fbe74aa5        8 weeks ago         172.2 MB
(須要注意的是,上面五個鏡像中,四個分別至關因而硬連接和軟鏈接)
[root@xuxuebiao ~]# docker inspect 712d266e4b2e(鏡像ID)  查看centos:nginx的詳細信息(做者了等等)
[
{
    "Id": "712d266e4b2ef6554d001a53d2b6969f4e6a67ed22816b10b422ffa2c0c80973",
    "Parent": "0e76b29fa3b66db2f3d1354d123c7094122083b2289dc1b5cfe1dabdc89cefdf",
    "Comment": "Nginx-test",
    "Created": "2015-08-16T08:25:00.380756797Z",
    "Container": "6d0be722dcb181bce33189f980e41d49e26ead26999cf32d9912aabce932eb3c",
    "ContainerConfig": {
        "Hostname": "6d0be722dcb1",
        "Domainname": "",
        "User": "",
        "AttachStdin": true,
        "AttachStdout": true,
        "AttachStderr": true,
        "PortSpecs": null,
        "ExposedPorts": null,
        "Tty": true,
        "OpenStdin": true,
        "StdinOnce": true,
        "Env": null,
        "Cmd": [
            "/bin/bash"
        ],
        "Image": "0e76b29fa3b6",
        "Volumes": null,
        "VolumeDriver": "",
        "WorkingDir": "",
        "Entrypoint": null,
        "NetworkDisabled": false,
        "MacAddress": "",
        "OnBuild": null,
        "Labels": {}
    },
    "DockerVersion": "1.7.1",
    "Author": "xuxuebiao",
    "Config": {
        "Hostname": "",
        "Domainname": "",
        "User": "",
        "AttachStdin": false,
        "AttachStdout": false,
        "AttachStderr": false,
        "PortSpecs": null,
        "ExposedPorts": null,
        "Tty": false,
        "OpenStdin": false,
        "StdinOnce": false,
        "Env": null,
        "Cmd": [
            "/bin/bash"
        ],
        "Image": "",
        "Volumes": null,
        "VolumeDriver": "",
        "WorkingDir": "",
        "Entrypoint": null,
        "NetworkDisabled": false,
        "MacAddress": "",
        "OnBuild": null,
        "Labels": {}
    },
    "Architecture": "amd64",
    "Os": "linux",
    "Size": 0,
    "VirtualSize": 301236984
}
]

2.基於本地模板導入鏡像:
    #cat centos.minimal.tar.gz | docker import - centos:latest

存儲和載入鏡像;
存出鏡像:將鏡像文件保存到本地tar文件
[root@xuxuebiao ~]# docker save -o centos-nginx.tar centos:nginx
[root@xuxuebiao ~]# ls
centos-nginx.tar

載入鏡像:將本地tar鏡像文件導入本地鏡像庫
[root@xuxuebiao ~]# docker load < centos-nginx.tar
或者使用#docker load --input centos.tar

上傳鏡像:
默認上傳到dockerhub網站上,(相似於git了啦),先進行tag打標籤,而後使用push進行上傳。
#docker tag centos:nginx xxbandy/centos:nginx    (能夠區分一下那個是標籤,那個是真正的image)
#docker push xxbandy/centos:nginx(必須有dockhub的用戶名和密碼啊|則dockerhub的用戶名是xxbandy,倉庫是centos:nginx)
注意,爲何要打tag標籤呢,就是爲了可以將剛纔的鏡像成功的上傳到本身的dockerhub的repo裏面,所以這個標籤的規則和hub上的是一致的!        

好比:

[root@xuxuebiao ~]# docker push xxbandy123/centos  (個人用戶名爲xxbandy123 centos是個人repository)
The push refers to a repository [xxbandy123/centos] (len: 1)
712d266e4b2e: Image already exists
712d266e4b2e: Image already exists
0e76b29fa3b6: Image already exists
7322fbe74aa5: Image already exists
c852f6d61e65: Image already exists
Digest: sha256:0101b798bfddf10774132934dcd747002e73dfd325a85604299a89027d0d7a28


思考:在作了這麼多試驗,使用docker ps -a 會發現存在不少容器,那麼容器又須要怎樣管理呢?

感謝你們關注,幹活下次繼續!(Dockerfile建立鏡像、網絡的封裝、邏輯卷的使用、註冊中心的配置、容器後臺啓動以及調用!。。。。。 )

注意:轉載請註明來源地址。歡迎交流!

相關文章
相關標籤/搜索