Docker
學(xué)習(xí)目標(biāo):
掌握Docker基礎(chǔ)知識宰掉,能夠理解Docker鏡像與容器的概念
完成Docker安裝與啟動(dòng)
掌握Docker鏡像與容器相關(guān)命令
掌握Tomcat Nginx 等軟件的常用應(yīng)用的安裝
掌握docker遷移與備份相關(guān)命令
能夠運(yùn)用Dockerfile編寫創(chuàng)建容器的腳本
-
能夠搭建與使用docker私有倉庫
?
1 Docker簡介
1.1 什么是虛擬化
在計(jì)算機(jī)中什燕,虛擬化(英語:Virtualization)是一種資源管理技術(shù)味混,是將計(jì)算機(jī)的各種實(shí)體資源惰爬,如服務(wù)器、網(wǎng)絡(luò)锐墙、內(nèi)存及存儲(chǔ)等华匾,予以抽象、轉(zhuǎn)換后呈現(xiàn)出來浅侨,打破實(shí)體結(jié)構(gòu)間的不可切割的障礙纽谒,使用戶可以比原本的組態(tài)更好的方式來應(yīng)用這些資源。這些資源的新虛擬部份是不受現(xiàn)有資源的架設(shè)方式如输,地域或物理組態(tài)所限制鼓黔。一般所指的虛擬化資源包括計(jì)算能力和資料存儲(chǔ)。
在實(shí)際的生產(chǎn)環(huán)境中不见,虛擬化技術(shù)主要用來解決高性能的物理硬件產(chǎn)能過剩和老的舊的硬件產(chǎn)能過低的重組重用澳化,透明化底層物理硬件,從而最大化的利用物理硬件 對資源充分利用
虛擬化技術(shù)種類很多稳吮,例如:軟件虛擬化缎谷、硬件虛擬化、內(nèi)存虛擬化灶似、網(wǎng)絡(luò)虛擬化(vip)列林、桌面虛擬化、服務(wù)虛擬化酪惭、虛擬機(jī)等等希痴。
1.2 什么是Docker
Docker 是一個(gè)開源項(xiàng)目,誕生于 2013 年初春感,最初是 dotCloud 公司內(nèi)部的一個(gè)業(yè)余項(xiàng)目砌创。它基于 Google 公司推出的 Go 語言實(shí)現(xiàn)。 項(xiàng)目后來加入了 Linux 基金會(huì)甥厦,遵從了 Apache 2.0 協(xié)議纺铭,項(xiàng)目代碼在 GitHub 上進(jìn)行維護(hù)。
[圖片上傳失敗...(image-fe9d38-1545326069366)]
Docker 自開源后受到廣泛的關(guān)注和討論刀疙,以至于 dotCloud 公司后來都改名為 Docker Inc舶赔。Redhat 已經(jīng)在其 RHEL6.5 中集中支持 Docker;Google 也在其 PaaS 產(chǎn)品中廣泛應(yīng)用谦秧。
Docker 項(xiàng)目的目標(biāo)是實(shí)現(xiàn)輕量級的操作系統(tǒng)虛擬化解決方案竟纳。 Docker 的基礎(chǔ)是 Linux 容器(LXC)等技術(shù)。
在 LXC 的基礎(chǔ)上 Docker 進(jìn)行了進(jìn)一步的封裝疚鲤,讓用戶不需要去關(guān)心容器的管理锥累,使得操作更為簡便。用戶操作 Docker 的容器就像操作一個(gè)快速輕量級的虛擬機(jī)一樣簡單集歇。
為什么選擇Docker?
(1)上手快桶略。
用戶只需要幾分鐘,就可以把自己的程序“Docker化”。Docker依賴于“寫時(shí)復(fù)制”(copy-on-write)模型际歼,使修改應(yīng)用程序也非常迅速惶翻,可以說達(dá)到“隨心所致,代碼即改”的境界鹅心。
隨后吕粗,就可以創(chuàng)建容器來運(yùn)行應(yīng)用程序了。大多數(shù)Docker容器只需要不到1秒中即可啟動(dòng)旭愧。由于去除了管理程序的開銷颅筋,Docker容器擁有很高的性能,同時(shí)同一臺宿主機(jī)中也可以運(yùn)行更多的容器输枯,使用戶盡可能的充分利用系統(tǒng)資源议泵。
(2)職責(zé)的邏輯分類
使用Docker,開發(fā)人員只需要關(guān)心容器中運(yùn)行的應(yīng)用程序用押,而運(yùn)維人員只需要關(guān)心如何管理容器肢簿。Docker設(shè)計(jì)的目的就是要加強(qiáng)開發(fā)人員寫代碼的開發(fā)環(huán)境與應(yīng)用程序要部署的生產(chǎn)環(huán)境一致性。從而降低那種“開發(fā)時(shí)一切正常蜻拨,肯定是運(yùn)維的問題(測試環(huán)境都是正常的,上線后出了問題就歸結(jié)為肯定是運(yùn)維的問題)”
(3)快速高效的開發(fā)生命周期
Docker的目標(biāo)之一就是縮短代碼從開發(fā)桩引、測試到部署缎讼、上線運(yùn)行的周期,讓你的應(yīng)用程序具備可移植性坑匠,易于構(gòu)建血崭,并易于協(xié)作。(通俗一點(diǎn)說厘灼,Docker就像一個(gè)盒子夹纫,里面可以裝很多物件,如果需要這些物件的可以直接將該大盒子拿走设凹,而不需要從該盒子中一件件的取舰讹。)
(4)鼓勵(lì)使用面向服務(wù)的架構(gòu)
Docker還鼓勵(lì)面向服務(wù)的體系結(jié)構(gòu)和微服務(wù)架構(gòu)。Docker推薦單個(gè)容器只運(yùn)行一個(gè)應(yīng)用程序或進(jìn)程闪朱,這樣就形成了一個(gè)分布式的應(yīng)用程序模型月匣,在這種模型下,應(yīng)用程序或者服務(wù)都可以表示為一系列內(nèi)部互聯(lián)的容器奋姿,從而使分布式部署應(yīng)用程序锄开,擴(kuò)展或調(diào)試應(yīng)用程序都變得非常簡單,同時(shí)也提高了程序的內(nèi)省性称诗。(當(dāng)然萍悴,可以在一個(gè)容器中運(yùn)行多個(gè)應(yīng)用程序)
1.3 容器與虛擬機(jī)比較
下面的圖片比較了 Docker 和傳統(tǒng)虛擬化方式的不同之處,可見容器是在操作系統(tǒng)層面上實(shí)現(xiàn)虛擬化,直接復(fù)用本地主機(jī)的操作系統(tǒng)癣诱,而傳統(tǒng)方式則是在硬件層面實(shí)現(xiàn)计维。
[圖片上傳失敗...(image-e02239-1545326069366)]
與傳統(tǒng)的虛擬機(jī)相比,Docker優(yōu)勢體現(xiàn)為啟動(dòng)速度快狡刘、占用體積小享潜。
1.4 Docker 組件
1.4.1 Docker服務(wù)器與客戶端
Docker是一個(gè)客戶端-服務(wù)器(C/S)架構(gòu)程序。Docker客戶端只需要向Docker服務(wù)器或者守護(hù)進(jìn)程發(fā)出請求嗅蔬,服務(wù)器或者守護(hù)進(jìn)程將完成所有工作并返回結(jié)果剑按。Docker提供了一個(gè)命令行工具Docker以及一整套RESTful API。你可以在同一臺宿主機(jī)上運(yùn)行Docker守護(hù)進(jìn)程和客戶端澜术,也可以從本地的Docker客戶端連接到運(yùn)行在另一臺宿主機(jī)上的遠(yuǎn)程Docker守護(hù)進(jìn)程艺蝴。
[圖片上傳失敗...(image-901dea-1545326069366)]
1.4.2 Docker鏡像與容器
鏡像是構(gòu)建Docker的基石。用戶基于鏡像來運(yùn)行自己的容器鸟废。鏡像也是Docker生命周期中的“構(gòu)建”部分猜敢。鏡像是基于聯(lián)合文件系統(tǒng)的一種層式結(jié)構(gòu),由一系列指令一步一步構(gòu)建出來盒延。例如:
添加一個(gè)文件缩擂;
執(zhí)行一個(gè)命令;
打開一個(gè)窗口添寺。
也可以將鏡像當(dāng)作容器的“源代碼”胯盯。鏡像體積很小,非臣坡叮“便攜”博脑,易于分享、存儲(chǔ)和更新票罐。
Docker可以幫助你構(gòu)建和部署容器叉趣,你只需要把自己的應(yīng)用程序或者服務(wù)打包放進(jìn)容器即可。容器是基于鏡像啟動(dòng)起來的该押,容器中可以運(yùn)行一個(gè)或多個(gè)進(jìn)程疗杉。我們可以認(rèn)為,鏡像是Docker生命周期中的構(gòu)建或者打包階段沈善,而容器則是啟動(dòng)或者執(zhí)行階段乡数。 容器基于鏡像啟動(dòng),一旦容器啟動(dòng)完成后闻牡,我們就可以登錄到容器中安裝自己需要的軟件或者服務(wù)净赴。
所以Docker容器就是:
一個(gè)鏡像格式;
一些列標(biāo)準(zhǔn)操作罩润;
一個(gè)執(zhí)行環(huán)境玖翅。
Docker借鑒了標(biāo)準(zhǔn)集裝箱的概念。標(biāo)準(zhǔn)集裝箱將貨物運(yùn)往世界各地,Docker將這個(gè)模型運(yùn)用到自己的設(shè)計(jì)中金度,唯一不同的是:集裝箱運(yùn)輸貨物应媚,而Docker運(yùn)輸軟件。
和集裝箱一樣猜极,Docker在執(zhí)行上述操作時(shí)中姜,并不關(guān)心容器中到底裝了什么,它不管是web服務(wù)器跟伏,還是數(shù)據(jù)庫丢胚,或者是應(yīng)用程序服務(wù)器什么的。所有的容器都按照相同的方式將內(nèi)容“裝載”進(jìn)去受扳。
Docker也不關(guān)心你要把容器運(yùn)到何方:我們可以在自己的筆記本中構(gòu)建容器携龟,上傳到Registry氏义,然后下載到一個(gè)物理的或者虛擬的服務(wù)器來測試虚青,在把容器部署到具體的主機(jī)中。像標(biāo)準(zhǔn)集裝箱一樣璃氢,Docker容器方便替換华望,可以疊加蕊蝗,易于分發(fā),并且盡量通用赖舟。
1.4.3 Registry(注冊中心)
Docker用Registry來保存用戶構(gòu)建的鏡像匿又。Registry分為公共和私有兩種。Docker公司運(yùn)營公共的Registry叫做Docker Hub建蹄。用戶可以在Docker Hub注冊賬號,分享并保存自己的鏡像(說明:在Docker Hub下載鏡像巨慢裕偿,可以自己構(gòu)建私有的Registry)洞慎。
2 Docker安裝與啟動(dòng)
2.1 安裝Docker
Docker官方建議在Ubuntu中安裝,因?yàn)镈ocker是基于Ubuntu發(fā)布的嘿棘,而且一般Docker出現(xiàn)的問題Ubuntu是最先更新或者打補(bǔ)丁的劲腿。在很多版本的CentOS中是不支持更新最新的一些補(bǔ)丁包的。
由于我們學(xué)習(xí)的環(huán)境都使用的是CentOS鸟妙,因此這里我們將Docker安裝到CentOS上焦人。注意:這里建議安裝在CentOS7.x以上的版本,在CentOS6.x的版本中重父,安裝前需要安裝其他很多的環(huán)境而且Docker很多補(bǔ)丁不支持更新花椭。
請直接掛載課程配套的Centos7.x鏡像
(1)yum 包更新到最新
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> sudo yum update</pre>
(2)安裝需要的軟件包, yum-util 提供yum-config-manager功能房午,另外兩個(gè)是devicemapper驅(qū)動(dòng)依賴的
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> sudo yum install -y yum-utils device-mapper-persistent-data lvm2</pre>
(3)設(shè)置yum源為阿里云
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo</pre>
(4)安裝docker
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> sudo yum install docker-ce</pre>
(5)安裝后查看docker版本
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> docker -v</pre>
2.2 設(shè)置ustc的鏡像
ustc是老牌的linux鏡像服務(wù)提供者了矿辽,還在遙遠(yuǎn)的ubuntu 5.04版本的時(shí)候就在用。ustc的docker鏡像加速器速度很快。ustc docker mirror的優(yōu)勢之一就是不需要注冊袋倔,是真正的公共服務(wù)雕蔽。
https://lug.ustc.edu.cn/wiki/mirrors/help/docker
編輯該文件:
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> vi /etc/docker/daemon.json </pre>
在該文件中輸入如下內(nèi)容:
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> {
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
}</pre>
2.3 Docker的啟動(dòng)與停止
systemctl命令是系統(tǒng)服務(wù)管理器指令
啟動(dòng)docker:
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> systemctl start docker</pre>
停止docker:
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> systemctl stop docker</pre>
重啟docker:
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> systemctl restart docker</pre>
查看docker狀態(tài):
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> systemctl status docker</pre>
開機(jī)啟動(dòng):
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> systemctl enable docker</pre>
查看docker概要信息
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> docker info</pre>
查看docker幫助文檔
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> docker --help</pre>
3 常用命令
3.1 鏡像相關(guān)命令
3.1.1 查看鏡像
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> docker images</pre>
REPOSITORY:鏡像名稱
TAG:鏡像標(biāo)簽
IMAGE ID:鏡像ID
CREATED:鏡像的創(chuàng)建日期(不是獲取該鏡像的日期)
SIZE:鏡像大小
這些鏡像都是存儲(chǔ)在Docker宿主機(jī)的/var/lib/docker目錄下
3.1.2 搜索鏡像
如果你需要從網(wǎng)絡(luò)中查找需要的鏡像,可以通過以下命令搜索
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> docker search 鏡像名稱</pre>
NAME:倉庫名稱
DESCRIPTION:鏡像描述
STARS:用戶評價(jià)宾娜,反應(yīng)一個(gè)鏡像的受歡迎程度
OFFICIAL:是否官方
AUTOMATED:自動(dòng)構(gòu)建批狐,表示該鏡像由Docker Hub自動(dòng)構(gòu)建流程創(chuàng)建的
3.1.3 拉取鏡像
拉取鏡像就是從中央倉庫中下載鏡像到本地
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> docker pull 鏡像名稱</pre>
例如,我要下載centos7鏡像
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> docker pull centos:7</pre>
3.1.4 刪除鏡像
按鏡像ID刪除鏡像
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> docker rmi 鏡像ID</pre>
刪除所有鏡像
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> docker rmi docker images -q
</pre>
3.2 容器相關(guān)命令
3.2.1 查看容器
查看正在運(yùn)行的容器
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> docker ps</pre>
查看所有容器
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> docker ps –a</pre>
查看最后一次運(yùn)行的容器
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> docker ps –l</pre>
查看停止的容器
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> docker ps -f status=exited</pre>
3.2.2 創(chuàng)建與啟動(dòng)容器
創(chuàng)建容器常用的參數(shù)說明:
創(chuàng)建容器命令:docker run
-i:表示運(yùn)行容器
-t:表示容器啟動(dòng)后會(huì)進(jìn)入其命令行前塔。加入這兩個(gè)參數(shù)后嚣艇,容器創(chuàng)建就能登錄進(jìn)去。即分配一個(gè)偽終端嘱根。
--name :為創(chuàng)建的容器命名髓废。
-v:表示目錄映射關(guān)系(前者是宿主機(jī)目錄,后者是映射到宿主機(jī)上的目錄)该抒,可以使用多個(gè)-v做多個(gè)目錄或文件映射慌洪。注意:最好做目錄映射,在宿主機(jī)上做修改凑保,然后共享到容器上冈爹。
-d:在run后面加上-d參數(shù),則會(huì)創(chuàng)建一個(gè)守護(hù)式容器在后臺運(yùn)行(這樣創(chuàng)建容器后不會(huì)自動(dòng)登錄容器,如果只加-i -t兩個(gè)參數(shù)欧引,創(chuàng)建后就會(huì)自動(dòng)進(jìn)去容器)频伤。
-p:表示端口映射,前者是宿主機(jī)端口芝此,后者是容器內(nèi)的映射端口憋肖。可以使用多個(gè)-p做多個(gè)端口映射
(1)交互式方式創(chuàng)建容器
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> docker run -it --name=容器名稱 鏡像名稱:標(biāo)簽 /bin/bash</pre>
這時(shí)我們通過ps命令查看婚苹,發(fā)現(xiàn)可以看到啟動(dòng)的容器岸更,狀態(tài)為啟動(dòng)狀態(tài)
退出當(dāng)前容器
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> exit</pre>
(2)守護(hù)式方式創(chuàng)建容器:
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> docker run -di --name=容器名稱 鏡像名稱:標(biāo)簽</pre>
登錄守護(hù)式容器方式:
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> docker exec -it 容器名稱 (或者容器ID) /bin/bash</pre>
3.2.3 停止與啟動(dòng)容器
停止容器:
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> docker stop 容器名稱(或者容器ID)</pre>
啟動(dòng)容器:
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> docker start 容器名稱(或者容器ID)</pre>
3.2.4 文件拷貝
如果我們需要將文件拷貝到容器內(nèi)可以使用cp命令
<pre class="md-fences mock-cm md-end-block md-fences-with-lineno" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">docker cp 需要拷貝的文件或目錄 容器名稱:容器目錄
</pre>
也可以將文件從容器內(nèi)拷貝出來
<pre class="md-fences mock-cm md-end-block md-fences-with-lineno" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">docker cp 容器名稱:容器目錄 需要拷貝的文件或目錄
</pre>
3.2.5 目錄掛載
我們可以在創(chuàng)建容器的時(shí)候,將宿主機(jī)的目錄與容器內(nèi)的目錄進(jìn)行映射膊升,這樣我們就可以通過修改宿主機(jī)某個(gè)目錄的文件從而去影響容器怎炊。 創(chuàng)建容器 添加-v參數(shù) 后邊為 宿主機(jī)目錄:容器目錄,例如:
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> docker run -di -v /usr/local/myhtml:/usr/local/myhtml --name=mycentos3 centos:7</pre>
如果你共享的是多級的目錄廓译,可能會(huì)出現(xiàn)權(quán)限不足的提示评肆。
這是因?yàn)镃entOS7中的安全模塊selinux把權(quán)限禁掉了,我們需要添加參數(shù) --privileged=true 來解決掛載的目錄沒有權(quán)限的問題
3.2.6 查看容器IP地址
我們可以通過以下命令查看容器運(yùn)行的各種數(shù)據(jù)
<pre class="md-fences mock-cm md-end-block md-fences-with-lineno" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">docker inspect 容器名稱(容器ID)
</pre>
也可以直接執(zhí)行下面的命令直接輸出IP地址
<pre class="md-fences mock-cm md-end-block md-fences-with-lineno" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">docker inspect --format='{{.NetworkSettings.IPAddress}}' 容器名稱(容器ID)
</pre>
3.2.7 刪除容器
刪除指定的容器:
<pre class="md-fences mock-cm md-end-block md-fences-with-lineno" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">docker rm 容器名稱(容器ID)
</pre>
4 應(yīng)用部署
4.1 MySQL部署
(1)拉取mysql鏡像
<pre class="md-fences mock-cm md-end-block md-fences-with-lineno" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">docker pull centos/mysql-57-centos7
</pre>
(2)創(chuàng)建容器
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> docker run -di --name=tensquare_mysql -p 33306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql</pre>
-p 代表端口映射非区,格式為 宿主機(jī)映射端口:容器運(yùn)行端口
-e 代表添加環(huán)境變量 MYSQL_ROOT_PASSWORD 是root用戶的登陸密碼
(3)遠(yuǎn)程登錄mysql
連接宿主機(jī)的IP ,指定端口為33306
4.2 tomcat部署
(1)拉取鏡像
<pre class="md-fences mock-cm md-end-block md-fences-with-lineno" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">docker pull tomcat:7-jre7
</pre>
(2)創(chuàng)建容器
創(chuàng)建容器 -p表示地址映射
<pre class="md-fences mock-cm md-end-block md-fences-with-lineno" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">docker run -di --name=mytomcat -p 9000:8080
-v /usr/local/webapps:/usr/local/tomcat/webapps tomcat:7-jre7
</pre>
4.3 Nginx部署
(1)拉取鏡像
<pre class="md-fences mock-cm md-end-block md-fences-with-lineno" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">docker pull nginx
</pre>
(2)創(chuàng)建Nginx容器
<pre class="md-fences mock-cm md-end-block md-fences-with-lineno" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">docker run -di --name=mynginx -p 80:80 nginx
</pre>
4.4 Redis部署
(1)拉取鏡像
<pre class="md-fences mock-cm md-end-block md-fences-with-lineno" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">docker pull redis
</pre>
(2)創(chuàng)建容器
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> docker run -di --name=myredis -p 6379:6379 redis</pre>
5 遷移與備份
5.1 容器保存為鏡像
我們可以通過以下命令將容器保存為鏡像
<pre class="md-fences mock-cm md-end-block md-fences-with-lineno" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">docker commit mynginx mynginx_i
</pre>
5.2 鏡像備份
我們可以通過以下命令將鏡像保存為tar 文件
<pre class="md-fences mock-cm md-end-block md-fences-with-lineno" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">docker save -o mynginx.tar mynginx_i
</pre>
5.3 鏡像恢復(fù)與遷移
首先我們先刪除掉mynginx_img鏡像 然后執(zhí)行此命令進(jìn)行恢復(fù)
<pre class="md-fences mock-cm md-end-block md-fences-with-lineno" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">docker load -i mynginx.tar
</pre>
-i 輸入的文件
執(zhí)行后再次查看鏡像瓜挽,可以看到鏡像已經(jīng)恢復(fù)
6 Dockerfile
6.1 什么是Dockerfile
Dockerfile是由一系列命令和參數(shù)構(gòu)成的腳本,這些命令應(yīng)用于基礎(chǔ)鏡像并最終創(chuàng)建一個(gè)新的鏡像院仿。
1秸抚、對于開發(fā)人員:可以為開發(fā)團(tuán)隊(duì)提供一個(gè)完全一致的開發(fā)環(huán)境速和; 2、對于測試人員:可以直接拿開發(fā)時(shí)所構(gòu)建的鏡像或者通過Dockerfile文件構(gòu)建一個(gè)新的鏡像開始工作了剥汤; 3颠放、對于運(yùn)維人員:在部署時(shí),可以實(shí)現(xiàn)應(yīng)用的無縫移植吭敢。
6.2 常用命令
命令 | 作用 |
---|---|
FROM image_name:tag | 定義了使用哪個(gè)基礎(chǔ)鏡像啟動(dòng)構(gòu)建流程 |
MAINTAINER user_name | 聲明鏡像的創(chuàng)建者 |
ENV key value | 設(shè)置環(huán)境變量 (可以寫多條) |
RUN command | 是Dockerfile的核心部分(可以寫多條) |
ADD source_dir/file dest_dir/file | 將宿主機(jī)的文件復(fù)制到容器內(nèi)碰凶,如果是一個(gè)壓縮文件,將會(huì)在復(fù)制后自動(dòng)解壓 |
COPY source_dir/file dest_dir/file | 和ADD相似鹿驼,但是如果有壓縮文件并不能解壓 |
WORKDIR path_dir | 設(shè)置工作目錄 |
6.3 使用腳本創(chuàng)建鏡像
步驟:
(1)創(chuàng)建目錄
<pre class="md-fences mock-cm md-end-block md-fences-with-lineno" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">mkdir –p /usr/local/dockerjdk8
</pre>
(2)下載jdk-8u171-linux-x64.tar.gz并上傳到服務(wù)器(虛擬機(jī))中的/usr/local/dockerjdk8目錄
(3)創(chuàng)建文件Dockerfile vi Dockerfile
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> #依賴鏡像名稱和ID
FROM centos:7
指定鏡像創(chuàng)建者信息
MAINTAINER ITCAST
切換工作目錄
WORKDIR /usr
RUN mkdir /usr/local/java
ADD 是相對路徑j(luò)ar,把java添加到容器中
ADD jdk-8u171-linux-x64.tar.gz /usr/local/java/
?
配置java環(huán)境變量
ENV JAVA_HOME /usr/local/java/jdk1.8.0_171
ENV JRE_HOME JAVA_HOME/lib/dt.jar:JRE_HOME/lib:JAVA_HOME/bin:$PATH</pre>
(4)執(zhí)行命令構(gòu)建鏡像
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> docker build -t='jdk1.8' .</pre>
注意后邊的空格和點(diǎn)欲低,不要省略
(5)查看鏡像是否建立完成
<pre class="md-fences mock-cm md-end-block md-fences-with-lineno" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">docker images
</pre>
7 Docker私有倉庫
7.1 私有倉庫搭建與配置
(1)拉取私有倉庫鏡像(此步省略)
<pre class="md-fences mock-cm md-end-block md-fences-with-lineno" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">docker pull registry
</pre>
(2)啟動(dòng)私有倉庫容器
<pre class="md-fences mock-cm md-end-block md-fences-with-lineno" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">docker run -di --name=registry -p 5000:5000 registry
</pre>
(3)打開瀏覽器 輸入地址http://192.168.184.141:5000/v2/_catalog看到{"repositories":[]}
表示私有倉庫搭建成功并且內(nèi)容為空
(4)修改daemon.json
<pre class="md-fences mock-cm md-end-block md-fences-with-lineno" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">vi /etc/docker/daemon.json
</pre>
添加以下內(nèi)容,保存退出畜晰。
<pre class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"> {"insecure-registries":["192.168.184.141:5000"]} </pre>
此步用于讓 docker信任私有倉庫地址
(5)重啟docker 服務(wù)
<pre class="md-fences mock-cm md-end-block md-fences-with-lineno" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">systemctl restart docker
</pre>
7.2 鏡像上傳至私有倉庫
(1)標(biāo)記此鏡像為私有倉庫的鏡像
<pre class="md-fences mock-cm md-end-block md-fences-with-lineno" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">docker tag jdk1.8 192.168.184.141:5000/jdk1.8
</pre>
(2)再次啟動(dòng)私服容器
<pre class="md-fences mock-cm md-end-block md-fences-with-lineno" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">docker start registry
</pre>
(3)上傳標(biāo)記的鏡像
<pre class="md-fences mock-cm md-end-block md-fences-with-lineno" style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">docker push 192.168.184.141:5000/jdk1.8</pre>