01_docker镜像管理:80分钟一口气学完docker+k8s!带你掌握docker+k8s所有核心知识点,全程干货,无废话!
docker镜像的实际使用学习
开发过程中,需要安装很多三方工具,比如etcd、kafka、mysql、nginx等等
1、下载安装Docker工具。
2、获取该软件的Docker镜像(基本上,都能搜索到核实的镜像),下载镜像nginx镜像,docker、pull nginx。
3、运行该镜像,启动一个容器,这个nginx服务就运行在容器中。
4、停止容器,删除该镜像,你的电脑,好像就没有使用nginx一样。
基于镜像的增删改查维护:
1、获取镜像
1、获取镜像,镜像托管仓库,好比yum源一样
默认的Docker仓库是:Dockerhub,有大量的优质镜像,以及用户自己上传的镜像CentOS容器
Docker search 镜像名:tag tag就是具体的标签版本
Dokcer search CentOS
注:1、查看docker是否启动,若有Client和Server就是启动的。
[root@Hadoop2 ~]# docker version
Client: Docker Engine - Community
Version: 24.0.6
API version: 1.41 (downgraded from 1.43)
Go version: go1.20.7
Git commit: ed223bc
Built: Mon Sep 4 12:35:25 2023
OS/Arch: linux/amd64
Context: default
Server: Docker Engine - Community
Engine:
Version: 20.10.6
API version: 1.41 (minimum version 1.12)
Go version: go1.13.15
Git commit: 8728dd2
Built: Fri Apr 9 22:43:57 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.22
GitCommit: 8165feabfdfe38c65b599c4993d227328c231fca
runc:
Version: 1.1.8
GitCommit: v1.1.8-0-g82f18fe
docker-init:
Version: 0.19.0
GitCommit: de40ad0
2、查看当前运行中的容器
root@Hadoop2 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
84544a09e63d nginx "/docker-entrypoint.…" 6 hours ago Up 6 hours 0.0.0.0:80->80/tcp, :::80->80/tcp gallant_goldberg
3、查看本地的镜像文件有那些
[root@Hadoop2 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f5a6b296b8a2 10 days ago 187MB
redis latest 39ac5829bade 11 days ago 138MB
[root@Hadoop2 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f5a6b296b8a2 10 days ago 187MB
redis latest 39ac5829bade 11 days ago 138MB
4、下载镜像
#下载docker镜像
docker pull centos #默认的是centos:latest
docker pull centos:7.8.2003
5、查看镜像的储存路径
[root@Hadoop2 ~]# docker info
Client: Docker Engine - Community
Version: 24.0.6
Context: default
Debug Mode: false
Plugins:
buildx: Docker Buildx (Docker Inc.)
Version: v0.11.2
Path: /usr/libexec/docker/cli-plugins/docker-buildx
compose: Docker Compose (Docker Inc.)
Version: v2.21.0
Path: /usr/libexec/docker/cli-plugins/docker-compose
Server:
Containers: 2
Running: 1
Paused: 0
Stopped: 1
Images: 2
Server Version: 20.10.6
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc io.containerd.runc.v2 io.containerd.runtime.v1.linux
Default Runtime: runc
Init Binary: docker-init
containerd version: 8165feabfdfe38c65b599c4993d227328c231fca
runc version: v1.1.8-0-g82f18fe
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-1160.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 8
Total Memory: 3.84GiB
Name: Hadoop2
ID: LNP5:33MY:CWU3:FMPD:EXRX:HZGK:KXM3:KHAH:CAGT:O4FV:DLXP:AGEP
Docker Root Dir: /var/lib/docker
Debug Mode: false
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
#目的地址
[root@Hadoop2 ~]# docker info | grep Root
Docker Root Dir: /var/lib/docker
6、查看存储的镜像
[root@Hadoop2 ~]# ls /var/lib/docker/image/overlay2/imagedb/content/sha256/
39ac5829badedab9c9d6915aefa8da387e423514c8c4680d8c4e0a67e2368a02 f5a6b296b8a29b4e3d89ffa99e4a86309874ae400e82b3d3993f84e1e3bb0eb9
[root@Hadoop2 ~]# ls /var/lib/docker/image/overlay2/imagedb/content/sha256/ -l
总用量 16
-rw------- 1 root root 7780 9月 14 14:27 39ac5829badedab9c9d6915aefa8da387e423514c8c4680d8c4e0a67e2368a02
-rw------- 1 root root 8147 9月 13 17:03 f5a6b296b8a29b4e3d89ffa99e4a86309874ae400e82b3d3993f84e1e3bb0eb9
[root@Hadoop2 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f5a6b296b8a2 10 days ago 187MB
redis latest 39ac5829bade 11 days ago 138MB
注:变更加速源
7、删除镜像
#下载
docker pull hello-word
#根据镜像的id、名称、摘要,被删除的镜像不得有容器记录
docker rmi hello-world
#删除容器记录
[root@Hadoop2 ~]# docker rm 4cf129075bed
4cf129075bed
#删除镜像成功
[root@Hadoop2 ~]# docker rmi hello-world:latest
Untagged: hello-world:latest
Untagged: hello-world@sha256:4f53e2564790c8e7856ec08e384732aa38dc43c52f02952483e3f003afbf23db
Deleted: sha256:9c7a54a9a43cca047013b82af109fe963fde787f63f9e016fdc3384500c2823d
Deleted: sha256:01bb4fce3eb1b56b05adf99504dafd31907a5aadac736e36b27595c8b92f07f1
#删除镜像指定前三位即可
[root@Hadoop2 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest 9c7a54a9a43c 4 months ago 13.3kB
[root@Hadoop2 ~]# docker rmi 9c7
Untagged: hello-world:latest
Untagged: hello-world@sha256:4f53e2564790c8e7856ec08e384732aa38dc43c52f02952483e3f003afbf23db
Deleted: sha256:9c7a54a9a43cca047013b82af109fe963fde787f63f9e016fdc3384500c2823d
Deleted: sha256:01bb4fce3eb1b56b05adf99504dafd31907a5aadac736e36b27595c8b92f07f
注:删除容器记录。如果没有删除容器记录直接删减镜像会报错
注:正在运行的容器和所有容器
#正在运行的容器
[root@Hadoop2 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
#所有容器
[root@Hadoop2 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
84544a09e63d nginx "/docker-entrypoint.…" 5 days ago Exited (0) 4 days ago gallant_goldberg
00d67544a872 nginx "/docker-entrypoint.…" 9 days ago Exited (255) 9 days ago 0.0.0.0:80->80/tcp, :::80->80/tcp great_mayer
[root@Hadoop2 ~]#
镜像管理:
1、批量删除镜像
#查看列出本地镜像
[root@Hadoop2 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f5a6b296b8a2 13 days ago 187MB
redis latest 39ac5829bade 2 weeks ago 138MB
centos latest 5d0da3dc9764 2 years ago 231MB
#-a 列出本地所有镜像
[root@Hadoop2 ~]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f5a6b296b8a2 13 days ago 187MB
redis latest 39ac5829bade 2 weeks ago 138MB
centos latest 5d0da3dc9764 2 years ago 231MB
#只显示镜像ID
[root@Hadoop2 ~]# docker images -aq
f5a6b296b8a2
39ac5829bade
5d0da3dc9764
#echo打印反引号中语句执行的结果
[root@Hadoop2 ~]# echo `docker images -aq`
f5a6b296b8a2 39ac5829bade 5d0da3dc9764
[root@Hadoop2 ~]#
#批量删除镜像、慎用
[root@Hadoop2 ~]# docker rmi `docker images -aq`
Untagged: redis:latest
Untagged: redis@sha256:f92a0be0ba8c085e6a5e2d2bea386365443485bcd67ced5ca8ddcdacdd4656d2
Deleted: sha256:39ac5829badedab9c9d6915aefa8da387e423514c8c4680d8c4e0a67e2368a02
Deleted: sha256:ca63b0754b94fce00f88849ca791a46d20036fdf833cec84fa828a7c5cb456e4
Deleted: sha256:701c3b8e81293857d87b169518f259edafe17e6b5e9f008f225bbe18fc55239c
Deleted: sha256:fc4c4559a037a9975d143450932206bd2ee760221a1d460fb0893611ac5dabbb
Deleted: sha256:d1bc31282da2e3624b875cb8626f45c54993aef0d4889a82616edbdd5fab012b
Deleted: sha256:3217de59d5a4ba6ec04f3b0296c7edadc6a3dd7a60a7cee3f6ae988d24f707c8
Untagged: centos:latest
Untagged: centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Deleted: sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6
Deleted: sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59
Error response from daemon: conflict: unable to delete f5a6b296b8a2 (must be forced) - image is being used by stopped container 00d67544a872
[root@Hadoop2 ~]# echo `docker images -aq`
f5a6b296b8a2
[root@Hadoop2 ~]#
2、导出镜像、导入镜像
#批量删除镜像的用法,rm
#批量删除镜像,慎用
#批量删除容器
docker rm `docker ps -a`
#1、比如默认运行的centos镜像,不提供vim功能,运行该容器后,在容器内安装vim
无vim
[root@Hadoop2 ~]# docker run -it centos bash
[root@81d951191d3d /]# vim
bash: vim: command not found
[root@81d951191d3d /]#
安装vim,若报错
[root@81d951191d3d /]# yum install vim -y
Failed to set locale, defaulting to C.UTF-8
CentOS Linux 8 - AppStream 53 B/s | 38 B 00:00
Error: Failed to download metadata for repo 'appstream': Cannot prepare internal mirrorlist: No URLs in mirrorlist
解决方法:https://developer.aliyun.com/article/1165954
安装vim
[root@81d951191d3d yum.repos.d]# yum install vim -y
检查是否安装好
[root@81d951191d3d yum.repos.d]# vim
#2、提交该镜像,再导出该镜像为压缩文件,可以发给其他人用。但是我认为是提交的一个容器记录对应的容器,作为镜像,让后启动该镜像就是一个与生成容器记录的那个一样的容器。
#3、放在容器管理篇学习
docker commit
#4、导出镜像的命令
导出镜像保存到指定位置,以压缩包形式存储
[root@Hadoop2 ~]# docker image save centos > /opt/centos
[root@Hadoop2 ~]# docker image save centos > /opt/centos.tgz
[root@Hadoop2 ~]# ls /opt/ -l
总用量 465984
-rw-r--r-- 1 root root 238581248 9月 23 13:23 centos
-rw-r--r-- 1 root root 238581248 9月 23 13:23 centos.tgz
drwx--x--x 4 root root 28 9月 12 21:08 containerd
drwxr-xr-x. 5 root root 55 6月 30 17:57 module
drwxr-xr-x. 2 root root 6 10月 31 2018 rh
drwxr-xr-x. 2 root root 108 6月 30 17:37 software
[root@Hadoop2 ~]#
导入镜像
[root@Hadoop2 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> b57d813f9e5d 41 minutes ago 559MB
nginx latest f5a6b296b8a2 2 weeks ago 187MB
[root@Hadoop2 ~]# docker image load -i /opt/centos.tgz
Loaded image: centos:latest
[root@Hadoop2 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> b57d813f9e5d 44 minutes ago 559MB
nginx latest f5a6b296b8a2 2 weeks ago 187MB
centos latest 5d0da3dc9764 2 years ago 231MB
[root@Hadoop2 ~]#
若删除镜像有依赖关系,可以通过镜像名+版本号删除
[root@Hadoop2 ~]# docker rmi 5d0da3dc9764
Error response from daemon: conflict: unable to delete 5d0da3dc9764 (cannot be forced) - image has dependent child images
[root@Hadoop2 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> b57d813f9e5d 39 minutes ago 559MB
nginx latest f5a6b296b8a2 2 weeks ago 187MB
centos latest 5d0da3dc9764 2 years ago 231MB
通过镜像名+版本号删除
[root@Hadoop2 ~]# docker rmi centos:latest
Untagged: centos:latest
Untagged: centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
[root@Hadoop2 ~]#
注:检验安装的vim
3、查看镜像的详细信息,语法:docker image inspect+镜像ID,列出的jeson信息;
[root@Hadoop2 ~]# docker image inspect 5d0da3dc9764
[
{
"Id": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"RepoTags": [
"centos:latest"
],
"RepoDigests": [],
"Parent": "",
"Comment": "",
"Created": "2021-09-15T18:20:05.184694267Z",
"Container": "9bf8a9e2ddff4c0d76a587c40239679f29c863a967f23abf7a5babb6c2121bf1",
"ContainerConfig": {
"Hostname": "9bf8a9e2ddff",
"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/sh",
"-c",
"#(nop) ",
"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"
}
},
"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,
"VirtualSize": 231268856,
"GraphDriver": {
"Data": {
"MergedDir": "/var/lib/docker/overlay2/6dddfccfb15dace43a872d1e3ff8826ddbf6e85a21069370284530793d41e55d/merged",
"UpperDir": "/var/lib/docker/overlay2/6dddfccfb15dace43a872d1e3ff8826ddbf6e85a21069370284530793d41e55d/diff",
"WorkDir": "/var/lib/docker/overlay2/6dddfccfb15dace43a872d1e3ff8826ddbf6e85a21069370284530793d41e55d/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59"
]
},
"Metadata": {
"LastTagTime": "0001-01-01T00:00:00Z"
}
}
]
[root@Hadoop2 ~]#
注:查看docker的进程的信息
查看docker进程的信息
[root@Hadoop2 ~]# docker info
Client: Docker Engine - Community
Version: 24.0.6
Context: default
Debug Mode: false
Plugins:
buildx: Docker Buildx (Docker Inc.)
Version: v0.11.2
Path: /usr/libexec/docker/cli-plugins/docker-buildx
compose: Docker Compose (Docker Inc.)
Version: v2.21.0
Path: /usr/libexec/docker/cli-plugins/docker-compose
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 3
Server Version: 20.10.6
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 8165feabfdfe38c65b599c4993d227328c231fca
runc version: v1.1.8-0-g82f18fe
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-1160.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 8
Total Memory: 3.84GiB
Name: Hadoop2
ID: LNP5:33MY:CWU3:FMPD:EXRX:HZGK:KXM3:KHAH:CAGT:O4FV:DLXP:AGEP
Docker Root Dir: /var/lib/docker
Debug Mode: false
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
[root@Hadoop2 ~]#