中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

五分鐘了解docker

發布時間:2020-02-26 02:26:35 來源:網絡 閱讀:333 作者:wx5d3fd1efe40e3 欄目:系統運維

內容要點:

一、docker概述

二、docker的核心概念及安裝方式

三、docker容器操作

四、docker資源控制

五、docker數據庫管理


一、Docker概述


Docker是什么?


1.是一種輕量級的“虛擬機”
2.在Linux容器里運行應用的開源工具


Docker與虛擬機的區別


參考案例指導文檔表1-1


Docker的使用場景(spring clond微服務)


1.打包應用程序簡化部署
2.可脫離底層硬件任意遷移
3.例:服務器從騰訊云遷移到阿里云


二、Docker的核心概念及安裝方式


Docker核心概念


1.鏡像
2.容器
3.倉庫(共有,私有)


CentOS安裝Docker的兩種方式


1.使用CURL獲得Docker的安裝腳本進行安裝
2.使用YUM倉庫來安裝Docker


Demo:在CentOS 7-4上安裝Docker


[root@localhost?~]#?yum?install?yum-utils?device-mapper-persistent-data?lvm2?-y
[root@localhost?~]#?yum-config-manager?--add-repo??
[root@localhost?~]#?ls?
/etc/yum.repos.d/CentOS-CR.repo????CentOS-fasttrack.repo??CentOS-Sources.repo??docker-ce.repo#此時會顯示docker的鏡像源
[root@localhost?~]#?yum?install?-y?docker-ce
[root@localhost?~]#?systemctl?stop?firewalld.service
[root@localhost?~]#?setenforce?0
[root@localhost?~]#?systemctl?start?docker.service
[root@localhost?~]#?systemctl?enable?docker.service
[root@localhost?~]#?docker?search?nginx?????#搜索nginx鏡像
NAME??????????????????????????????DESCRIPTION?????????????????????????????????????STARS???????????????OFFICIAL????????????AUTOMATED
nginx?????????????????????????????Official?build?of?Nginx.????????????????????????12375???????????????[OK]
jwilder/nginx-proxy???????????????Automated?Nginx?reverse?proxy?for?docker?con…???1704????????????????????????????????????[OK]
richarvey/nginx-php-fpm???????????Container?running?Nginx?+?PHP-FPM?capable?of…???749?????????????????????????????????????[OK]
linuxserver/nginx?????????????????An?Nginx?container,?brought?to?you?by?LinuxS…???84?????
......以下省略多項#顯示出的倉庫中的鏡像按星級進行排名,星級越高,下載人數越多,自然就越靠譜
[root@localhost?~]#?docker?pull?nginx#此時我們不使用加速進行下載
Using?default?tag:?latest
latest:?Pulling?from?library/nginx
000eee12ec04:?Downloading??3.083MB/27.09MB
eb22865337de:?Downloading??1.703MB/23.74MB
bee5d581ef8b:?Download?complete
#此時速度會非常慢,有時候甚至會出現卡頓

#我們可以在此處做一個優化操作就是:鏡像加速`此命令可以在阿里云的官網上搜索“官方鏡像加速”從而獲取到自己的命令代碼(需要先注冊阿里云平臺的賬號)`
`找到控制臺,登陸后找到左側的“產品與服務”,再找到“容器鏡像服務”,再找到左側的“鏡像加速器”,在“操作文檔”中選擇自己的系統,此處我們需要選擇“CentOS”,就可以找到自己賬號生成的鏡像加速配置文件`

[root@localhost?~]#?tee?/etc/docker/daemon.json?<<-'EOF'
{
??"registry-mirrors":?["https://w1ogxqvl.mirror.aliyuncs.com"]
}
EOF

[root@localhost?~]#?systemctl?daemon-reload
[root@localhost?~]#?systemctl?restart?docker
[root@localhost?~]#?docker?pull?nginx
Using?default?tag:?latest
latest:?Pulling?from?library/nginx
000eee12ec04:?Pull?complete
eb22865337de:?Pull?complete
bee5d581ef8b:?Pull?complete
Digest:?sha256:50cf965a6e08ec5784009d0fccb380fc479826b6e0e65684d9879170a9df8566
Status:?Downloaded?newer?image?for?nginx:latest
docker.io/library/nginx:latest
#此時很快就可以下載完成了
[root@localhost?~]#?docker?images???????????????#查看下載下來的鏡像
REPOSITORY??????????TAG?????????????????IMAGE?ID????????????CREATED?????????????SIZE
nginx???????????????latest??????????????231d40e811cd????????4?weeks?ago?????????126MB


