docker的基本操作示例
Docker镜像的基本操作
文章目录
- Docker镜像的基本操作
- 1.docker 拉取镜像
- 2.显示镜像列表
- 2.1docker images -a 显示本地所有的镜像
- 2.2docker images --no--trunc 显示完整的镜像信息
- 2.3docker images -q 显示镜像ID
- 2.4 使用docker images --digests 将镜像的摘要值显示出来
- 2.5 基于镜像仓库名称和标签显示镜像
- 2.6 过滤要显示的镜像 就是你要找到镜像
- 2.7按指定的格式列出镜像
- 2.8 通过shell命令实现替换镜像的批量操作
- 2.9查看镜像的详细信息
- 3.设置镜像标签
- 4.查看镜像的详细信息
- 5.查看镜像的构建历史以及验证镜像分层
- 6 查找镜像
- 7 删除本地镜像
- 8.docker镜像的导入和导出
1.docker 拉取镜像
docker pull 加上拉取渡渡鸟网站的镜像
[root@localhost ~]# docker pull swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/ubuntu:jammy
jammy: Pulling from ddn-k8s/docker.io/ubuntu
762bedf4b1b7: Pull complete
Digest: sha256:162bc9174efbd590d56376172251c678c9b438d703ed82cfec8e144b1d156282
Status: Downloaded newer image for swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/ubuntu:jammy
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/ubuntu:jammy
[root@localhost ~]# docker imagaes
docker: 'imagaes' is not a docker command.
See 'docker --help'
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/ubuntu jammy 53a843653cbc 6 weeks ago 77.9MB
my_centos v1.0 5d0da3dc9764 3 years ago 231MB
[root@localhost ~]#
2.显示镜像列表
docker images 命令格式
docker images options repository[:TAG]]--
1 docker images 将显示所有顶层的镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/ubuntu jammy 53a843653cbc 6 weeks ago 77.9MB
my_centos v1.0 5d0da3dc9764 3 years ago 231MB
2.1docker images -a 显示本地所有的镜像
[root@localhost ~]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/ubuntu jammy 53a843653cbc 6 weeks ago 77.9MB
my_centos v1.0 5d0da3dc9764 3 years ago 231MB
2.2docker images --no–trunc 显示完整的镜像信息
[root@localhost ~]# docker images --no-trunc
REPOSITORY TAG IMAGE ID CREATED SIZE
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/ubuntu jammy sha256:53a843653cbcd9e10be207e951d907dc2481d9c222de57d24cfcac32e5165188 6 weeks ago 77.9MB
my_centos v1.0 sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6 3 years ago 231MB
2.3docker images -q 显示镜像ID
[root@localhost ~]# docker images -q
53a843653cbc
5d0da3dc9764
2.4 使用docker images --digests 将镜像的摘要值显示出来
[root@localhost ~]# docker images --digests
REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/ubuntu jammy sha256:162bc9174efbd590d56376172251c678c9b438d703ed82cfec8e144b1d156282 53a843653cbc 6 weeks ago 77.9MB
my_centos v1.0 <none> 5d0da3dc9764 3 years ago 231MB
2.5 基于镜像仓库名称和标签显示镜像
[root@localhost ~]# docker images 53a8 #这是镜像的ID
REPOSITORY TAG IMAGE ID CREATED SIZE
2.6 过滤要显示的镜像 就是你要找到镜像
docker images -f before 53a8
[root@localhost ~]# docker images -f before=53a8
REPOSITORY TAG IMAGE ID CREATED SIZE
my_centos v1.0 5d0da3dc9764 3 years ago 231MB
[root@localhost ~]#
2.7按指定的格式列出镜像
[root@localhost ~]# docker images --format "{{ .ID }}"
53a843653cbc
5d0da3dc9764
并且还可以用其它的替换ID 如Repository 镜像仓库名称 Tag 镜像标签 Digest 镜像摘要 CreateSince 镜像创建以来的时长 CreateAt:镜像创建的具体时间 Size镜像硬盘占用空间
2.8 通过shell命令实现替换镜像的批量操作
2.9查看镜像的详细信息
[root@localhost ~]# docker inspect --format='{{.Architecture }}' 53a8
amd64
3.设置镜像标签
docker tag 命令的格式
dockers tag SOURCE IMAGE[:TAG]TARGET IMAGE[:TAG]
[root@localhost ~]# docker tag my_centos:v1.0 iy_centos:v2.0
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/ubuntu jammy 53a843653cbc 6 weeks ago 77.9MB
iy_centos v2.0 5d0da3dc9764 3 years ago 231MB
my_centos v1.0 5d0da3dc9764 3 years ago 231MB
[root@localhost ~]#
4.查看镜像的详细信息
docker inspect 查看docker对象的详细信息
root@localhost ~]# docker inspect 5d0d #这是镜像的前四位ID
[
{
"Id": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"RepoTags": [
"iy_centos:v2.0",
"my_centos:v1.0"
],
"RepoDigests": [],
"Parent": "",
"Comment": "",
"Created": "2021-09-15T18:20:05.184694267Z",
"DockerVersion": "20.10.7",
"Author": "",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"Image": "sha256:f5b050f177fd426be8fe998a8ecf3fb1858d7e26dff4080b29a327d1bd5ba422",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20210915",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 231268856,
"GraphDriver": {
"Data": {
"MergedDir": "/var/lib/docker/overlay2/026f4ca1f327986a5041703d2cf9befcffafcbd64c276f9b31c470dfda905743/merged",
"UpperDir": "/var/lib/docker/overlay2/026f4ca1f327986a5041703d2cf9befcffafcbd64c276f9b31c470dfda905743/diff",
"WorkDir": "/var/lib/docker/overlay2/026f4ca1f327986a5041703d2cf9befcffafcbd64c276f9b31c470dfda905743/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59"
]
},
"Metadata": {
"LastTagTime": "2024-09-25T20:33:33.681308847+08:00"
}
}
]
2.使用docker insepect --f 来进行看详细镜像里的详细信息
5.查看镜像的构建历史以及验证镜像分层
docker history
[root@localhost ~]# docker history 5d0d
IMAGE CREATED CREATED BY SIZE COMMENT
5d0da3dc9764 3 years ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 3 years ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 3 years ago /bin/sh -c #(nop) ADD file:805cb5e15fb6e0bb0… 231MB
[root@localhost ~]#
6 查找镜像
docker search httpd #不管这个
7 删除本地镜像
docker rmi命令用法
docker rmi options IMAGE 一个镜像对应了多个标签 只有将最后一个标签删除 镜像才会真正的删除
7.1 docker rmi --force 表示强制删除镜像
root@localhost ~]# docker rmi --force 5d0d
Untagged: iy_centos:v2.0
Untagged: my_centos:v1.0
Deleted: sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6
7.2 docker rmi --no-prune 表示不删除没有标签的父镜像
[root@localhost ~]# docker rmi --no-prune 53a8
Untagged: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/ubuntu:jammy
Untagged: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/ubuntu@sha256:162bc9174efbd590d56376172251c678c9b438d703ed82cfec8e144b1d156282
Deleted: sha256:53a843653cbcd9e10be207e951d907dc2481d9c222de57d24cfcac32e5165188
Deleted: sha256:1b9b7346fee7abbc7f5538eaa23548bd05a45abe8daf6794024be0c8ad7d60bb
[root@localhost ~]#
7.3 docker image prune 清理未使用过的镜像 默认的这个命令 只会清理虚悬镜像 虚悬镜像是没被打标签且没有任何镜像引用的镜像
docker image prune 会删除全部虚悬镜像
[root@localhost ~]# docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
7.4 删除没有被现有容器使用的所有镜像
docker image prune -a
[root@localhost ~]# docker image prune -a
WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
[root@localhost ~]#
8.docker镜像的导入和导出
docker save 命令将镜像导出到归档文件
docker save options IMAGE
用于指定镜像归档的文件
docker save --output
[root@localhost ~]# docker save --output jwk.tar 53a8
docker load命令语法
docker load options
[root@localhost ~]# docker load --input jwk.tar
Loaded image ID: sha256:53a843653cbcd9e10be207e951d907dc2481d9c222de57d24cfcac32e5165188
[root@localhost ~]#
–output
[root@localhost ~]# docker save --output jwk.tar 53a8
docker load命令语法
docker load options
[root@localhost ~]# docker load --input jwk.tar
Loaded image ID: sha256:53a843653cbcd9e10be207e951d907dc2481d9c222de57d24cfcac32e5165188
[root@localhost ~]#