3.1、docker镜像拉取
这里以nginx服务为例(已配置完镜像加速)
#检查本地镜像为空
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
#搜索nginx相关镜像(在云端镜像仓库中)
#Official build of Nginx.一般选择这种描述为官方构建的镜像
[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
#删除镜像命令(根据查询的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、运行镜像生成nginx容器
#参数d表示后台运行
#参数p表示容器和宿主机端口之间的映射,前面的是宿主机端口,后端的是容器需要的端口
#运行后会返回容器的ID
[root@docker ~]# docker run -d -p 80:80 nginx
f6430635c902b161306e1921897965c1eea5930e053976df3c829c41634951c8
#检查容器运行情况
[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
#虚拟机上可以启用多个nginx容器,注意映射不同的宿主机端口
[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
#关闭容器和重新启动容器(都需要对应的ID)
[root@docker ~]# docker stop f6430635c902
f6430635c902
[root@docker ~]# docker start f6430635c902
f6430635c902
3.3、交互式启动容器并进入容器内部
[root@docker ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 8 months ago 141MB
#参数i表示交互式操作
#参数t表示开启一个终端
#605c77e624dd是nginx镜像的ID
#bash表示进入容器后执行的命令
[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
进入启动中的容器
#查看系统上所有生成的容器
[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 参数可以对容器进行对应启动停止重启等
#使用exec参数以及ID进入容器
[root@docker ~]# docker exec -it f6430635c902 bash
#查看发行版内容
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批量化操作
批量化删除容器
#分别启动niginx和centos两个容器,给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进行删除
#批量打印镜像ID
[root@docker ~]# echo `docker images -aq`
605c77e624dd 5d0da3dc9764 afb6fca791e0
#批量删除镜像(利用上面批量打印的镜像ID)
[root@docker ~]# docker rmi `docker images -aq`
#同样可以批量删除容器,危险命令会强制删除
[root@docker ~]# docker rm `docker ps -aq`
将安装了vim的centos镜像导出为压缩包交给其他人使用
#启动容器进入确认包已安装
[root@docker ~]# docker start e9273cd68cf0
e9273cd68cf0
[root@docker ~]# docker exec -it e9273cd68cf0 bash
[root@docker ~]# vim
#这里需要使用docker commit将容器提交为新的镜像(后续学习)
#导出镜像为压缩文件
[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
#从压缩包导入镜像文件
[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
查看镜像相关信息
[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个字母
[root@docker ~]# docker image inspaect afb
补充容器运行部分参数
#参数i表示交互式操作
#参数t表示开启一个终端
#-d 后台运行
#--rm 容器运行结束后自动删除
#--name 给生成的容器命名
[root@docker ~]# docker run -d --rm --name pyt centos:7.8.2003 ping baidu.com
查看指定容器详细信息
[root@docker ~]# docker container inspect 070a
容器端口映射以及查看
#d表示容器在宿主机后端运行
#p表示端口映射,宿主机端口:容器程序端口
[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