您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Docker容器的基礎(chǔ)管理方式”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
是一種輕量級的“虛擬機(jī)”
在Linux容器里運(yùn)行應(yīng)用的開源工具
虛擬機(jī)是在一臺(tái)物理機(jī)器上,利用虛擬化技術(shù),虛擬出來多個(gè)操作系統(tǒng),每個(gè)操作系統(tǒng)之間是隔離的。Docker是開源的應(yīng)用容器引擎,依然需要先在電腦上安裝操作系統(tǒng),然后安裝Docker容器的管理器,才可以。虛擬機(jī)是在硬件級別進(jìn)行虛擬化,而Docker是在操作系統(tǒng)的層面虛擬化;虛擬機(jī)是通過模擬硬件搭建操作系統(tǒng)
打包應(yīng)用程序簡化部署
可脫離底層硬件任意遷移
例:服務(wù)器從騰訊云遷移到阿里云
鏡像
容器
倉庫
使用curl獲得docker的安裝腳本進(jìn)行安裝
使用yum倉庫來安裝docker
[root@localhost ~]# yum install -y \ > yum-utils \ //設(shè)置源工具 > device-mapper-persistent-data \ //映射工具 > lvm2 [root@localhost ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo //加載阿里云鏡像源 [root@localhost ~]# yum install docker-ce -y //安裝docker容器 [root@localhost ~]# systemctl stop firewalld.service [root@localhost ~]# setenforce 0 [root@localhost ~]# systemctl start docker //開啟docker容器 [root@localhost ~]# systemctl enable docker //設(shè)置開機(jī)自啟動(dòng) [root@localhost ~]# ps aux | grep docker //查看docker進(jìn)程是否開啟 [root@localhost ~]# docker version //查看版本 [root@localhost ~]# docker search nginx //搜索公有鏡像 [root@localhost ~]# docker pull nginx //下載鏡像
登錄阿里云網(wǎng)站--控制臺(tái)--產(chǎn)品服務(wù)--容器鏡像服務(wù)--鏡像加速器--選擇centos
[root@localhost ~]# tee /etc/docker/daemon.json <<-'EOF' //進(jìn)行鏡像加速 > { > "registry-mirrors": ["https://3a8s9zx5.mirror.aliyuncs.com"] > } > EOF { "registry-mirrors": ["https://3a8s9zx5.mirror.aliyuncs.com"] } [root@localhost ~]# systemctl daemon-reload //重載守護(hù)進(jìn)程 [root@localhost ~]# systemctl restart docker //重啟docker服務(wù) [root@localhost ~]# docker pull nginx //下載nginx鏡像 [root@localhost ~]# docker images //查看下載的鏡像信息 REPOSITORY TAG IMAGE ID CREATE SIZE nginx latest f7bb5701a33c 4 days ago 126MB
容器1:192.168.80.12
容器2:192.168.80.13
[root@localhost ~]# docker inspect f7bb5701a33c //查看鏡像信息 [root@localhost ~]# docker tag nginx:latest nginx:web //添加新標(biāo)簽 [root@localhost ~]# docker images //查看鏡像 REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f7bb5701a33c 4 days ago 126MB nginx web f7bb5701a33c 4 days ago 126MB //重新生成一個(gè)鏡像,原有的不會(huì)消失 [root@localhost ~]# docker images | grep web //查看標(biāo)簽為web的鏡像 nginx web f7bb5701a33c 4 days ago 126MB [root@localhost ~]# docker rmi nginx:web //刪除鏡像,或者直接跟ID號 Untagged: nginx:web [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f7bb5701a33c 4 days ago 126MB [root@localhost ~]# cd /opt/ [root@localhost opt]# docker save -o nginx nginx:latest //存出鏡像 [root@localhost opt]# ls containerd nginx rh [root@localhost opt]# scp /opt/nginx root@192.168.80.13:/opt/ //遠(yuǎn)程復(fù)制到13服務(wù)器上
[root@localhost opt]# docker load < nginx //載入鏡像 //或者使用docker load --input 存出文件名 [root@localhost opt]# docker images //查看鏡像信息 REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f7bb5701a33c 4 days ago 126MB
[root@localhost opt]# docker tag nginx:latest nginx:web //修改標(biāo)簽 [root@localhost opt]# docker login //登錄docker(你需要注冊) Username: //用戶名 Password: //密碼 [root@localhost opt]# docker push nginx:web //上傳公有倉庫
[root@localhost opt]# docker create -it nginx:latest /bin/bash //基于鏡像創(chuàng)建一個(gè)容器 -i讓容器的標(biāo)準(zhǔn)輸入保持打開,-t讓docker分配一個(gè)偽終端 36fdfb0925ba040c094d585d70a3481bd450c7d39e6636ceeb10b5c1b9743593 [root@localhost opt]# docker ps -a //-a列出最近一次啟動(dòng)的容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 36fdfb0925ba nginx:latest "/bin/bash" 3 seconds ago Created mystifying_dijkstra [root@localhost opt]# docker start 36fdfb0925ba //開啟容器 36fdfb0925ba [root@localhost opt]# docker ps -a //查看此時(shí)容器狀態(tài)為開啟 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 36fdfb0925ba nginx:latest "/bin/bash" 16 minutes ago Up 6 seconds 80/tcp mystifying_dijkstra
[root@localhost opt]# docker search centos7 //查看找centos7鏡像 [root@localhost opt]# docker pull paigeeworld/centos7 //下載鏡像 [root@localhost opt]# docker images //查看鏡像 REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f7bb5701a33c 4 days ago 126MB nginx web f7bb5701a33c 4 days ago 126MB paigeeworld/centos7 latest 4cbe7aa905e7 5 years ago 382MB [root@localhost opt]# docker create -it paigeeworld/centos7 /bin/bash //創(chuàng)建容器 c48649c8cee9124cb456be4f93882e6dff16f88ba45051731138142d99293dfe [root@localhost opt]# docker ps -a //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c48649c8cee9 paigeeworld/centos7 "/bin/bash" 4 seconds ago Created relaxed_curran 36fdfb0925ba nginx:latest "/bin/bash" 24 minutes ago Exited (0) 3 minutes ago mystifying_dijkstra [root@localhost opt]# docker start c48649c8cee9 //開啟容器 c48649c8cee9 [root@localhost opt]# docker ps -a //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c48649c8cee9 paigeeworld/centos7 "/bin/bash" 34 seconds ago Up 7 seconds relaxed_curran 36fdfb0925ba nginx:latest "/bin/bash" 24 minutes ago Exited (0) 3 minutes ago
[root@localhost opt]# docker run paigeeworld/centos7 /usr/bin/bash -c ls / //docker run直接下載鏡像,創(chuàng)建容器,并開啟,進(jìn)入容器執(zhí)行命令,退出 bin boot dev etc home [root@localhost opt]# docker ps -a //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c48649c8cee9 paigeeworld/centos7 "/bin/bash" 9 minutes ago Up 9 minutes relaxed_curran [root@localhost opt]# docker exec -it c48649c8cee9 /bin/bash //進(jìn)入容器(必須為開啟狀態(tài)) 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 ps -a //此時(shí)容器狀態(tài)還是開啟的狀態(tài) CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c48649c8cee9 paigeeworld/centos7 "/bin/bash" 10 minutes ago Up 10 minutes relaxed_curran [root@localhost opt]# docker stop c48649c8cee9 //關(guān)閉容器 c48649c8cee9 [root@localhost opt]# docker ps -a //此時(shí)容器為退出狀態(tài) CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c48649c8cee9 paigeeworld/centos7 "/bin/bash" 11 minutes ago Exited [root@localhost opt]# docker run -d paigeeworld/centos7 /bin/bash -c "while true;do echo hello;done" //持續(xù)在后臺(tái)執(zhí)行,-d在后臺(tái)進(jìn)行運(yùn)行 398f3d27f36b7f59a2167a71e71f61064e4e9a0808dfa13404caec0280a0b9c2 [root@localhost opt]# docker ps -a //查看容器一直是開啟狀態(tài) CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 398f3d27f36b paigeeworld/centos7 "/bin/bash -c 'while…" 9 seconds ago Up 7 seconds [root@localhost opt]# docker export 36fdfb0925ba > nginx_c //容器導(dǎo)出 [root@localhost opt]# ls containerd nginx nginx_c rh [root@localhost opt]# scp /opt/nginx_c root@192.168.13.129:/opt/ //遠(yuǎn)程復(fù)制到另一臺(tái)虛擬機(jī)
[root@localhost opt]# ls containerd nginx nginx_c rh [root@localhost opt]# cat nginx_c | docker import - nginx:web //容器導(dǎo)入 會(huì)生成鏡像而不會(huì)創(chuàng)建容器 sha256:1488d058197863aedd46d289eeb11dc39f19a2b855c3ecf383331a4d0bac568c [root@localhost opt]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx web 1488d0581978 5 seconds ago 125MB [root@localhost opt]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@localhost opt]# docker ps -a | awk '{print "docker rm "$1}' | bash //批量刪除容器
通過--cpu-quota選項(xiàng)來限制cpu的使用率
通過修改配置文件cpu.cfs_quota_us實(shí)現(xiàn)
docker run --cpu-shares 1024 容器A
docker run --cpu-shares 1024 容器B
docker run --cpu-shares 2048 容器C
[root@localhost opt]# docker run --cpu-quota 20000 nginx:latest //設(shè)置20%限定 [root@localhost opt]# cd /sys/fs/cgroup/cpu/docker/ [root@localhost opt]# cat cpu.cfs_quota_us -1 [root@localhost opt]# docker run -itd --name c1 --cpu-shares 512 paigeeworld/centos7 //創(chuàng)建容器c1設(shè)置權(quán)重,使得c1和c2的cpu資源占比為33.3%和66.7% ec4ab03a7969eebe4746cfe67184bc2c6f9c97e81b22bc2ffab452820a78a0a7 [root@localhost opt]# docker run -itd --name c2 --cpu-shares 1024 paigeeworld/centos7 c688b014329c6a33b0d66947f4489a1a1cb6febc321090ecb4a82b68ae6df250 [root@localhost opt]# docker ps -a //查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c688b014329c paigeeworld/centos7 "/bin/bash" About a minute ago Up About a minute c2 ec4ab03a7969 paigeeworld/centos7 "/bin/bash" About a minute ago Up About a minute c1 [root@localhost opt]# docker run --name c3 --cpuset-cpus 0,1 paigeeworld/centos7 //限制容器使用指定的cpu [root@localhost opt]# docker ps -a //查看容器的信息 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 751409a81682 paigeeworld/centos7 "/bin/bash" 8 seconds ago Exited (0) 7 seconds ago c3 [root@localhost opt]# docker run --name c5 -m 512m paigeeworld/centos7 //內(nèi)存使用限制 [root@localhost opt]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8c3101668345 paigeeworld/centos7 "/bin/bash" 5 seconds ago Exited (0) 4 seconds ago c5
--device-read-bps:限制某個(gè)設(shè)備的數(shù)據(jù)量 --device-write-bps:限制寫入某個(gè)設(shè)備的數(shù)據(jù)量 --device-read-iops:限制讀某個(gè)設(shè)備的次數(shù) --device-write-iops:限制寫入某個(gè)設(shè)備的次數(shù) [root@localhost opt]# docker run -d --device-write-bps /dev/sda:30mb paigeeworld/centos7
方便查看容器內(nèi)產(chǎn)生的數(shù)據(jù)
多容器間實(shí)現(xiàn)數(shù)據(jù)共享
數(shù)據(jù)卷
數(shù)據(jù)卷容器
數(shù)據(jù)卷是一個(gè)提供容器使用的特殊目錄
數(shù)據(jù)卷容器就是一個(gè)普通的容器
[root@localhost ~]# docker pull centos //下載鏡像 [root@localhost ~]# docker run -v /var/www:/data1 --name web1 -it centos /bin/bash //關(guān)聯(lián)宿主機(jī)和容器的共享 [root@2483bee94f1a /]# cd data1/ [root@2483bee94f1a data1]# echo "123" > test01.txt [root@2483bee94f1a data1]# exit exit [root@localhost ~]# cat /var/www/test01.txt 123
[root@localhost ~]# docker run --name web100 -v /data1 -v /data2 -it centos /bin/bash //創(chuàng)建一個(gè)web100容器并分別有兩個(gè)卷為data1,2 [root@ba6a328c068e /]# cd data1/ [root@ba6a328c068e data1]# echo "111" > 111.txt //分別在目錄中寫入內(nèi)容 [root@ba6a328c068e data1]# cd ../data2/ [root@ba6a328c068e data2]# echo "222" > 222.txt [root@ba6a328c068e data2]# exit exit [root@localhost ~]# docker run -it --volumes-from web100 -it centos /bin/bash //將新容器掛載數(shù)據(jù)卷容器web100 [root@3f64be49dadd /]# cat data1/111.txt 111 [root@3f64be49dadd /]# cat data2/222.txt 222
“Docker容器的基礎(chǔ)管理方式”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。