您好,登錄后才能下訂單哦!
docker容器是什么?一般大家對(duì)docker容器的了解可能停留在概念的層面上,而對(duì)于docker容器的應(yīng)用場(chǎng)景和使用方法了解相對(duì)較少。今天就跟大家聊聊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 //開(kāi)啟docker容器
[root@localhost ~]# systemctl enable docker //設(shè)置開(kāi)機(jī)自啟動(dòng)
[root@localhost ~]# ps aux | grep docker //查看docker進(jìn)程是否開(kāi)啟
[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
[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號(hào)
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(你需要注冊(cè))
Username: //用戶名
Password: //密碼
[root@localhost opt]# docker push nginx:web //上傳公有倉(cāng)庫(kù)
[root@localhost opt]# docker create -it nginx:latest /bin/bash //基于鏡像創(chuàng)建一個(gè)容器 -i讓容器的標(biāo)準(zhǔn)輸入保持打開(kāi),-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 //開(kāi)啟容器
36fdfb0925ba
[root@localhost opt]# docker ps -a //查看此時(shí)容器狀態(tài)為開(kā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 //開(kāi)啟容器
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)建容器,并開(kāi)啟,進(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)入容器(必須為開(kāi)啟狀態(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)還是開(kā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 //查看容器一直是開(kāi)啟狀態(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 //批量刪除容器
[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
[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容器介紹和使用的詳細(xì)內(nèi)容了,看完之后是否有所收獲呢?如果想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。