在 设计篇: 04-Dockerfile设计原则与镜像管理规范 中,我们探讨了镜像逻辑分层策略,其中系统层 (OS Layer) 是整个体系的基石。本篇将指导你如何选择合适的 Linux 发行版作为基础,并构建一个标准化、精简且安全的系统层镜像,为后续的所有应用镜像打下坚实基础。
选择合适的基础 OS 镜像至关重要,它直接影响后续镜像的大小、安全性、稳定性和构建效率。以下是几种常见选择及其特点:
镜像 (示例标签) | 大小 (压缩后) | C 库 | 包管理器 | 主要特点 |
---|---|---|---|---|
alpine:latest | ~ 5-8 MB | musl | apk | 极度轻量 |
debian:bullseye-slim | ~ 30 MB | glibc | apt | 稳定、精简 |
ubuntu:latest | ~ 30 MB | glibc | apt | 流行、更新快 |
rockylinux:9-minimal | ~ 40 MB+ | glibc | dnf | RHEL 兼容、企业级 |
在本系列后续示例中,我们将选用 debian:bullseye-slim 作为基础系统镜像,因为它在稳定性、社区支持、体积和兼容性方面取得了较好的平衡。
我们的目标是创建一个纯净、精简的系统层镜像,只包含运行后续应用所必需的最核心组件。其他工具(如编译环境、调试工具、特定语言运行时)应放在后续的工具层或运行层。
首先,我们需要创建一个 Git 仓库来存储和管理我们的 Dockerfile:
git clone http://git.leops.local/ops/dockerfiles-base.git
cd dockerfiles-base
mkdir -p common/os/debian/bullseye-slim
cd common/os/debian/bullseye-slim
# 将 docker/dockerfile:1 同步到我们自己的镜像仓库中
# 这一步确保我们使用的构建工具也在内部可控
docker pull docker/dockerfile:1
docker tag docker/dockerfile:1 harbor.leops.local/library/docker/dockerfile:1
docker push harbor.leops.local/library/docker/dockerfile:1
下面是我们的系统层 Dockerfile,包含了必要的基础组件和配置:
#syntax=harbor.leops.local/library/docker/dockerfile:1
ARG OS_VERSION=bullseye-slim
FROM debian:$OS_VERSION
ARG OS_VERSION
LABEL org.opencontainers.image.authors="ops.leops.local" \
org.opencontainers.image.source="http://git.leops.local/ops/dockerfiles-base/common/os/debian/bullseye-slim/Dockerfile" \
org.opencontainers.image.description="Standard Debian $OS_VERSION base image for internal use."
ENV LANG='en_US.UTF-8' \
LANGUAGE='en_US.UTF-8' \
TZ='Asia/Shanghai' \
OS_VERSION=$OS_VERSION
WORKDIR /app
RUN sed -i -e 's#deb.debian.org#mirrors.aliyun.com#g' \
-e 's#security.debian.org#mirrors.aliyun.com#g' \
-e 's#ftp.debian.org#mirrors.aliyun.com#g' \
/etc/apt/sources.list \
&& apt-get update \
&& apt-get install -y --only-upgrade $(apt-get --just-print upgrade | awk 'tolower($4) ~ /.*security.*/ || tolower($5) ~ /.*security.*/ {print $2}' | sort | uniq) \
&& DEBIAN_FRONTEND=noninteractive DEBCONF_NOWARNINGS=yes apt-get install -y --no-install-recommends \
ca-certificates openssl \
tzdata wget curl procps psmisc dnsutils iproute2 telnet vim-tiny zip unzip inetutils-ping tcpdump lsof \
net-tools traceroute strace fonts-noto-cjk fontconfig locales \
&& sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen \
&& dpkg-reconfigure --frontend=noninteractive locales \
&& update-locale LANG=en_US.UTF-8 \
&& fc-cache \
&& apt-get clean \
&& rm -rf /var/cache/apt/archives/* /var/lib/apt/lists/* /tmp/* /var/tmp/* \
&& truncate -s 0 /var/log/*log \
&& ln -sf /usr/share/zoneinfo/${TZ} /etc/localtime \
&& echo ${TZ} > /etc/timezone \
&& echo 'export PS1="\e[1;34m[\e[1;33m\u@\e[1;32m\h\e[1;37m:\w\[\e[1;34m]\e[1;36m\\$ \e[0m"' >> /etc/bash.bashrc \
&& echo 'transfer(){ if [ $# -eq 0 ];then echo "No arguments specified.\nUsage:\n transfer \n ... | transfer ">&2;return 1;fi;if tty -s;then file="$1";file_name=$(basename "$file");if [ ! -e "$file" ];then echo "$file: No such file or directory">&2;return 1;fi;if [ -d "$file" ];then file_name="$file_name.zip" ,;(cd "$file"&&zip -r -q - .)|curl --progress-bar --upload-file "-" "http://transfer.leops.local/$file_name"|tee /dev/null,;else cat "$file"|curl --progress-bar --upload-file "-" "http://transfer.leops.local/$file_name"|tee /dev/null;fi;else file_name=$1;curl --progress-bar --upload-file "-" "http://transfer.leops.local/$file_name"|tee /dev/null;fi;echo;}' >> /etc/bash.bashrc
Dockerfile 最佳实践要点
首先需要配置 Docker buildx 环境,支持同时构建多架构镜像:
# 配置 buildkitd,添加镜像加速和私有仓库配置
cat << EOF >> /etc/buildkit/buildkitd.toml
[registry."harbor.leops.local"]
insecure = true
http = true
[registry."docker.io"]
mirrors = ["https://docker.1ms.run", "https://docker.1panel.live", "https://docker.1panel.dev", "https://docker.zhai.cm", "https://docker.llkk.cc", "https://a.ussh.net"]
EOF
# 创建一个支持多平台的 builder 实例
docker buildx create --name container-builder --driver docker-container --driver-opt "network=host" --buildkitd-config /etc/buildkit/buildkitd.toml --bootstrap --use
# 确认 builder 正在运行并已准备就绪
docker buildx inspect --bootstrap
创建一个构建脚本(build.sh)自动完成镜像构建和推送:
#!/bin/bash
set -e
# 配置
REGISTRY="harbor.leops.local"
IMAGE_BASE_NAME="common/os/debian"
VERSION="bullseye-20250407-slim"
# 声明镜像地址数组
declare -a IMAGE_PATHS
IMAGE_PATHS+=(
"${REGISTRY}/${IMAGE_BASE_NAME}:${VERSION}" # 完整版本
"${REGISTRY}/${IMAGE_BASE_NAME}:${VERSION%%-*}" # 主版本
"${REGISTRY}/${IMAGE_BASE_NAME}:${VERSION%%-*}-slim"# 主版本
)
# 构建镜像
build_image() {
echo "Building and pushing image:"
for img in "${IMAGE_PATHS[@]}"; do echo -n " $img"; done
docker buildx build --platform linux/amd64,linux/arm64 \
$(for img in "${IMAGE_PATHS[@]}"; do echo"-t $img "; done) \
--build-arg "OS_VERSION=${VERSION}" \
--label "OS_VERSION=${VERSION}" \
--label "org.opencontainers.image.created=$(date --rfc-3339=seconds)" \
--provenance=false \
--pull \
--push \
.
echo "Build complete."
}
# 参数处理
case "$1" in
"list-tags")
# 输出镜像标签列表
printf '%s\n'"${IMAGE_PATHS[@]}"
;;
*)
build_image
;;
esac
执行构建过程
# 确保脚本有执行权限
chmod +x build.sh
# 执行构建 (确保已登录到你的镜像仓库)
./build.sh
完成构建后,将所有文件提交到 Git 仓库进行版本控制:
git add -A .
git commit -m "feat: add debian bullseye-slim"
git push
至此,你已经成功构建了一个标准化的系统层基础镜像,它将作为你公司内部镜像体系的坚实基础。后续的所有应用镜像都可以基于此镜像进行构建,确保一致性和安全性。