您好,登錄后才能下訂單哦!
docker容器是什么?一般大家對docker容器的了解可能停留在概念的層面上,而對于docker容器的應用場景和使用方法了解相對較少。今天就跟大家聊聊docker容器的具體使用。
[root@localhost ~]# yum install -y \
> yum-utils \ //設置源工具
> 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 //設置開機自啟動
[root@localhost ~]# ps aux | grep docker //查看docker進程是否開啟
[root@localhost ~]# docker version //查看版本
[root@localhost ~]# docker search nginx //搜索公有鏡像
[root@localhost ~]# docker pull nginx //下載鏡像
登錄阿里云網站--控制臺--產品服務--容器鏡像服務--鏡像加速器--選擇centos
[root@localhost ~]# tee /etc/docker/daemon.json <<-'EOF' //進行鏡像加速
> {
> "registry-mirrors": ["https://3a8s9zx5.mirror.aliyuncs.com"]
> }
> EOF
{
"registry-mirrors": ["https://3a8s9zx5.mirror.aliyuncs.com"]
}
[root@localhost ~]# systemctl daemon-reload //重載守護進程
[root@localhost ~]# systemctl restart docker //重啟docker服務
[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 //添加新標簽
[root@localhost ~]# docker images //查看鏡像
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f7bb5701a33c 4 days ago 126MB
nginx web f7bb5701a33c 4 days ago 126MB
//重新生成一個鏡像,原有的不會消失
[root@localhost ~]# docker images | grep web //查看標簽為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/ //遠程復制到13服務器上
[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 //修改標簽
[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 //基于鏡像創建一個容器 -i讓容器的標準輸入保持打開,-t讓docker分配一個偽終端
36fdfb0925ba040c094d585d70a3481bd450c7d39e6636ceeb10b5c1b9743593
[root@localhost opt]# docker ps -a //-a列出最近一次啟動的容器
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 //查看此時容器狀態為開啟
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 //創建容器
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直接下載鏡像,創建容器,并開啟,進入容器執行命令,退出
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 //進入容器(必須為開啟狀態)
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 //此時容器狀態還是開啟的狀態
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 //關閉容器
c48649c8cee9
[root@localhost opt]# docker ps -a //此時容器為退出狀態
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" //持續在后臺執行,-d在后臺進行運行
398f3d27f36b7f59a2167a71e71f61064e4e9a0808dfa13404caec0280a0b9c2
[root@localhost opt]# docker ps -a //查看容器一直是開啟狀態
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 //容器導出
[root@localhost opt]# ls
containerd nginx nginx_c rh
[root@localhost opt]# scp /opt/nginx_c root@192.168.13.129:/opt/ //遠程復制到另一臺虛擬機
[root@localhost opt]# ls
containerd nginx nginx_c rh
[root@localhost opt]# cat nginx_c | docker import - nginx:web //容器導入 會生成鏡像而不會創建容器
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 //設置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 //創建容器c1設置權重,使得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 //內存使用限制
[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:限制某個設備的數據量
--device-write-bps:限制寫入某個設備的數據量
--device-read-iops:限制讀某個設備的次數
--device-write-iops:限制寫入某個設備的次數
[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 //關聯宿主機和容器的共享
[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 //創建一個web100容器并分別有兩個卷為data1,2
[root@ba6a328c068e /]# cd data1/
[root@ba6a328c068e data1]# echo "111" > 111.txt //分別在目錄中寫入內容
[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 //將新容器掛載數據卷容器web100
[root@3f64be49dadd /]# cat data1/111.txt
111
[root@3f64be49dadd /]# cat data2/222.txt
222
以上就是docker容器介紹和使用的詳細內容了,看完之后是否有所收獲呢?如果想了解更多相關內容,歡迎關注億速云行業資訊!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。