3.1孤澎、docker鏡像拉取
這里以nginx服務(wù)為例(已配置完鏡像加速)
#檢查本地鏡像為空
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
#搜索nginx相關(guān)鏡像(在云端鏡像倉庫中)
#Official build of Nginx.一般選擇這種描述為官方構(gòu)建的鏡像
[root@docker ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 17362 [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS… 177
bitnami/nginx Bitnami nginx Docker Image 140 [OK]
ubuntu/nginx Nginx, a high-performance reverse proxy & we… 58
bitnami/nginx-ingress-controller Bitnami Docker Image for NGINX Ingress Contr… 19 [OK]
rancher/nginx-ingress-controller 11
webdevops/nginx Nginx container 9 [OK]
ibmcom/nginx-ingress-controller Docker Image for IBM Cloud Private-CE (Commu… 4
bitnami/nginx-ldap-auth-daemon 3
rancher/nginx 2
kasmweb/nginx An Nginx image based off nginx:alpine and in… 2
vmware/nginx 2
rancher/nginx-ingress-controller-defaultbackend 2
rapidfort/nginx RapidFort optimized, hardened image for NGINX 2
bitnami/nginx-exporter 2
wallarm/nginx-ingress-controller Kubernetes Ingress Controller with Wallarm e… 1
vmware/nginx-photon 1
bitnami/nginx-intel 1
rapidfort/nginx-ib RapidFort optimized, hardened image for NGIN… 0
rancher/nginx-conf 0
ibmcom/nginx-ingress-controller-ppc64le Docker Image for IBM Cloud Private-CE (Commu… 0
rancher/nginx-ssl 0
continuumio/nginx-ingress-ws 0
rancher/nginx-ingress-controller-amd64 0
ibmcom/nginx-ppc64le Docker image for nginx-ppc64le 0
#拉取nginx鏡像
[root@docker ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete
a9edb18cadd1: Pull complete
589b7251471a: Pull complete
186b1aaa4aa6: Pull complete
b4df32aa5a72: Pull complete
a0bcbecc962e: Pull complete
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
#查看本地docker鏡像文件
[root@docker ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 8 months ago 141MB
#刪除鏡像命令(根據(jù)查詢的ID刪除)
#被刪除的鏡像不能被容器所依賴
[root@docker ~]# docker rmi 605c77e624dd
Untagged: nginx:latest
Untagged: nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Deleted: sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85
Deleted: sha256:b625d8e29573fa369e799ca7c5df8b7a902126d2b7cbeb390af59e4b9e1210c5
Deleted: sha256:7850d382fb05e393e211067c5ca0aada2111fcbe550a90fed04d1c634bd31a14
Deleted: sha256:02b80ac2055edd757a996c3d554e6a8906fd3521e14d1227440afd5163a5f1c4
Deleted: sha256:b92aa5824592ecb46e6d169f8e694a99150ccef01a2aabea7b9c02356cdabe7c
Deleted: sha256:780238f18c540007376dd5e904f583896a69fe620876cabc06977a3af4ba4fb5
Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f
3.2届氢、運(yùn)行鏡像生成nginx容器
#參數(shù)d表示后臺運(yùn)行
#參數(shù)p表示容器和宿主機(jī)端口之間的映射,前面的是宿主機(jī)端口覆旭,后端的是容器需要的端口
#運(yùn)行后會返回容器的ID
[root@docker ~]# docker run -d -p 80:80 nginx
f6430635c902b161306e1921897965c1eea5930e053976df3c829c41634951c8
#檢查容器運(yùn)行情況
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f6430635c902 nginx "/docker-entrypoint.…" 4 minutes ago Up 4 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp interesting_jepsen
#虛擬機(jī)上可以啟用多個(gè)nginx容器退子,注意映射不同的宿主機(jī)端口
[root@docker ~]# docker run -d -p 81:80 nginx
c70e93ca06cfbb41e9815a655b9797613f28a24ede37d8054f6ac67d4dadd24b
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c70e93ca06cf nginx "/docker-entrypoint.…" 23 seconds ago Up 22 seconds 0.0.0.0:81->80/tcp, :::81->80/tcp hopeful_chandrasekhar
f6430635c902 nginx "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp interesting_jepsen
#關(guān)閉容器和重新啟動(dòng)容器(都需要對應(yīng)的ID)
[root@docker ~]# docker stop f6430635c902
f6430635c902
[root@docker ~]# docker start f6430635c902
f6430635c902
3.3、交互式啟動(dòng)容器并進(jìn)入容器內(nèi)部
[root@docker ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 8 months ago 141MB
#參數(shù)i表示交互式操作
#參數(shù)t表示開啟一個(gè)終端
#605c77e624dd是nginx鏡像的ID
#bash表示進(jìn)入容器后執(zhí)行的命令
[root@docker ~]# docker run -it 605c77e624dd bash
root@c31e0f8620e7:/# pwd
/
root@c31e0f8620e7:/# ls
bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var
boot docker-entrypoint.d etc lib media opt root sbin sys usr
進(jìn)入啟動(dòng)中的容器
#查看系統(tǒng)上所有生成的容器
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c31e0f8620e7 605c77e624dd "/docker-entrypoint.…" 21 hours ago Exited (127) 21 hours ago brave_lewin
c70e93ca06cf nginx "/docker-entrypoint.…" 25 hours ago Exited (0) 3 minutes ago hopeful_chandrasekhar
297a8b439ee4 nginx "/docker-entrypoint.…" 25 hours ago Created elated_neumann
f6430635c902 nginx "/docker-entrypoint.…" 25 hours ago Exited (0) 3 minutes ago interesting_jepsen
#利用ID直接刪除容器
[root@docker ~]# docker rm 297a8b439ee4
297a8b439ee4
#start/stop/restart 參數(shù)可以對容器進(jìn)行對應(yīng)啟動(dòng)停止重啟等
#使用exec參數(shù)以及ID進(jìn)入容器
[root@docker ~]# docker exec -it f6430635c902 bash
#查看發(fā)行版內(nèi)容
root@f6430635c902:/# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
3.4型将、docker批量化操作
批量化刪除容器
#分別啟動(dòng)niginx和centos兩個(gè)容器寂祥,給centos容器安裝vim工具
[root@docker ~]# docker run -d -p 80:80 nginx
b4a3960907b46d58a2bac1d993d3de0431dafc29029bc692c0005f345b72d4bb
[root@docker ~]# docker run -it centos:7.8.2003 bash
[root@e9273cd68cf0 /]# vim
bash: vim: command not found
[root@e9273cd68cf0 /]# yum install -y vim
#利用反引號批量讀取容器ID進(jìn)行刪除
#批量打印鏡像ID
[root@docker ~]# echo `docker images -aq`
605c77e624dd 5d0da3dc9764 afb6fca791e0
#批量刪除鏡像(利用上面批量打印的鏡像ID)
[root@docker ~]# docker rmi `docker images -aq`
#同樣可以批量刪除容器,危險(xiǎn)命令會強(qiáng)制刪除
[root@docker ~]# docker rm `docker ps -aq`
將安裝了vim的centos鏡像導(dǎo)出為壓縮包交給其他人使用
#啟動(dòng)容器進(jìn)入確認(rèn)包已安裝
[root@docker ~]# docker start e9273cd68cf0
e9273cd68cf0
[root@docker ~]# docker exec -it e9273cd68cf0 bash
[root@docker ~]# vim
#這里需要使用docker commit將容器提交為新的鏡像(后續(xù)學(xué)習(xí))
#導(dǎo)出鏡像為壓縮文件
[root@docker ~]# docker image save centos:7.8.2003 > /tmp/centos7.8.2003.tar.gz
[root@docker ~]# ls /tmp
centos7.8.2003.tar.gz ks-script-5SfAYL yum.log
#從壓縮包導(dǎo)入鏡像文件
[root@docker ~]# docker image load -i /tmp/centos7.8.2003.tar.gz
fb82b029bea0: Loading layer [==================================================>] 211.1MB/211.1MB
Loaded image: centos:7.8.2003
查看鏡像相關(guān)信息
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 8 months ago 141MB
centos latest 5d0da3dc9764 11 months ago 231MB
centos 7.8.2003 afb6fca791e0 2 years ago 203MB
#afb是鏡像ID的前3個(gè)字母
[root@docker ~]# docker image inspaect afb
補(bǔ)充容器運(yùn)行部分參數(shù)
#參數(shù)i表示交互式操作
#參數(shù)t表示開啟一個(gè)終端
#-d 后臺運(yùn)行
#--rm 容器運(yùn)行結(jié)束后自動(dòng)刪除
#--name 給生成的容器命名
[root@docker ~]# docker run -d --rm --name pyt centos:7.8.2003 ping baidu.com
查看指定容器詳細(xì)信息
[root@docker ~]# docker container inspect 070a
容器端口映射以及查看
#d表示容器在宿主機(jī)后端運(yùn)行
#p表示端口映射七兜,宿主機(jī)端口:容器程序端口
[root@docker ~]# docker run -d -p 85:80 --name test_ngixn nginx
82c332973ca9373d7cf0ec50649adf7b0429ca3e47c8dabd2bc5751e31f455dd
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
82c332973ca9 nginx "/docker-entrypoint.…" 7 seconds ago Up 6 seconds 0.0.0.0:85->80/tcp, :::85->80/tcp test_ngixn
070a02932583 centos:7.8.2003 "bash" 33 minutes ago Up 14 minutes test1
[root@docker ~]# docker port 82c3
80/tcp -> 0.0.0.0:85
80/tcp -> :::85
安裝新程序的容器提交為新的鏡像
#這里070a的容器安裝了vim程序
[root@docker ~]# docker commit 070a02932583 cnetos7.0.2003-vim
sha256:b851a5dc45b2b64618e8038a0df92fcbae90e176f946c5ca7f596b7dd776f511
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
cnetos7.0.2003-vim latest b851a5dc45b2 9 seconds ago 441MB
nginx latest 605c77e624dd 8 months ago 141MB
centos latest 5d0da3dc9764 11 months ago 231MB
centos 7.8.2003 afb6fca791e0 2 years ago 203MB