四、Docker容器操作



創建容器:


docker create [選項]鏡像運行的程序

-i讓容器的標準輸入保持打開
-t 讓Docker分配一個偽終端


查看容器運行狀態:


docker ps [選項]

-a列出最近一次啟動的容器


啟動容器:


docker start容器的ID/名稱


創建容器并啟動執行shell命令:


docker run [選項]鏡像運行的程序執行的命令

-d在后臺進行運行


終止容器:


docker stop容器的ID/名稱


進入容器:


docker exec [選項]容器的ID/名稱/bin/bash


導出容器:


docker export容器的ID/名稱&gt;文件名


導入容器:


cat文件名| docker import -生成的鏡像名稱:標簽


刪除容器:


docker rm [選項]容器的ID/名稱


Demo:基礎操作


[root@localhost?~]#?docker?inspect?231d40e811cd?????????????#查看鏡像信息
[
????{
????????????"Id":?"sha256:231d40e811cd970168fb0c4770f2161aa30b9ba6fe8e68527504df69643aa145",
????????????????????"RepoTags":?[
????????????????????????????????"nginx:latest"
????????],
????????????????"RepoDigests":?[
......以下省略多行

[root@localhost?~]#?docker?tag?nginx:latest?nginx:web???????#重新定義標簽
[root@localhost?~]#?docker?images
REPOSITORY??????????TAG?????????????????IMAGE?ID????????????CREATED?????????????SIZE
nginx???????????????latest??????????????231d40e811cd????????4?weeks?ago?????????126MB
nginx???????????????web?????????????????231d40e811cd????????4?weeks?ago?????????126MB
[root@localhost?~]#?docker?images?|?grep?nginx
nginx???????????????latest??????????????231d40e811cd????????4?weeks?ago?????????126MB
nginx???????????????web?????????????????231d40e811cd????????4?weeks?ago?????????126MB
#刪除鏡像
[root@localhost?~]#?docker?rmi?nginx:web
Untagged:?nginx:web
[root@localhost?~]#?docker?images
REPOSITORY??????????TAG?????????????????IMAGE?ID????????????CREATED?????????????SIZE
nginx???????????????latest??????????????231d40e811cd????????4?weeks?ago?????????126MB
#存出鏡像
[root@localhost?~]#?cd?/opt/
[root@localhost?opt]#?lscontainerd??rh
[root@localhost?opt]#?docker?save?-o?nginx?nginx:latest
[root@localhost?opt]#?lscontainerd??nginx??rh
[root@localhost?opt]#?ls?-lh
總用量?125M
drwx--x--x.?4?root?root???28?12月?24?11:01?containerd
-rw-------.?1?root?root?125M?12月?24?11:17?nginx
drwxr-xr-x.?2?root?root????6?3月??26?2015?rh


Demo:導入操作


需要再打開另外一臺虛擬機CentOS 7-5,并安裝Docker引擎:


[root@localhost?~]#?docker?images???????????????#此時Docker鏡像是空的
REPOSITORY??????????TAG?????????????????IMAGE?ID????????????CREATED?????????????SIZE

再回到CentOS 7-4,將之前導出的nginx文件導入到另一臺虛擬機上:


[root@localhost?opt]#?scp?/opt/nginx?root@192.168.18.145:/opt/
The?authenticity?of?host?'192.168.18.145?(192.168.18.145)'?can't?be?established.
ECDSA?key?fingerprint?is?SHA256:mTT+FEtzAu4X3D5srZlz93S3gye8MzbqVZFDzfJd4Gk.
ECDSA?key?fingerprint?is?MD5:fa:5a:88:23:49:60:9b:b8:7e:4b:14:4b:3f:cd:96:a0.
Are?you?sure?you?want?to?continue?connecting?(yes/no)??yes
Warning:?Permanently?added?'192.168.18.145'?(ECDSA)?to?the?list?of?known?hosts.
root@192.168.18.145's?password:
nginx????????????????????????????????????????????????????????100%??124MB?124.0MB/s???00:01

再回到CentOS 7-5上進行查看操作:


[root@localhost?~]#?cd?/opt/
[root@localhost?opt]#?ls
containerd??nginx??rh
[root@localhost?opt]#?docker?load??<?nginx??????????????#導入過程
831c5620387f:?Loading?layer??72.48MB/72.48MB
5fb987d2e54d:?Loading?layer??57.67MB/57.67MB
4fc1aa8003a3:?Loading?layer??3.584kB/3.584kB
Loaded?image:?nginx:latest
[root@localhost?opt]#?docker?images?????????????#查看鏡像
REPOSITORY??????????TAG?????????????????IMAGE?ID????????????CREATED?????????????SIZE
nginx???????????????latest??????????????231d40e811cd????????4?weeks?ago?????????126MB


Demo:容器的操作


[root@localhost?opt]#?docker?ps?-a
CONTAINER?ID????????IMAGE???????????????COMMAND?????????????CREATED?????????????STATUS??????????????PORTS???????????????NAMES
[root@localhost?opt]#?docker?create?-it?nginx:latest?/bin/bash????????#容器創建82013604d57b7e0cbad870b367ca241579581a3c34d977aec58157a89fe9188e
[root@localhost?opt]#?docker?ps?-a
CONTAINER?ID????????IMAGE???????????????COMMAND?????????????CREATED?????????????STATUS??????????????PORTS???????????????NAMES
82013604d57b????????nginx:latest????????"/bin/bash"?????????18?seconds?ago??????Created?????????????????????????????????laughing_robinson
[root@localhost?opt]#?docker?start?82013604d57b?????????????#啟動
82013604d57b
[root@localhost?opt]#?docker?ps?-a
CONTAINER?ID????????IMAGE???????????????COMMAND?????????????CREATED?????????????STATUS??????????????PORTS???????????????NAMES
82013604d57b????????nginx:latest????????"/bin/bash"?????????2?minutes?ago???????Up?20?seconds???????80/tcp??????????????laughing_robinson
#此時狀態為up,端口為80

[root@localhost?opt]#?docker?search?centos7
[root@localhost?opt]#?docker?pull?paigeeworld/centos7
[root@localhost?opt]#?docker?images
REPOSITORY????????????TAG?????????????????IMAGE?ID????????????CREATED?????????????SIZE
nginx?????????????????latest??????????????231d40e811cd????????4?weeks?ago?????????126MB
paigeeworld/centos7???latest??????????????4cbe7aa905e7????????5?years?ago?????????382MB

[root@localhost?opt]#?docker?create?-it?paigeeworld/centos7/bin/bash
fe74e5e0e354e855f76b2edc043358c71f9a2e99d10ac3875c610e1fd9dac4d7
[root@localhost?opt]#?docker?ps?-a
CONTAINER?ID????????IMAGE?????????????????COMMAND?????????????CREATED?????????????STATUS??????????????PORTS???????????????NAMES
fe74e5e0e354????????paigeeworld/centos7???"/bin/bash"?????????8?seconds?ago???????Created?????????????????????????????????trusting_satoshi
#此時只是Created創建好的狀態
[root@localhost?opt]#?docker?start?fe74e5e0e354?????????????#啟動
fe74e5e0e354
[root@localhost?opt]#?docker?ps?-a
CONTAINER?ID????????IMAGE?????????????????COMMAND?????????????CREATED??????????????STATUS??????????????PORTS???????????????NAMES
fe74e5e0e354????????paigeeworld/centos7???"/bin/bash"?????????About?a?minute?ago???Up?11?seconds???????????????????????????trusting_satoshi
82013604d57b????????nginx:latest??????????"/bin/bash"?????????8?minutes?ago????????Up?6?minutes????????80/tcp??????????????laughing_robinson
#此時是Up開啟的狀態


Demo:對于開啟的容器進行命令操作


[root@localhost?opt]#?docker?run?paigeeworld/centos7?/usr/bin/bash?-c?ls?/
#查看鏡像根目錄下有哪些文件bin
boot
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
[root@localhost?opt]#?docker?ps?-a
CONTAINER?ID????????IMAGE?????????????????COMMAND??????????????????CREATED??????????????STATUS??????????????????????????PORTS???????????????NAMES
d04961e9862f????????paigeeworld/centos7???"/usr/bin/bash?-c?ls…"???About?a?minute?ago???Exited?(0)?About?a?minute?ago???????????????????????frosty_pare
#docker?run?=?鏡像做了create然后start,再進入到容器中執行了ls,最后執行了stop

此時我們再對沒有此鏡像的CentOS 7-5 執行相同操作,看看有何不同:
它會先找倉庫進行下載,再創建容器,再啟動容器 ,在執行ls命令,最后再stop,過程一氣呵成(過程如下圖)


五分鐘了解docker

#進入容器[root@localhost?opt]#?docker?ps?-a
CONTAINER?ID????????IMAGE?????????????????COMMAND??????????????????CREATED?????????????STATUS??????????????????????PORTS???????????????NAMES
d04961e9862f????????paigeeworld/centos7???"/usr/bin/bash?-c?ls…"???15?minutes?ago??????Exited?(0)?15?minutes?ago???????????????????????frosty_pare
fe74e5e0e354????????paigeeworld/centos7???"/bin/bash"??????????????22?minutes?ago??????Up?21?minutes???????????????????????????????????trusting_satoshi
82013604d57b????????nginx:latest??????????"/bin/bash"??????????????30?minutes?ago??????Up?28?minutes???????????????80/tcp??????????????laughing_robinson
[root@localhost?opt]#?docker?exec?-it?fe74e5e0e354?/bin/bash????????#選擇運行的容器進入環境
bash-4.2#?ls?/bin???dev??home??lib64???????media??opt???root??sbin??sys??usr
boot??etc??lib???lost+found??mnt????proc??run???srv???tmp??var
bash-4.2#?exit
exit
#終止運行
[root@localhost?opt]#?docker?stop?fe74e5e0e354??????????????#停止
fe74e5e0e354
[root@localhost?opt]#?docker?ps?-a
CONTAINER?ID????????IMAGE?????????????????COMMAND??????????????????CREATED?????????????STATUS???????????????????????PORTS???????????????NAMES
fe74e5e0e354????????paigeeworld/centos7???"/bin/bash"??????????????25?minutes?ago??????Exited?(137)?4?seconds?ago???????????????????????trusting_satoshi
#此時狀態為退出


四、Docker資源控制


限制CPU使用速率:


1.通過--cpu-quota選項來限制CPU的使用率
2.通過修改配置文件cpu.cfs_quota_us實現


多任務按比例分享CPU:(其中數字為權重)


docker run --cpu-shares 1024容器A
docker run --cpu-shares 1024容器B
docker run --cpu-shares 2048容器C


使用--cpuset-cpus選項限制CPU內核使用權


Demo1:CPU使用率控制


[root@localhost?~]#?docker?run?--cpu-quota?20000?nginx:latest
#該容器在電腦中所占資源只有20%,如果沒有設置它就會對別的資源進行搶占
[root@localhost?~]#?docker?ps?-a
CONTAINER?ID????????IMAGE???????????????COMMAND??????????????????CREATED?????????????STATUS?????????????????????PORTS???????????????NAMES
e1e8a4aef629????????nginx:latest????????"nginx?-g?'daemon?of…"???5?seconds?ago???????Up?4?seconds???????????????80/tcp??????????????strange_dubinsky
#此時會顯示一個新的進程(需要新開一個終端進行查看)

[root@localhost?~]#?cd?/sys/fs/cgroup/cpu/docker/
[root@localhost?docker]#?ls
cgroup.procs???????????cpu.shares
cpuacct.stat???????????cpu.stat
cpuacct.usage??????????e1e8a4aef6293bb2f116fe219ef0e2e190d9e094d79bb583925b49d0af3e9e67
#此時會在配置文件的目錄下找到以’e1e8a4aef629’開頭的配置文件
[root@localhost?docker]#?cd?e1e8a4aef6293bb2f116fe219ef0e2e190d9e094d79bb583925b49d0af3e9e67
[root@localhost?e1e8a4aef6293bb2f116fe219ef0e2e190d9e094d79bb583925b49d0af3e9e67]#?ls
cgroup.clone_children??cpuacct.usage?????????cpu.rt_period_us???notify_on_release
cgroup.event_control???cpuacct.usage_percpu??cpu.rt_runtime_us??tasks
cgroup.procs???????????cpu.cfs_period_us?????cpu.shares
cpuacct.stat???????????cpu.cfs_quota_us??????cpu.stat
[root@localhost?e1e8a4aef6293bb2f116fe219ef0e2e190d9e094d79bb583925b49d0af3e9e67]#?cat?cpu.cfs_quota_us?????#此quota文件可以設置上限值20000

Demo2:按比例分配


[root@localhost?docker]#?docker?pull?paigeeworld/centos7
#需要再下載一個cento7的鏡像
[root@localhost?docker]#?docker?images
REPOSITORY????????????TAG?????????????????IMAGE?ID????????????CREATED?????????????SIZE
nginx?????????????????latest??????????????231d40e811cd????????4?weeks?ago?????????126MB
paigeeworld/centos7???latest??????????????4cbe7aa905e7????????5?years?ago?????????382MB

`創建兩個容器為c1和c2,若只有這兩個容器,設置容器的權重,使得c1和c2的CPU資源占比為33.3%和66.7%`
[root@localhost?docker]#?docker?run?-itd?--name?cl?--cpu-shares?512?paigeeworld/centos7ab596821561386ec6519652606b79b363e548f29ad2c289480f7a577e8f5803b
[root@localhost?docker]#?docker?run?-itd?--name?c2?--cpu-shares?1024?paigeeworld/centos7462562fbf8ba7614328c3afde41bcc466dd329fbfe79317b5ec0e6e5cefde71d

[root@localhost?docker]#?docker?ps?-a
CONTAINER?ID????????IMAGE?????????????????COMMAND??????????????????CREATED?????????????STATUS??????????????????????PORTS???????????????NAMES
462562fbf8ba????????paigeeworld/centos7???"/bin/bash"??????????????35?seconds?ago??????Up?34?seconds???????????????????????????????????c2
ab5968215613????????paigeeworld/centos7???"/bin/bash"??????????????36?seconds?ago??????Up?36?seconds???????????????????????????????????cl#此時就會產生cl和c2兩個新的容器了,分別占用1/3和2/3

Demo3:限制容器使用指定的CPU


[root@localhost?docker]#?docker?run?--name?c3?--cpuset-cpus?0,1?paigeeworld/centos7
#此時可以使用到兩個核心數資源

Demo4:內存使用限制


[root@localhost?docker]#?docker?run?--name?c4?-m?512m?paigeeworld/centos7
[root@localhost?docker]#?docker?ps?-aCONTAINER?ID????????IMAGE?????????????????COMMAND??????????????????CREATED?????????????STATUS??????????????????????PORTS???????????????NAMES
580c4e066308????????paigeeworld/centos7???"/bin/bash"??????????????3?seconds?ago???????Exited?(0)?2?seconds?ago????????????????????????c4

Demo4:對blkio的限制


--device-read-bps:限制讀某個設備的bps(數據量)??
[root@localhost?dev]#?docker?run?-d?--device-read-bps?/dev/sda:30mb?paigeeworld/centos7627b05085a2305f5e2ef5a3725458e25b85441c8e400402f72f562fb71813ab1

--device-write-bps:限制寫入某個設備的bps(數據量)?
例:docker?run?-d?--device-write-bps?/dev/sda:30mb?paigeeworld/centos7

--device-read-iops:限制讀某個設備的iops?(次數)
--device-write-iops:限制寫入某個設備的iops?(次數)


五、Docker的數據管理


數據管理操作:


1.方便查看容器內產生的數據
2.多容器間實現數據共享


兩種管理方式:


1.數據卷
2.數據卷容器


數據卷:直接在centos上添加,可實現數據共享,實現的是宿主和容器之間的數據共享

數據卷容器:容器之間實現共享,和宿主機沒有關系,A容器中的a目錄與B容器上的d目錄實現數據共享


數據卷


數據卷是一個提供容器使用的特殊目錄


創建數據卷:


docker run -d -v /data1 -v /data2 --name web
httpd:centos

掛載主機目錄作為數據卷:

docker run -d -v /var/www:/data1 --name web-1
httpd:centos



數據卷容器


  1. 數據卷容器就是一個普通的容器
  2. 掛載web容器中的數據卷到新的容器


docker run -it --volumes-from web --name db1
httpd:centos /bin/bash


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

饶平县| 沐川县| 四川省| 来宾市| 读书| 通州区| 浠水县| 湘潭市| 甘孜县| 古浪县| 原平市| 中宁县| 南岸区| 上饶市| 慈溪市| 正宁县| 长宁区| 汉阴县| 乳山市| 拜城县| 栖霞市| 桑日县| 新巴尔虎右旗| 从江县| 苗栗县| 安丘市| 嘉定区| 淳化县| 太原市| 平湖市| 泰顺县| 淮南市| 淄博市| 庐江县| 肥西县| 安徽省| 临漳县| 武清区| 石泉县| 大足县| 星子县|