Linux之Docker-01

时间:2022-10-15 22:00:39
一、镜像基础命令
1、docker version
[root@DY-Ubuntu-01 ~]#docker version                #查看 Docker 版本
2、docker info
[root@DY-Ubuntu-01 ~]#docker info
Client:
Context: default
Debug Mode: false #client 端是否开启 debug

Server:
Containers: 0 #当前主机运行的容器总数
Running: 0 #有几个容器是正在运行的
Paused: 0 #有几个容器是暂停的
Stopped: 0 #有几个容器是停止的
Images: 0 #当前服务器的镜像数
Server Version: 20.10.19 #服务端版本
Storage Driver: overlay2 #正在使用的存储引擎
Backing Filesystem: xfs #后端文件系统,即服务器的磁盘文件系统
Supports d_type: true #是否支持 d_type
Native Overlay Diff: true #是否支持差异数据存储
userxattr: false
Logging Driver: json-file #日志类型
Cgroup Driver: cgroupfs #Cgroups 类型
Cgroup Version: 1
Plugins: #插件
Volume: local #卷
Network: bridge host ipvlan macvlan null overlay # overlay 跨主机通信
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog # 日志类型
Swarm: inactive #是否支持 swarm
Runtimes: runc io.containerd.runc.v2 io.containerd.runtime.v1.linux #已安装的容器运行时
Default Runtime: runc #默认使用的容器运行时
Init Binary: docker-init #初始化容器的守护进程,即 pid 为 1 的进程
containerd version: 9cd3357b7fd7218e4aec3eae239db1f68a5a6ec6 #版本
runc version: v1.1.4-0-g5fd4c4d1 #runc 版本
init version: de40ad0 #init 版本
Security Options: #安全选项
apparmor #安全模块,https://docs.docker.com/engine/security/apparmor/
seccomp #安全计算模块,即制容器操作,https://docs.docker.com/engine/security/seccomp
Profile: default #默认的配置文件
Kernel Version: 5.4.0-126-generic #宿主机内核版本
Operating System: Ubuntu 20.04.4 LTS #宿主机操作系统
OSType: linux #宿主机操作系统类型
Architecture: x86_64 #宿主机架构
CPUs: 2 #宿主机 CPU 数量
Total Memory: 3.84GiB #宿主机总内存
Name: DY-Ubuntu-01 #宿主机 hostname
ID: YZQ6:756A:YPQR:XKA7:POII:EQ6Z:KUPN:BHPK:Q6QK:GYCX:56RA:PWJY #宿主机 ID
Docker Root Dir: /var/lib/docker #宿主机关于docker数据的保存目录
Debug Mode: false #server 端是否开启 debug
Registry: https://index.docker.io/v1/ #仓库路径
Labels:
Experimental: false #是否测试版
Insecure Registries:
127.0.0.0/8 #非安全的镜像仓库
Registry Mirrors:
https://pgavrk5n.mirror.aliyuncs.com/ #镜像仓库
Live Restore Enabled: false #是否开启活动重启 (重启docker-daemon 不关闭容器 )
Product License: Community Engine
WARNING: No swap limit support #系统警告信息 (没有开启 swap 资源限制 )

范例: 解决SWAP报警提示 " WARNING: No swap limit support "

[root@DY-Ubuntu-01 ~]#vim /etc/default/grub
[root@DY-Ubuntu-01 ~]#update-grub
[root@DY-Ubuntu-01 ~]#reboot
3、docker search、pull
[root@DY-Ubuntu-01 ~]#docker search nginx              #搜索nginx镜像,默认版本latest默认从docker官网搜索
[root@DY-Ubuntu-01 ~]#docker pull nginx #拉取nginx镜像,默认从docker官网拉取
[root@DY-Ubuntu-01 ~]#docker image history nginx #查看镜像分层历史
[root@DY-Ubuntu-01 ~]#docker inspect nginx #查看镜像详细信息
[root@DY-Ubuntu-01 ~]#docker save nginx -o nginx.tar #导出nginx镜像
[root@DY-Ubuntu-01 ~]#docker images #列出镜像文件
[root@DY-Ubuntu-01 ~]#docker search -f=stars=100 nginx   #搜索点赞大于100的nginx镜像
[root@DY-Ubuntu-01 ~]#docker search --limit 3 nginx #搜索三个结果
[root@node1 ~]# docker run -it -d --name alpine-1 --rm alpine     #后台交互运行一个名称为alpine-1的alpine容器,停止容器后删除容器
[root@node1 ~]# docker exec -it alpine-1 /bin/sh #进入alpine-1容器,指定bash类型为sh
/ # vi /etc/apk/repositories #修改源替换成阿里源
https://mirrors.aliyun.com/alpine/v3.15/main
https://mirrors.aliyun.com/alpine/v3.15/community

