Docker初探(一)-有關docker的介紹和簡單使用
先分享一下構建好的image(原始碼部署的Nginx) https://hub.docker.com/r/xxbandy123/centos/
本地docker image http://pan.baidu.com/s/1dDtux6h
為什麼要提起docker呢?在當前大資料盛行的環境下,資料的處理量愈來愈多,如何能夠讓應用快速的部署,消耗的資源更少,這些將是網際網路企業需要考慮的問題。隨而就興起了相關雲端計算平臺。 眾所周知的是雲端計算平臺無非分為三個方向,IAAS,PAAS,SAAs,。那麼國內,在SAAS平臺上可以說已經 做到了。比如QQ,微信,等等包括當前的一些企業應用都可以看做SAAS的應用。在IAAS,和PAAS一直沒有太多的關注,而在中國也沒有太大的發展。
那麼什麼是docker呢?docker又能為我們帶來什麼呢?
Docker 是一個開源的應用容器引擎,讓開發者可以打包他們的應用以及依賴包到一個可移植的容器中,然後釋出到任何流行的 Linux 機器上,也可以實現虛擬化。容器是完全使用沙箱機制,相互之間不會有任何介面(類似 iPhone 的 app)。幾乎沒有效能開銷,可以很容易地在機器和資料中心中執行。最重要的是,他們不依賴於任何語言、框架包括系統。
什麼是容器?有哪些現有的容器技術?Docker與其他容器技術的不同是什麼? 從巨集觀角度來看,容器更像是輕量化的虛擬機器。你可以在容器中安裝任何你想要的軟體,獨立且不影響其他容器或者宿主環境。每一個容器有它自己的網路 堆疊、程序空間、檔案系統等。同時,他們的影響要遠小於虛擬機器:容器啟動更快、佔用更少的記憶體和硬碟空間。從底層角度來看,這都是因為容器只是宿主機上的 一個程序,利用核心特徵如名稱空間和組管理來提供這種隔離。啟動一個容器只是啟動了一個普通的UNIX程序;建立一個容器只是複製了一個copy-on- wirte檔案系統的映象。
Docker與其他容器技術不同,因為它不只是一個容器引擎。Docker是一個平臺,整合了Docker引擎、Docker Hub以及一系列的生態工具,如Docker Compose、Docker Machine、Docker Swarm等。這些都採用開發API。
Docker用到的技術:
傳統虛擬化技術和Docker技術:
Docker與其他hypervisor虛擬技術的不同之處? 有人說Docker是是一個“容器的hypervisor”,但是許多人並不這麼認為,這是因為hypervisor通常是管理虛擬機器,而 Docker是管理容器。在深層技術細節也是不同的。當一個hypervisor啟動虛擬機器,它建立虛擬硬體,並利用特定CPU基礎結構和特性,如VT- x,AMD-x或者許可權層級。當Docker建立一個容器,它利用的是核心的特性,如名稱空間、群組管理,而不依賴硬體特性。 這意味著容器在某種意義上說更具有可移植性,因為它們可以同時在物理機或者虛擬機器上執行;但是他們從另外某種意義上來說,也更不容易移植,因為容 器必須使用宿主的核心。這意味著,你不能執行一個Windows容器在Linux核心上(除非linux核心可以執行Windows二進位制程式)。
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實戰:
一、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] 二、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映象建立容器和使用容器完畢!
三、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建立映象、網路的封裝、邏輯卷的使用、註冊中心的配置、容器後臺啟動以及呼叫!。。。。。 )