基于 scratch 构建简单镜像
1.准备一个示范用的可执行文件
1.搭建 C 编译环境
[root@host1 ~]# yum install gcc
上次元数据过期检查:21:38:04 前,执行于 2025年09月17日 星期三 21时10分30秒。
依赖关系解决。
=====================================================================================================软件包 架构 版本 仓库 大小
=====================================================================================================
安装:gcc x86_64 11.5.0-11.el9 appstream 32 M
安装依赖关系:glibc-devel x86_64 2.34-231.el9 appstream 38 kglibc-headers x86_64 2.34-231.el9 appstream 546 kkernel-headers x86_64 5.14.0-615.el9 appstream 1.8 Mlibxcrypt-devel x86_64 4.4.18-3.el9 appstream 29 kmake x86_64 1:4.3-8.el9 baseos 536 k事务概要
=====================================================================================================
安装 6 软件包总下载:35 M
安装大小:96 M
确定吗?[y/N]: y
下载软件包:
(1/6): glibc-devel-2.34-231.el9.x86_64.rpm 34 kB/s | 38 kB 00:01
(2/6): make-4.3-8.el9.x86_64.rpm 94 kB/s | 536 kB 00:05
(3/6): glibc-headers-2.34-231.el9.x86_64.rpm 118 kB/s | 546 kB 00:04
(4/6): libxcrypt-devel-4.4.18-3.el9.x86_64.rpm 181 kB/s | 29 kB 00:00
(5/6): kernel-headers-5.14.0-615.el9.x86_64.rpm 5.2 MB/s | 1.8 MB 00:00
(6/6): gcc-11.5.0-11.el9.x86_64.rpm 3.4 MB/s | 32 MB 00:09
-----------------------------------------------------------------------------------------------------
总计 3.7 MB/s | 35 MB 00:09
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务准备中 : 1/1 安装 : kernel-headers-5.14.0-615.el9.x86_64 1/6 安装 : glibc-headers-2.34-231.el9.x86_64 2/6 安装 : libxcrypt-devel-4.4.18-3.el9.x86_64 3/6 安装 : glibc-devel-2.34-231.el9.x86_64 4/6 安装 : make-1:4.3-8.el9.x86_64 5/6 安装 : gcc-11.5.0-11.el9.x86_64 6/6 运行脚本: gcc-11.5.0-11.el9.x86_64 6/6 验证 : make-1:4.3-8.el9.x86_64 1/6 验证 : gcc-11.5.0-11.el9.x86_64 2/6 验证 : glibc-devel-2.34-231.el9.x86_64 3/6 验证 : glibc-headers-2.34-231.el9.x86_64 4/6 验证 : kernel-headers-5.14.0-615.el9.x86_64 5/6 验证 : libxcrypt-devel-4.4.18-3.el9.x86_64 6/6 已安装:gcc-11.5.0-11.el9.x86_64 glibc-devel-2.34-231.el9.x86_64 glibc-headers-2.34-231.el9.x86_64 kernel-headers-5.14.0-615.el9.x86_64 libxcrypt-devel-4.4.18-3.el9.x86_64 make-1:4.3-8.el9.x86_64 完毕!
[root@host1 ~]# vi /etc/yum.repos.d/crb.repo
[root@host1 ~]# cat /etc/yum.repos.d/crb.repo
[crb]
name=CentOS-$releasever - CRB
baseurl=https://mirrors.aliyun.com/centos-stream/$releasever/CRB/$basearch/os/
gpgcheck=1
enabled=1 # 直接设置为启用(无需后续再执行 enable 命令)
gpgkey=https://mirrors.aliyun.com/centos-stream/RPM-GPG-KEY-CentOS-Official
[root@host1 ~]# yum install glibc-static
上次元数据过期检查:21:41:13 前,执行于 2025年09月17日 星期三 21时10分30秒。
未找到匹配的参数: glibc-static
错误:没有任何匹配: glibc-static
[root@host1 ~]# dnf config-manager --set-enabled crb
错误:没有匹配的仓库可以修改:crb 。
[root@host1 ~]# dnf repolist all | grep codeready-builder
[root@host1 ~]# dnf config-manager --set-enabled codeready-builder-for-centos-9-stream-rpms
错误:没有匹配的仓库可以修改:codeready-builder-for-centos-9-stream-rpms 。
[root@host1 ~]# dnf install -y centos-release-codeready-builder
上次元数据过期检查:21:43:57 前,执行于 2025年09月17日 星期三 21时10分30秒。
未找到匹配的参数: centos-release-codeready-builder
错误:没有任何匹配: centos-release-codeready-builder
[root@host1 ~]# cat /etc/centos-release
CentOS Stream release 9
[root@host1 ~]# vi /etc/yum.repos.d/crb.repo
[root@host1 ~]# cat /etc/yum.repos.d/crb.repo
[crb]
name=CentOS-$releasever - CRB
baseurl=https://mirrors.aliyun.com/centos-stream/$releasever/CRB/$basearch/os/
gpgcheck=1
enabled=1 # 直接设置为启用(无需后续再执行 enable 命令)
gpgkey=https://mirrors.aliyun.com/centos-stream/RPM-GPG-KEY-CentOS-Official
[root@host1 ~]# dnf clean all
无效配置值: enabled=1 # 直接设置为启用(无需后续再执行 enable 命令) 在 /etc/yum.repos.d/crb.repo ; 无效的布尔值 '1 # 直接设置为启用(无需后续再执行 enable 命令)'
17 个文件已删除
[root@host1 ~]# dnf makecache
无效配置值: enabled=1 # 直接设置为启用(无需后续再执行 enable 命令) 在 /etc/yum.repos.d/crb.repo ; 无效的布尔值 '1 # 直接设置为启用(无需后续再执行 enable 命令)'
CentOS Stream 9 - BaseOS (Aliyun) 3.5 MB/s | 8.8 MB 00:02
CentOS Stream 9 - AppStream (Aliyun) 8.1 MB/s | 25 MB 00:03
CentOS-9 - CRB 3.2 kB/s | 2.3 kB 00:00
Errors during downloading metadata for repository 'crb':- Status code: 404 for https://mirrors.aliyun.com/centos-stream/9/CRB/x86_64/os/repodata/repomd.xml (IP: 222.81.122.232)
错误:为仓库 'crb' 下载元数据失败 : Cannot download repomd.xml: Cannot download repodata/repomd.xml: All mirrors were tried
[root@host1 ~]# rm -f /etc/yum.repos.d/crb.repo
[root@host1 ~]# vi /etc/yum.repos.d/crb.repo
[root@host1 ~]# cat /etc/yum.repos.d/crb.repo
[crb]
name=CentOS Stream $releasever - CRB # 仓库名称
baseurl=https://mirrors.aliyun.com/centos-stream/$releasever/CRB/$basearch/os/ # 正确的阿里云CRB路径
gpgcheck=1 # 启用GPG签名验证(安全)
enabled=1 # 启用仓库(布尔值仅1/0,单独成行,无多余注释)
# GPG密钥路径(阿里云CentOS Stream官方密钥)
gpgkey=https://mirrors.aliyun.com/centos-stream/RPM-GPG-KEY-CentOS-Official
[root@host1 ~]# dnf repolist all 2>&1 | grep -i "error"
[root@host1 ~]# dnf clean all
无效配置值: gpgcheck=1 # 启用GPG签名验证(安全) 在 /etc/yum.repos.d/crb.repo 中; 无效的布尔值 '1 # 启用gpg签名验证(安全)'
无效配置值: enabled=1 # 启用仓库(布尔值仅1/0,单独成行,无多余注释) 在 /etc/yum.repos.d/crb.repo 中; 无效的布尔值 '1 # 启用仓库(布尔值仅1/0,单独成行,无多余注释)'
13 个文件已删除
[root@host1 ~]# dnf makecache
无效配置值: gpgcheck=1 # 启用GPG签名验证(安全) 在 /etc/yum.repos.d/crb.repo 中; 无效的布尔值 '1 # 启用gpg签名验证(安全)'
无效配置值: enabled=1 # 启用仓库(布尔值仅1/0,单独成行,无多余注释) 在 /etc/yum.repos.d/crb.repo 中; 无效的布尔值 '1 # 启用仓库(布尔值仅1/0,单独成行,无多余注释)'
正在等待 pid 为175178的进程退出。
CentOS Stream 9 - BaseOS (Aliyun) 5.4 kB/s | 3.9 kB 00:00
CentOS Stream 9 - AppStream (Aliyun) 6.1 kB/s | 4.4 kB 00:00
CentOS Stream 9 - CRB # 仓库名称 0.0 B/s | 0 B 00:00
错误:为仓库 'crb' 下载元数据失败 : Cannot prepare internal mirrorlist: Cannot resolve path for: "#"
[root@host1 ~]# rm -rf /etc/yum.repos.d/crb.repo
[root@host1 ~]# vi /etc/yum.repos.d/crb.repo
[root@host1 ~]# cat /etc/yum.repos.d/crb.repo
[crb]
name=CentOS Stream $releasever - CRB
baseurl=https://mirrors.aliyun.com/centos-stream/$releasever/CRB/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=https://mirrors.aliyun.com/centos-stream/RPM-GPG-KEY-CentOS-Official
[root@host1 ~]# dnf repolist all 2>&1 | grep -i "error"
[root@host1 ~]# dnf clean all
13 个文件已删除
[root@host1 ~]# dnf makecache
CentOS Stream 9 - BaseOS (Aliyun) 3.1 MB/s | 8.8 MB 00:02
CentOS Stream 9 - AppStream (Aliyun) 5.4 MB/s | 25 MB 00:04
CentOS Stream 9 - CRB 2.3 kB/s | 2.3 kB 00:00
Errors during downloading metadata for repository 'crb':- Status code: 404 for https://mirrors.aliyun.com/centos-stream/9/CRB/x86_64/os/repodata/repomd.xml (IP: 60.165.66.82)
错误:为仓库 'crb' 下载元数据失败 : Cannot download repomd.xml: Cannot download repodata/repomd.xml: All mirrors were tried
[root@host1 ~]# rm -rf /etc/yum.repos.d/crb.repo
[root@host1 ~]# vi /etc/yum.repos.d/crb.repo
[root@host1 ~]# cat /etc/yum.repos.d/crb.repo
[crb]
name=CentOS Stream $releasever - CRB
baseurl=https://vault.centos.org/centos-stream/$releasever/CRB/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=https://vault.centos.org/centos-stream/RPM-GPG-KEY-CentOS-Official
[root@host1 ~]# dnf clean all
13 个文件已删除
[root@host1 ~]# dnf makecache
CentOS Stream 9 - BaseOS (Aliyun) 3.2 MB/s | 8.8 MB 00:02
CentOS Stream 9 - AppStream (Aliyun) 4.9 MB/s | 25 MB 00:05
CentOS Stream 9 - CRB 580 B/s | 8.1 kB 00:14
Errors during downloading metadata for repository 'crb':- Status code: 404 for https://vault.centos.org/centos-stream/9/CRB/x86_64/os/repodata/repomd.xml (IP: 54.230.175.124)
错误:为仓库 'crb' 下载元数据失败 : Cannot download repomd.xml: Cannot download repodata/repomd.xml: All mirrors were tried
[root@host1 ~]# uname -m
x86_64
[root@host1 ~]# cat /etc/centos-release
CentOS Stream release 9
[root@host1 ~]# cd /tmp
[root@host1 tmp]# wget https://rpmfind.net/linux/centos-stream/9-stream/CRB/x86_64/os/Packages/glibc-static-2.34-231.el9.x86_64.rpm
--2025-09-18 19:10:41-- https://rpmfind.net/linux/centos-stream/9-stream/CRB/x86_64/os/Packages/glibc-static-2.34-231.el9.x86_64.rpm
正在解析主机 rpmfind.net (rpmfind.net)... 195.220.108.108
正在连接 rpmfind.net (rpmfind.net)|195.220.108.108|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1518258 (1.4M) [application/x-rpm]
正在保存至: “glibc-static-2.34-231.el9.x86_64.rpm”glibc-static-2.34-231.el9 100%[==================================>] 1.45M 511KB/s 用时 2.9s 2025-09-18 19:10:45 (511 KB/s) - 已保存 “glibc-static-2.34-231.el9.x86_64.rpm” [1518258/1518258])[root@host1 tmp]# rpm -ivh /tmp/glibc-static-2.34-231.el9.x86_64.rpm
错误:依赖检测失败:
libxcrypt-static(x86-64) >= 4.0.0 被 glibc-static-2.34-231.el9.x86_64 需要
[root@host1 tmp]# cd /tmp
[root@host1 tmp]# wget https://rpmfind.net/linux/centos-stream/9-stream/CRB/x86_64/os/Packages/libxcrypt-static-4.4.18-3.el9.x86_64.rpm
--2025-09-18 19:11:44-- https://rpmfind.net/linux/centos-stream/9-stream/CRB/x86_64/os/Packages/libxcrypt-static-4.4.18-3.el9.x86_64.rpm
正在解析主机 rpmfind.net (rpmfind.net)... 195.220.108.108
正在连接 rpmfind.net (rpmfind.net)|195.220.108.108|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:104769 (102K) [application/x-rpm]
正在保存至: “libxcrypt-static-4.4.18-3.el9.x86_64.rpm”libxcrypt-static-4.4.18-3 100%[==================================>] 102.31K 155KB/s 用时 0.7s 2025-09-18 19:11:47 (155 KB/s) - 已保存 “libxcrypt-static-4.4.18-3.el9.x86_64.rpm” [104769/104769])[root@host1 tmp]# rpm -ivh /tmp/libxcrypt-static-4.4.18-3.el9.x86_64.rpm
错误:依赖检测失败:
glibc-static(x86-64) >= 2.27 被 libxcrypt-static-4.4.18-3.el9.x86_64 需要
[root@host1 tmp]# rpm -ivh /tmp/libxcrypt-static-4.4.18-3.el9.x86_64.rpm /tmp/glibc-static-2.34-231.el9.x86_64.rpm
Verifying... ################################# [100%]
准备中... ################################# [100%]
正在升级/安装...1:glibc-static-2.34-231.el9 ################################# [ 50%]2:libxcrypt-static-4.4.18-3.el9 ################################# [100%]
[root@host1 tmp]# rpm -qa | grep -E "glibc-static|libxcrypt-static"
glibc-static-2.34-231.el9.x86_64
libxcrypt-static-4.4.18-3.el9.x86_64
即使一直报错也不要放弃,学会纠错才是目的!!!
2.建立一个项目目录用作镜像构建上下文,并切换到该目录
[root@host1 tmp]# cd
[root@host1 ~]# mkdir scratch-img
[root@host1 ~]# cd scratch-img
[root@host1 scratch-img]# pwd
/root/scratch-img
3.在该目录中编写一个示例 C 语言程序
[root@host1 scratch-img]# vi hello.c
[root@host1 scratch-img]# cat hello.c
#include<stdio.h>
int main(void)
{
printf("Hello,Docker!\n");
return 0;
}
4.对该源程序进行编译以生成可执行文件,然后运行测试
[root@host1 scratch-img]# gcc hello.c -static -o hello
[root@host1 scratch-img]# ./hello
Hello,Docker!
2.基于 scratch 构建镜像
1.创建 Dockerfile
[root@host1 scratch-img]# vi Dockerfile
[root@host1 scratch-img]# cat Dockerfile
FROM scratch
ADD hello /
CMD ["/hello"]
2.构建镜像
[root@host1 scratch-img]# docker build --tag hello .
[+] Building 0.2s (5/5) FINISHED docker:default=> [internal] load build definition from Dockerfile 0.0s=> => transferring dockerfile: 137B 0.0s=> [internal] load .dockerignore 0.0s=> => transferring context: 2B 0.0s=> [internal] load build context 0.1s=> => transferring context: 871.75kB 0.0s=> [1/1] ADD hello / 0.0s=> exporting to image 0.0s=> => exporting layers 0.0s=> => writing image sha256:993a96d621ddd1b761463e282a9a22662828467cb732a9553bca0a0a28ecb11e 0.0s=> => naming to docker.io/library/hello 0.0s
3.查看新构建镜像的基本信息
[root@host1 scratch-img]# docker images --filter "reference=hello"
REPOSITORY TAG IMAGE ID CREATED SIZE
hello latest 993a96d621dd About a minute ago 871kB
[root@host1 scratch-img]# ls -lh hello
-rwxr-xr-x. 1 root root 852K 9月 18 19:21 hello
4.运行这个新镜像,启动一个容器,显示的结果与二进制代码文件 hello 的内容相同
[root@host1 scratch-img]# docker run --rm hello
Hello,Docker!