/ # apk update #更新源
/ # apk add vim #安装软件
/ # apk del openssh #删除软件
/ # apk info nginx #显示软件的详细信息
/ # apk manifest nginx #显示校验
# Debian(ubuntu)系统建议安装的基础包
# apt update #安装软件前需要先更新索引
# apt install procps #提供top,ps,free等命令
# apt install psmisc # 提供pstree,killall等命令
# apt install iputils-ping #提供ping命令
# apt install net-tools #提供netstat网络工具等
# apt install iproute2 #提供ip,ss网络工具等
[root@node1 ~]# docker pull hello-world
Using default tag: latest #默认下载最新版本
latest: Pulling from library/hello-world
2db29710123e: Pull complete #分层下载
Digest: sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f #摘要
Status: Downloaded newer image for hello-world:latest #状态
docker.io/library/hello-world:latest #下载的完整地址
[root@node1 ~]# ll /var/lib/docker/overlay2/镜像ID        #镜像下载保存的路径
注意: 镜像下载完成后,会自动解压缩,比官网显示的可能会大很多,如: centos8.1.1911下载时只有70MB,下载完后显示237MB
4、阿里云加速
浏览器打开http://cr.console.aliyun.com,注册或登录阿里云账号,点击左侧的镜像加速器,将会得到一个专属的加速地址,而且有使用配置说明

修改daemon配置文件/etc/docker/daemon.json来使用加速器
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn","http://hub-mirror.c.163.com/","https://si7y70hh.mirror.aliyuncs.com"]
}
EOF
#网易云: http://hub-mirror.c.163.com/
#中科大: https://docker.mirrors.ustc.edu.cn
#腾讯云: https://mirror.ccs.tencentyun.com
#七牛云: https://reg-mirror.qiniu.com
systemctl daemon-reload
systemctl restart docker
5、查看本地镜像
[root@node1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE

REPOSITORY #镜像所属的仓库名称
TAG #镜像版本号(标识符),默认为latest
IMAGE ID #镜像唯一ID标识,如果ID相同,说明是同一个镜像有多个名称
CREATED #镜像在仓库中被创建时间
VIRTUAL SIZE #镜像的大小
[root@node1 ~]# docker images -q              #查看镜像id
c059bfaa849c
feb5d9fea6a5
[root@node1 ~]# docker images --no-trunc #显示完整的ImageID
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest sha256:c059bfaa849c4d8e4aecaeb3a10c2d9b3d85f5165c66ad3a4d937758128c4d18 10 months ago 5.59MB
hello-world latest sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412 12 months ago 13.3kB

[root@node1 ~]# docker image inspect alpine #查看指定镜像的详细信息
6、镜像导出
#导出单个镜像
#方法1
[root@node1 ~]# docker save -o alpine.tar alpine
#方法2
[root@node1 ~]# docker save alpine > alpine1.tar

=========================
#导出多个镜像至一个文件
#方法1
[root@node1 ~]# docker save -o all1.tar alpine hello-world
#方法2
[root@node1 ~]# docker save alpine hello-world -o all.tar
#方法3
[root@node1 ~]# docker save alpine hello-world > all2.tar

==========================
#导出所有镜像至一个文件
#方法1
[root@node1 ~]# docker save `docker images | awk 'NR>1{print $1":"$2}'` -o all4.tar #导出所有镜像到一个打包文件,此方法导入后可以看REPOSITORY和TAG
#方法2
[root@node1 ~]# docker image save `docker image ls --format "{{.Repository}}:{{.Tag}}"` -o all5.tar
#将所有镜像导入到一个文件中,此方法导入后可以看REPOSITORY和TAG

============================
#导出所有镜像至不同的文件中
#方法1
[root@node1 ~]# docker images | awk 'NR!=1{print $1,$2}' | while read repo tag ;do docker save ${repo}:${tag} -o ${repo}-${tag}.tar; done #导出所有镜像至不同的文件中

#方法2
[root@node1 ~]# for i in `docker images --format "{{.Repository}}:{{.Tag}}"`; do docker save $i -o `echo $i|cut -d: -f1`.tar; done #导出所有镜像至不同的文件中
7、镜像导入
#方法1
[root@DY-Ubuntu-01 ~]#docker load -i all5.tar
#方法2
[root@DY-Ubuntu-01 ~]#docker load < all5.tar
8、删除镜像
[root@DY-Ubuntu-01 ~]#docker rmi nginx
[root@DY-Ubuntu-01 ~]#docker rmi -f hello-world #强制删除正在使用的镜像,也会删除对应的容器
[root@DY-Ubuntu-01 ~]#docker rmi nginx httpd #删除多个镜像
================================================
[root@node1 ~]# docker image prune #清理dangling虚无镜像(没被标记且没被其它任何镜像引用的镜像)
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B

[root@node1 ~]# docker image prune -a -f #清理dangling虚无镜像以及不再使用的镜像
Deleted Images:
untagged: hello-world:latest
untagged: hello-world@sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
deleted: sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359
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
9、镜像打标签
#TARGET_IMAGE[:TAG]格式一般形式
仓库主机FQDN或IP[:端口]/项目名(或用户名)/image名字:版本
[root@DY-Ubuntu-01 ~]#docker tag alpine alpine:10.15
10、命令总结: 企业使用镜像及常见操作: 搜索、下载、导出、导入、删除
docker search centos
docker pull alpine
docker images
docker save > /opt/centos.tar #centos #导出镜像
docker load -i /opt/centos.tar #导入本地镜像
docker rmi 镜像ID/镜像名称 #删除指定ID的镜像,此镜像对应容器正启动镜像不能被删除,除非将容器全部关闭
[root@DY-Ubuntu-01 ~]#docker run -it -d -p 4000:4000 docs/docker.github.io:latest   #运行docker官方文档容器(用浏览器访问http://localhost:4000/可以看到类似官网的docker文档资料)