您好,登錄后才能下訂單哦!
1.Docker核心概念
2.Docker鏡像操作
3.Docker容器操作
4.Docker資源控制
5.Docker數據卷管理
1.是一種輕量級的“虛擬機”
2.在Linux容器里運行應用的開源工具
參考案例指導文檔表1-1
1.打包應用程序簡化部署
2.可脫離底層硬件任意遷移
3.例:服務器從騰訊云遷移到阿里云
1.鏡像
2.容器
3.倉庫(共有,私有)
1.使用CURL獲得Docker的安裝腳本進行安裝
2.使用YUM倉庫來安裝Docker
[root@localhost ~]# yum install yum-utils device-mapper-persistent-data lvm2 -y
[root@localhost ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.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 create [選項]鏡像運行的程序
-i讓容器的標準輸入保持打開
-t 讓Docker分配一個偽終端
docker ps [選項]
-a列出最近一次啟動的容器
docker start容器的ID/名稱
docker run [選項]鏡像運行的程序執行的命令
-d在后臺進行運行
docker stop容器的ID/名稱
docker exec [選項]容器的ID/名稱/bin/bash
docker export容器的ID/名稱>文件名
cat文件名| docker import -生成的鏡像名稱:標簽
docker rm [選項]容器的ID/名稱
[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]# ls
containerd rh
[root@localhost opt]# docker save -o nginx nginx:latest
[root@localhost opt]# ls
containerd 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
[root@localhost ~]# docker images #此時Docker鏡像是空的
REPOSITORY TAG IMAGE ID CREATED SIZE
[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
[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
[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開啟的狀態
[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
#進入容器
[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
#此時狀態為退出
1.通過--cpu-quota選項來限制CPU的使用率
2.通過修改配置文件cpu.cfs_quota_us實現
docker run --cpu-shares 1024容器A
docker run --cpu-shares 1024容器B
docker run --cpu-shares 2048容器C
[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
[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/centos7
ab596821561386ec6519652606b79b363e548f29ad2c289480f7a577e8f5803b
[root@localhost docker]# docker run -itd --name c2 --cpu-shares 1024 paigeeworld/centos7
462562fbf8ba7614328c3afde41bcc466dd329fbfe79317b5ec0e6e5cefde71d
[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
[root@localhost docker]# docker run --name c3 --cpuset-cpus 0,1 paigeeworld/centos7
#此時可以使用到兩個核心數資源
[root@localhost docker]# docker run --name c4 -m 512m paigeeworld/centos7
[root@localhost docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
580c4e066308 paigeeworld/centos7 "/bin/bash" 3 seconds ago Exited (0) 2 seconds ago c4
--device-read-bps:限制讀某個設備的bps(數據量)
[root@localhost dev]# docker run -d --device-read-bps /dev/sda:30mb paigeeworld/centos7
627b05085a2305f5e2ef5a3725458e25b85441c8e400402f72f562fb71813ab1
--device-write-bps:限制寫入某個設備的bps(數據量)
例:docker run -d --device-write-bps /dev/sda:30mb paigeeworld/centos7
--device-read-iops:限制讀某個設備的iops (次數)
--device-write-iops:限制寫入某個設備的iops (次數)
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
docker run -it --volumes-from web --name db1
httpd:centos /bin/bash
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。