linux安装安装docker
- 1、linux安装docker
- [2、安装docker 26.1.0](#2、安装docker 26.1.0)
- [3、配置docker systemd服务](#3、配置docker systemd服务)
- 4、使用docker
1、linux安装docker
1.1、官网安装文档
https://docs.docker.com/engine/install/binaries/
1.2、下载docker安装包
官方下载链接:https://download.docker.com/linux/static/stable/
选择最新版 26.1.0 下载
文件名:docker-26.1.0.tgz
2、安装docker 26.1.0
上传docker-26.1.0.tgz到linux服务器
bash
cd /path/to/docker-26.1.0.tgz
bash
tar -xzvf docker-26.1.0.tgz
cp docker/* /usr/bin/
至此安装完成docker
查看docker版本
bash
docker --version
3、配置docker systemd服务
bash
vi /etc/systemd/system/docker.service
插入以下内容
bash
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
为docker.service添加可执行权限
bash
chmod +x /etc/systemd/system/docker.service
重新加载docker.service配置文件,使得systemctl识别docker.service
bash
systemctl daemon-reload
配置docker.service开机自启动
bash
systemctl enable docker.service
启动docker服务
bash
systemctl start docker.service
查看docker服务
bash
systemctl status docker.service
查看docker信息
bash
docker info
得知docker默认的根目录是 /var/lib/docker
查看docker根目录的组织结构
bash
ls -l /var/lib/docker
整个根目录大小是340K
根目录下各个子目录大小如下
docker 根目录结构如下 这些?所在的地方都是空格
bash
[root@centos ~]# tree /var/lib/docker/
/var/lib/docker/
├── buildkit
│?? ├── cache.db
│?? ├── containerdmeta.db
│?? ├── content
│?? │?? └── ingest
│?? ├── executor
│?? ├── history.db
│?? ├── metadata_v2.db
│?? └── snapshots.db
├── containerd
│?? └── daemon
│?? ├── io.containerd.content.v1.content
│?? │?? └── ingest
│?? ├── io.containerd.metadata.v1.bolt
│?? │?? └── meta.db
│?? ├── io.containerd.runtime.v1.linux
│?? ├── io.containerd.runtime.v2.task
│?? ├── io.containerd.snapshotter.v1.blockfile
│?? ├── io.containerd.snapshotter.v1.native
│?? │?? └── snapshots
│?? ├── io.containerd.snapshotter.v1.overlayfs
│?? │?? └── snapshots
│?? └── tmpmounts
├── containers
├── engine-id
├── image
│?? └── overlay2
│?? ├── distribution
│?? ├── imagedb
│?? │?? ├── content
│?? │?? │?? └── sha256
│?? │?? └── metadata
│?? │?? └── sha256
│?? ├── layerdb
│?? └── repositories.json
├── network
│?? └── files
│?? └── local-kv.db
├── overlay2
│?? └── l
├── plugins
│?? ├── storage
│?? │?? └── ingest
│?? └── tmp
├── runtimes
├── swarm
├── tmp
└── volumes
├── backingFsBlockDev
└── metadata.db
39 directories, 11 files
[root@centos ~]#
4、使用docker
4.1、拉取容器镜像
以redis为例
查询redis镜像的名称
bash
docker search redis --no-trunc
拉取镜像之前
查看docker 镜像列表为空
使用docker pull redis 命令下载redis容器镜像到本地主机上,默认拉取的是最新镜像
查看redis镜像文件
bash
docker image ls
redis镜像大小是116MB
再次查看docker根目录结构
拉取redis镜像前 根目录下各个子目录大小如下
对比拉取镜像前后的docker根目录变化
发现/var/lib/docker/overlay2 从8.0K增加到了122M
说明拉取的redis镜像就存储在/var/lib/docker/overlay2
查看/var/lib/docker/overlay2 有很多奇奇怪怪的目录,这个不用管,只需要知道镜像文件存储在/var/lib/docker/overlay2 即可
/var/lib/docker/image从40K增加到了556K
查看/var/lib/docker/image 发现存放的是一些数据库信息
查看/var/lib/docker/image/overlay2/repositories.json 存放的是redis镜像信息:镜像版本、sha256等
bash
[root@centos ~]# cat /var/lib/docker/image/overlay2/repositories.json
{"Repositories":{"redis":{"redis:latest":"sha256:9509c4dd19fbb2a8abe044ab2edba261139c141ef4ebba4dcb9e0d9295431288","redis@sha256:f14f42fc7e824b93c0e2fe3cdf42f68197ee0311c3d2e0235be37480b2e208e6":"sha256:9509c4dd19fbb2a8abe044ab2edba261139c141ef4ebba4dcb9e0d9295431288"}}}[root@centos ~]#
/var/lib/docker/containers 没有变化
/var/lib/docker/containerd 从72K z增大到156K
再次拉取mysql镜像
查询mysq镜像信息 确认mysql镜像名是mysql
bash
docker search mysql --no-trunc
bash
docker pull mysql
查看mysql镜像文件
bash
docker image ls
mysql镜像大小是623MB
再次查看docker根目录结构
bash
du -sh /var/lib/docker/*
拉取redis镜像后的docker根目录结构
bash
du -sh /var/lib/docker/*
拉取mysql镜像前后 /var/lib/docker/overlay从122M增加到了763M,增大了641M(是mysql镜像占用的空间)。
/var/lib/docker/containers 还是为空 因为还没有启动任何一个容器
bash
ls -l /var/lib/docker/containers
4.2、启动容器
启动容器前查看 /var/lib/docker/containers
bash
ls -l /var/lib/docker/containers
查看容器列表
bash
docker ps -a
启动redis容器
bash
docker run --name redis --user=root --net=host --privileged --cap-add=ALL -idt --log-opt max-size=64m --log-opt max-file=3 --restart=always redis:latest
启动redis容器后查看docker根目录结构
bash
du -sh /var/lib/docker/*
启动redis容器前docker根目录结构如下
bash
du -sh /var/lib/docker/*
对比启动redis容器前后的docker根目录 发现
/var/lib/docker/overlay2从763M增大到了881M
/var/lib/docker//containers从4.0K增大到了48K
启动redis容器前/var/lib/docker//containers是空的
启动redis容器前/var/lib/docker//containers多了一个文件夹,文件夹名称是redis容器的id。这说明/var/lib/docker//containers是存放容器的目录。
查看/var/lib/docker/overlay2目录
现在启动另一个容器mysql
bash
docker run --name mysql --user=root --net=host --privileged --cap-add=ALL -idt -e MYSQL_ROOT_PASSWORD=root --log-opt max-size=64m --log-opt max-file=3 --restart=always mysql:latest
启动mysql容器前/var/lib/docker//containers多了一个文件夹,文件夹名称是mysql容器的id。这说明/var/lib/docker//containers是存放容器的目录。
启动mysql容器后docker根目录结构如下
bash
du -sh /var/lib/docker/*
启动redis容器前查看docker根目录结构
bash
du -sh /var/lib/docker/*
对比启动mysql容器前后的docker根目录 发现
/var/lib/docker/overlay2从881还是881M
/var/lib/docker//containers从48K增大到了100K (新增了mysql容器)
说明拉取镜像、启动容器都会在/var/lib/docker/overlay2形成一些新的目录。
4.3、删除容器
删除容器
[root@centos ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
747e3dff416e mysql:latest "docker-entrypoint.s..." 10 minutes ago Restarting (1) 38 seconds ago mysql
a68fd1736512 redis:latest "docker-entrypoint.s..." 24 minutes ago Up 24 minutes redis
[root@centos ~]# docker stop mysql
mysql
[root@centos ~]# ls -l /var/lib/docker/containers
total 8
drwx--x---. 4 root root 4096 May 2 01:21 747e3dff416eab1a77ad7f661ebe2d2ad07f5d8e659d0e8573bb30c7dc3cd07d
drwx--x---. 4 root root 4096 May 2 00:57 a68fd17365126889fac3708724c61c1f2232965a5ec88722be4241f48685b8fb
[root@centos ~]# ls -l /var/lib/docker/overlay2/
total 92
drwx--x---. 4 root root 4096 May 2 00:14 051f04f8522bbf90076730d769b2d4a7d0570d144c29ecfebe9866e4dc0498d1
drwx--x---. 3 root root 4096 May 2 00:11 16e87bc225000d8c90584f4ab7316d843c1e7453921e6975fe002f97438b3e5a
drwx--x---. 4 root root 4096 May 2 01:10 22a31f446d2f26ed2a09328cfd1ec792d24c380a83b4801dcb1a7e4b918b5c6b
drwx--x---. 4 root root 4096 May 2 00:11 2a083f4c0b6ad97428b269d88caf06f491c187be25f040a302c79e616e33e4b9
drwx--x---. 4 root root 4096 May 1 23:26 46e6175768ba9ac9f48bdb014202ecc817e8804541747154af78a1f8b7efcaa8
drwx--x---. 4 root root 4096 May 1 23:26 4f63eba842282f2de15fcc43a7fbf0df75b74edeb62ce4e895adeab4d8c2b617
drwx--x---. 4 root root 4096 May 2 00:57 5160ebddec114e69dbbf37de2fcf4de990ff288779312769449bcebbb769183e
drwx--x---. 4 root root 4096 May 1 23:26 5a995c8ca94e2faee40c88de953070aa1ebb0f2ce661051e95d6dc2460dbb0dc
drwx--x---. 4 root root 4096 May 2 01:20 5b2efedb1427d6bef9018474aaa4d56417c7831086f0d78d1abb6effae32db8d
drwx--x---. 4 root root 4096 May 2 01:10 5b2efedb1427d6bef9018474aaa4d56417c7831086f0d78d1abb6effae32db8d-init
drwx--x---. 4 root root 4096 May 2 00:17 5bc1e2b92200829b9ac1443c4bd426edd926eec77f4ddf2cd890b23404b5b1ee
drwx--x---. 4 root root 4096 May 2 00:11 60b5706d8e1a11020a168677d5c4aaf538f150b1c07a900d79c89bb612d4ec9a
drwx--x---. 4 root root 4096 May 1 23:26 7f8ecf81a239830129e3136662af7c2968566213f1a26b9d805d506572a2dddf
drwx--x---. 4 root root 4096 May 1 23:26 8154da6399ac4e7249f95756ce2525ac18aea2c070a9038953a0ba042652bba9
drwx--x---. 4 root root 4096 May 2 00:11 83908ab8b7eb55b217a26858b0503258ac34e493604ec375998046f073564fce
drwx--x---. 5 root root 4096 May 2 00:57 90331d82f14c0dd702e5a8d73f2a2a90168ee196ab9834f04862c148359c8e80
drwx--x---. 4 root root 4096 May 2 00:57 90331d82f14c0dd702e5a8d73f2a2a90168ee196ab9834f04862c148359c8e80-init
drwx--x---. 4 root root 4096 May 2 00:11 bd543abdfac1d431d7f45f50a6b7d10c0e52441f5f7c1dff34258d501d978ce5
drwx--x---. 3 root root 4096 May 1 23:26 c88a9c32a71d1e85af673e7646a383d1e8441fe5fcf78fb00bc6d471ac06e08c
drwx--x---. 4 root root 4096 May 2 00:14 cead6d3dd7ca3dbd5c61677b00d63f1ae76acfa129088ba7b63e2fa439de4bf1
drwx--x---. 4 root root 4096 May 2 00:17 cecf5c0e4a6a53ab3d76801b79e864b5e3e367baa6b4af148835170f944b9120
drwx--x---. 4 root root 4096 May 1 23:26 d792b07968b864588cc4bcceff1be6d59f5a0125f6a37d8d8632adb9922c6fed
drwx------. 2 root root 4096 May 2 01:10 l
[root@centos ~]# docker stop mysql
mysql
[root@centos ~]# ls -l /var/lib/docker/containers
total 8
drwx--x---. 4 root root 4096 May 2 01:21 747e3dff416eab1a77ad7f661ebe2d2ad07f5d8e659d0e8573bb30c7dc3cd07d
drwx--x---. 4 root root 4096 May 2 00:57 a68fd17365126889fac3708724c61c1f2232965a5ec88722be4241f48685b8fb
[root@centos ~]# ls -l /var/lib/docker/overlay2/
total 92
drwx--x---. 4 root root 4096 May 2 00:14 051f04f8522bbf90076730d769b2d4a7d0570d144c29ecfebe9866e4dc0498d1
drwx--x---. 3 root root 4096 May 2 00:11 16e87bc225000d8c90584f4ab7316d843c1e7453921e6975fe002f97438b3e5a
drwx--x---. 4 root root 4096 May 2 01:10 22a31f446d2f26ed2a09328cfd1ec792d24c380a83b4801dcb1a7e4b918b5c6b
drwx--x---. 4 root root 4096 May 2 00:11 2a083f4c0b6ad97428b269d88caf06f491c187be25f040a302c79e616e33e4b9
drwx--x---. 4 root root 4096 May 1 23:26 46e6175768ba9ac9f48bdb014202ecc817e8804541747154af78a1f8b7efcaa8
drwx--x---. 4 root root 4096 May 1 23:26 4f63eba842282f2de15fcc43a7fbf0df75b74edeb62ce4e895adeab4d8c2b617
drwx--x---. 4 root root 4096 May 2 00:57 5160ebddec114e69dbbf37de2fcf4de990ff288779312769449bcebbb769183e
drwx--x---. 4 root root 4096 May 1 23:26 5a995c8ca94e2faee40c88de953070aa1ebb0f2ce661051e95d6dc2460dbb0dc
drwx--x---. 4 root root 4096 May 2 01:20 5b2efedb1427d6bef9018474aaa4d56417c7831086f0d78d1abb6effae32db8d
drwx--x---. 4 root root 4096 May 2 01:10 5b2efedb1427d6bef9018474aaa4d56417c7831086f0d78d1abb6effae32db8d-init
drwx--x---. 4 root root 4096 May 2 00:17 5bc1e2b92200829b9ac1443c4bd426edd926eec77f4ddf2cd890b23404b5b1ee
drwx--x---. 4 root root 4096 May 2 00:11 60b5706d8e1a11020a168677d5c4aaf538f150b1c07a900d79c89bb612d4ec9a
drwx--x---. 4 root root 4096 May 1 23:26 7f8ecf81a239830129e3136662af7c2968566213f1a26b9d805d506572a2dddf
drwx--x---. 4 root root 4096 May 1 23:26 8154da6399ac4e7249f95756ce2525ac18aea2c070a9038953a0ba042652bba9
drwx--x---. 4 root root 4096 May 2 00:11 83908ab8b7eb55b217a26858b0503258ac34e493604ec375998046f073564fce
drwx--x---. 5 root root 4096 May 2 00:57 90331d82f14c0dd702e5a8d73f2a2a90168ee196ab9834f04862c148359c8e80
drwx--x---. 4 root root 4096 May 2 00:57 90331d82f14c0dd702e5a8d73f2a2a90168ee196ab9834f04862c148359c8e80-init
drwx--x---. 4 root root 4096 May 2 00:11 bd543abdfac1d431d7f45f50a6b7d10c0e52441f5f7c1dff34258d501d978ce5
drwx--x---. 3 root root 4096 May 1 23:26 c88a9c32a71d1e85af673e7646a383d1e8441fe5fcf78fb00bc6d471ac06e08c
drwx--x---. 4 root root 4096 May 2 00:14 cead6d3dd7ca3dbd5c61677b00d63f1ae76acfa129088ba7b63e2fa439de4bf1
drwx--x---. 4 root root 4096 May 2 00:17 cecf5c0e4a6a53ab3d76801b79e864b5e3e367baa6b4af148835170f944b9120
drwx--x---. 4 root root 4096 May 1 23:26 d792b07968b864588cc4bcceff1be6d59f5a0125f6a37d8d8632adb9922c6fed
drwx------. 2 root root 4096 May 2 01:10 l
[root@centos ~]# docker rm mysql
mysql
[root@centos ~]# ls -l /var/lib/docker/containers
total 4
drwx--x---. 4 root root 4096 May 2 00:57 a68fd17365126889fac3708724c61c1f2232965a5ec88722be4241f48685b8fb
[root@centos ~]# ls -l /var/lib/docker/overlay2/
total 84
drwx--x---. 4 root root 4096 May 2 00:14 051f04f8522bbf90076730d769b2d4a7d0570d144c29ecfebe9866e4dc0498d1
drwx--x---. 3 root root 4096 May 2 00:11 16e87bc225000d8c90584f4ab7316d843c1e7453921e6975fe002f97438b3e5a
drwx--x---. 4 root root 4096 May 2 01:10 22a31f446d2f26ed2a09328cfd1ec792d24c380a83b4801dcb1a7e4b918b5c6b
drwx--x---. 4 root root 4096 May 2 00:11 2a083f4c0b6ad97428b269d88caf06f491c187be25f040a302c79e616e33e4b9
drwx--x---. 4 root root 4096 May 1 23:26 46e6175768ba9ac9f48bdb014202ecc817e8804541747154af78a1f8b7efcaa8
drwx--x---. 4 root root 4096 May 1 23:26 4f63eba842282f2de15fcc43a7fbf0df75b74edeb62ce4e895adeab4d8c2b617
drwx--x---. 4 root root 4096 May 2 00:57 5160ebddec114e69dbbf37de2fcf4de990ff288779312769449bcebbb769183e
drwx--x---. 4 root root 4096 May 1 23:26 5a995c8ca94e2faee40c88de953070aa1ebb0f2ce661051e95d6dc2460dbb0dc
drwx--x---. 4 root root 4096 May 2 00:17 5bc1e2b92200829b9ac1443c4bd426edd926eec77f4ddf2cd890b23404b5b1ee
drwx--x---. 4 root root 4096 May 2 00:11 60b5706d8e1a11020a168677d5c4aaf538f150b1c07a900d79c89bb612d4ec9a
drwx--x---. 4 root root 4096 May 1 23:26 7f8ecf81a239830129e3136662af7c2968566213f1a26b9d805d506572a2dddf
drwx--x---. 4 root root 4096 May 1 23:26 8154da6399ac4e7249f95756ce2525ac18aea2c070a9038953a0ba042652bba9
drwx--x---. 4 root root 4096 May 2 00:11 83908ab8b7eb55b217a26858b0503258ac34e493604ec375998046f073564fce
drwx--x---. 5 root root 4096 May 2 00:57 90331d82f14c0dd702e5a8d73f2a2a90168ee196ab9834f04862c148359c8e80
drwx--x---. 4 root root 4096 May 2 00:57 90331d82f14c0dd702e5a8d73f2a2a90168ee196ab9834f04862c148359c8e80-init
drwx--x---. 4 root root 4096 May 2 00:11 bd543abdfac1d431d7f45f50a6b7d10c0e52441f5f7c1dff34258d501d978ce5
drwx--x---. 3 root root 4096 May 1 23:26 c88a9c32a71d1e85af673e7646a383d1e8441fe5fcf78fb00bc6d471ac06e08c
drwx--x---. 4 root root 4096 May 2 00:14 cead6d3dd7ca3dbd5c61677b00d63f1ae76acfa129088ba7b63e2fa439de4bf1
drwx--x---. 4 root root 4096 May 2 00:17 cecf5c0e4a6a53ab3d76801b79e864b5e3e367baa6b4af148835170f944b9120
drwx--x---. 4 root root 4096 May 1 23:26 d792b07968b864588cc4bcceff1be6d59f5a0125f6a37d8d8632adb9922c6fed
drwx------. 2 root root 4096 May 2 01:22 l
[root@centos ~]# docker stop redis
redis
[root@centos ~]#
[root@centos ~]# docker rm redis
redis
[root@centos ~]# ls -l /var/lib/docker/container
ls: cannot access /var/lib/docker/container: No such file or directory
[root@centos ~]# ls -l /var/lib/docker/containers/
total 0
[root@centos ~]# ls -l /var/lib/docker/overlay2/
total 76
drwx--x---. 4 root root 4096 May 2 00:14 051f04f8522bbf90076730d769b2d4a7d0570d144c29ecfebe9866e4dc0498d1
drwx--x---. 3 root root 4096 May 2 00:11 16e87bc225000d8c90584f4ab7316d843c1e7453921e6975fe002f97438b3e5a
drwx--x---. 4 root root 4096 May 2 01:10 22a31f446d2f26ed2a09328cfd1ec792d24c380a83b4801dcb1a7e4b918b5c6b
drwx--x---. 4 root root 4096 May 2 00:11 2a083f4c0b6ad97428b269d88caf06f491c187be25f040a302c79e616e33e4b9
drwx--x---. 4 root root 4096 May 1 23:26 46e6175768ba9ac9f48bdb014202ecc817e8804541747154af78a1f8b7efcaa8
drwx--x---. 4 root root 4096 May 1 23:26 4f63eba842282f2de15fcc43a7fbf0df75b74edeb62ce4e895adeab4d8c2b617
drwx--x---. 4 root root 4096 May 2 00:57 5160ebddec114e69dbbf37de2fcf4de990ff288779312769449bcebbb769183e
drwx--x---. 4 root root 4096 May 1 23:26 5a995c8ca94e2faee40c88de953070aa1ebb0f2ce661051e95d6dc2460dbb0dc
drwx--x---. 4 root root 4096 May 2 00:17 5bc1e2b92200829b9ac1443c4bd426edd926eec77f4ddf2cd890b23404b5b1ee
drwx--x---. 4 root root 4096 May 2 00:11 60b5706d8e1a11020a168677d5c4aaf538f150b1c07a900d79c89bb612d4ec9a
drwx--x---. 4 root root 4096 May 1 23:26 7f8ecf81a239830129e3136662af7c2968566213f1a26b9d805d506572a2dddf
drwx--x---. 4 root root 4096 May 1 23:26 8154da6399ac4e7249f95756ce2525ac18aea2c070a9038953a0ba042652bba9
drwx--x---. 4 root root 4096 May 2 00:11 83908ab8b7eb55b217a26858b0503258ac34e493604ec375998046f073564fce
drwx--x---. 4 root root 4096 May 2 00:11 bd543abdfac1d431d7f45f50a6b7d10c0e52441f5f7c1dff34258d501d978ce5
drwx--x---. 3 root root 4096 May 1 23:26 c88a9c32a71d1e85af673e7646a383d1e8441fe5fcf78fb00bc6d471ac06e08c
drwx--x---. 4 root root 4096 May 2 00:14 cead6d3dd7ca3dbd5c61677b00d63f1ae76acfa129088ba7b63e2fa439de4bf1
drwx--x---. 4 root root 4096 May 2 00:17 cecf5c0e4a6a53ab3d76801b79e864b5e3e367baa6b4af148835170f944b9120
drwx--x---. 4 root root 4096 May 1 23:26 d792b07968b864588cc4bcceff1be6d59f5a0125f6a37d8d8632adb9922c6fed
drwx------. 2 root root 4096 May 2 01:25 l
[root@centos ~]#
删除一个容器会删除/var/lib/docker/containers下对应容器id名称的目录。并且会删除/var/lib/docker/overlay2/下的和这个容器相关的目录。
删除一个容器也会减小/var/lib/docker/containers、/var/lib/docker/overlay2的目录大小
删除容器前的docker根目录存储大小占用情况
删除容器后的docker根目录存储大小占用情况
4.4、重命名镜像
查看当前镜像tag是latest,不是具体的版本号,怎么重名为具体的版本号呢?
bash
docker image ls
使用docker tag命令或者docker image tag命令重命名镜像名称和tag
这两个命令是完全一样的
bash
docker tag --help
这里使用docker image tag 重命名镜像名称和tag,新的镜像和原镜像大小、id是完全一样的,只是名称和tag不同而已。相当于做了一个硬链接。
bash
docker image tag mysql:latest mysql:8.4.0
docker image tag redis:latest redis:7.2.4
然后删除原来tag为latest的镜像即可
bash
docker image rm redis:latest mysql:latest
4.5、删除镜像
bash
[root@centos ~]# du -sh /var/lib/docker/*
112K /var/lib/docker/buildkit
228K /var/lib/docker/containerd
4.0K /var/lib/docker/containers
4.0K /var/lib/docker/engine-id
2.4M /var/lib/docker/image
56K /var/lib/docker/network
763M /var/lib/docker/overlay2
16K /var/lib/docker/plugins
4.0K /var/lib/docker/runtimes
4.0K /var/lib/docker/swarm
4.0K /var/lib/docker/tmp
200M /var/lib/docker/volumes
[root@centos ~]#
[root@centos ~]# ls -l /var/lib/docker/overlay2/
total 76
drwx--x---. 4 root root 4096 May 2 02:11 051f04f8522bbf90076730d769b2d4a7d0570d144c29ecfebe9866e4dc0498d1
drwx--x---. 3 root root 4096 May 2 00:11 16e87bc225000d8c90584f4ab7316d843c1e7453921e6975fe002f97438b3e5a
drwx--x---. 4 root root 4096 May 2 02:11 22a31f446d2f26ed2a09328cfd1ec792d24c380a83b4801dcb1a7e4b918b5c6b
drwx--x---. 4 root root 4096 May 2 02:11 2a083f4c0b6ad97428b269d88caf06f491c187be25f040a302c79e616e33e4b9
drwx--x---. 4 root root 4096 May 2 02:12 46e6175768ba9ac9f48bdb014202ecc817e8804541747154af78a1f8b7efcaa8
drwx--x---. 4 root root 4096 May 2 02:12 4f63eba842282f2de15fcc43a7fbf0df75b74edeb62ce4e895adeab4d8c2b617
drwx--x---. 4 root root 4096 May 2 02:12 5160ebddec114e69dbbf37de2fcf4de990ff288779312769449bcebbb769183e
drwx--x---. 4 root root 4096 May 2 02:12 5a995c8ca94e2faee40c88de953070aa1ebb0f2ce661051e95d6dc2460dbb0dc
drwx--x---. 4 root root 4096 May 2 02:11 5bc1e2b92200829b9ac1443c4bd426edd926eec77f4ddf2cd890b23404b5b1ee
drwx--x---. 4 root root 4096 May 2 02:11 60b5706d8e1a11020a168677d5c4aaf538f150b1c07a900d79c89bb612d4ec9a
drwx--x---. 4 root root 4096 May 2 02:12 7f8ecf81a239830129e3136662af7c2968566213f1a26b9d805d506572a2dddf
drwx--x---. 4 root root 4096 May 2 02:12 8154da6399ac4e7249f95756ce2525ac18aea2c070a9038953a0ba042652bba9
drwx--x---. 4 root root 4096 May 2 02:11 83908ab8b7eb55b217a26858b0503258ac34e493604ec375998046f073564fce
drwx--x---. 4 root root 4096 May 2 02:11 bd543abdfac1d431d7f45f50a6b7d10c0e52441f5f7c1dff34258d501d978ce5
drwx--x---. 3 root root 4096 May 1 23:26 c88a9c32a71d1e85af673e7646a383d1e8441fe5fcf78fb00bc6d471ac06e08c
drwx--x---. 4 root root 4096 May 2 02:11 cead6d3dd7ca3dbd5c61677b00d63f1ae76acfa129088ba7b63e2fa439de4bf1
drwx--x---. 4 root root 4096 May 2 02:11 cecf5c0e4a6a53ab3d76801b79e864b5e3e367baa6b4af148835170f944b9120
drwx--x---. 4 root root 4096 May 2 02:12 d792b07968b864588cc4bcceff1be6d59f5a0125f6a37d8d8632adb9922c6fed
drwx------. 2 root root 4096 May 2 01:51 l
[root@centos ~]# ls -l /var/lib/docker/containers/
total 0
[root@centos ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 8.4.0 8251f0669c6e 29 hours ago 623MB
redis 7.2.4 9509c4dd19fb 3 weeks ago 116MB
[root@centos ~]# docker image rm redis
Error response from daemon: No such image: redis:latest
[root@centos ~]# docker image rm redis:7.2.4
Untagged: redis:7.2.4
Untagged: redis@sha256:f14f42fc7e824b93c0e2fe3cdf42f68197ee0311c3d2e0235be37480b2e208e6
Deleted: sha256:9509c4dd19fbb2a8abe044ab2edba261139c141ef4ebba4dcb9e0d9295431288
Deleted: sha256:08158bf97226c72de797d93cba059e9b024ff9cd9ae16c1be6e1cc6e025e7e7b
Deleted: sha256:905cbb12ee4fb342bf517fc781b2fcf2cee86c6a81fefc9f9880faa7cd422e78
Deleted: sha256:feeaa10bb9d23c60c249b231387a923e40d97477f0f9ee3a668e969ca212853b
Deleted: sha256:8344113b1909c55aa392b0a36652ce7853ce7999a41509417c243c17b9525b52
Deleted: sha256:e2e329299aeac9cc193544b50bbc2150f76af843991efd9a747a8c1ec2a934bd
Deleted: sha256:da5bc51517f018bb4d94e9fc178ab191fb65773844dd08ee027aebfcfa75f097
Deleted: sha256:bd82f177af4db81e497fc51862c45cff1fd1c630fec7ca5edb10d0419d16c570
Deleted: sha256:52ec5a4316fadc09a4a51f82b8d7b66ead0d71bea4f75e81e25b4094c4219061
[root@centos ~]# du -sh /var/lib/docker/*
112K /var/lib/docker/buildkit
212K /var/lib/docker/containerd
4.0K /var/lib/docker/containers
4.0K /var/lib/docker/engine-id
1.9M /var/lib/docker/image
56K /var/lib/docker/network
641M /var/lib/docker/overlay2
16K /var/lib/docker/plugins
4.0K /var/lib/docker/runtimes
4.0K /var/lib/docker/swarm
4.0K /var/lib/docker/tmp
200M /var/lib/docker/volumes
[root@centos ~]# ls -l /var/lib/docker/overlay2/
total 44
drwx--x---. 4 root root 4096 May 2 02:11 051f04f8522bbf90076730d769b2d4a7d0570d144c29ecfebe9866e4dc0498d1
drwx--x---. 3 root root 4096 May 2 00:11 16e87bc225000d8c90584f4ab7316d843c1e7453921e6975fe002f97438b3e5a
drwx--x---. 4 root root 4096 May 2 02:11 22a31f446d2f26ed2a09328cfd1ec792d24c380a83b4801dcb1a7e4b918b5c6b
drwx--x---. 4 root root 4096 May 2 02:11 2a083f4c0b6ad97428b269d88caf06f491c187be25f040a302c79e616e33e4b9
drwx--x---. 4 root root 4096 May 2 02:11 5bc1e2b92200829b9ac1443c4bd426edd926eec77f4ddf2cd890b23404b5b1ee
drwx--x---. 4 root root 4096 May 2 02:11 60b5706d8e1a11020a168677d5c4aaf538f150b1c07a900d79c89bb612d4ec9a
drwx--x---. 4 root root 4096 May 2 02:11 83908ab8b7eb55b217a26858b0503258ac34e493604ec375998046f073564fce
drwx--x---. 4 root root 4096 May 2 02:11 bd543abdfac1d431d7f45f50a6b7d10c0e52441f5f7c1dff34258d501d978ce5
drwx--x---. 4 root root 4096 May 2 02:11 cead6d3dd7ca3dbd5c61677b00d63f1ae76acfa129088ba7b63e2fa439de4bf1
drwx--x---. 4 root root 4096 May 2 02:11 cecf5c0e4a6a53ab3d76801b79e864b5e3e367baa6b4af148835170f944b9120
drwx------. 2 root root 4096 May 2 02:15 l
[root@centos ~]# ls -l /var/lib/docker/container
ls: cannot access /var/lib/docker/container: No such file or directory
[root@centos ~]# ls -l /var/lib/docker/containers/
total 0
[root@centos ~]#
[root@centos ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 8.4.0 8251f0669c6e 30 hours ago 623MB
[root@centos ~]#
[root@centos ~]# docker image rm mysql:8.4.0
Untagged: mysql:8.4.0
Deleted: sha256:8251f0669c6ec890f73deaaf7a7891d8f0cd80821d89b9122b950b679a331c05
Deleted: sha256:4673e27ac2ab194c2dc1ecf55e23c41ac6af43dea4fe25b746925d8ee3c37e11
Deleted: sha256:a5c6e746b0b00ae5639a4f0658087c57dee0a8870fd1617e604ab7a8b9e159f6
Deleted: sha256:b676ae90710384f4c68d2fa5382b26b6c03b3f280e8ce27c71eda9ba9791c4bf
Deleted: sha256:cf7779972ca581e80127084a52348dc0284c60e3afe93784d63f2473ca010a65
Deleted: sha256:41380ccfda7b5fc874e5f021938991acbbb0ae3551872df8d005d6ce43cb4349
Deleted: sha256:af3c8076b9b5268e4393601dc5f7d360f734919fcd588fed8300a604eb8c52f5
Deleted: sha256:fa043954b572513d0c09a10335f265d544c87e1311bb83b92f4da948c0292f3a
Deleted: sha256:9ebed9fdb8d2ee58c18ca09ca0569b5c89cfa8f52c825245233e717fbb38d79f
Deleted: sha256:bc6c00d4305b5e85504d372f4505e660275c2a41ef76ecf9bab27b1c2e54ef09
Deleted: sha256:fc037c17567de912528326b53c7296230319e5f20ff6639b8dfc6cd9bfda3724
[root@centos ~]#
[root@centos ~]# du -sh /var/lib/docker/*
112K /var/lib/docker/buildkit
204K /var/lib/docker/containerd
4.0K /var/lib/docker/containers
4.0K /var/lib/docker/engine-id
212K /var/lib/docker/image
56K /var/lib/docker/network
8.0K /var/lib/docker/overlay2
16K /var/lib/docker/plugins
4.0K /var/lib/docker/runtimes
4.0K /var/lib/docker/swarm
4.0K /var/lib/docker/tmp
200M /var/lib/docker/volumes
[root@centos ~]#
[root@centos ~]# ls -l /var/lib/docker/overlay2/
total 4
drwx------. 2 root root 4096 May 2 02:15 l
[root@centos ~]# ls -l /var/lib/docker/containers
total 0
[root@centos ~]#
删除一个镜像会删除/var/lib/docker/overlay2/下的和这个镜像相关的目录。
删除一个镜像也会减小/var/lib/docker/overlay2的目录大小
删除redis、mysql镜像前的docker根目录存储大小占用情况
删除redis、mysql镜像后的docker根目录存储大小占用情况