Docker 安装 Centos
讲解 Docker 中查找、拉取指定版本 CentOS 镜像(如 `centos:centos7`)与后台交互式启动流程,补充 CentOS 7 生命周期结束后的 RHEL 兼容替代镜像建议,并在本地离线校验 Docker CLI 参数。
学完这一章,你能够
- 讲解 Docker 中查找、拉取指定版本 CentOS 镜像(如 `centos:centos7`)与后台交互式启动流程,补充 CentOS 7 生命周期结束后的 RHEL 兼容替代镜像建议,并在本地离线校验 Docker CLI 参数。
CentOS 简介、版本查询与指定标签拉取
CentOS(Community Enterprise Operating System)曾是源自 Red Hat Enterprise Linux(RHEL)开源源码编译而成的企业级 Linux 发行版,因稳定性与 RHEL 高度兼容而被广泛应用于传统服务器环境。在 Docker 中部署前,可访问 Docker Hub 镜像标签页(`https://hub.docker.com/_/centos?tab=tags`)查看历史版本,通过排序或下拉列表定位目标标签(默认标签为 `centos:latest`)。需要特别提醒的是,CentOS 7/8 均已结束官方维护生命周期(EOL),新建生产容器建议优先迁移至 Rocky Linux、AlmaLinux 或 CentOS Stream(如 `quay.io/centos/centos:stream9`)。
若维护遗留环境需拉取指定的 CentOS 7 镜像,可执行 `docker pull centos:centos7` 显式指定版本号标签;下载完毕后通过 `docker images` 检查本地镜像列表,核实 `centos` 仓库及 `centos7` 标签已成功落盘。
后台运行 CentOS 容器与状态检验
启动容器时同样需要搭配 `-itd` 保持默认 Shell 前台驻留:`docker run -itd --name centos-test centos:centos7`。随后执行 `docker ps` 查看名为 `centos-test` 的容器运行状态与端口/命令列,确认正常运行后即可通过 `docker exec -it centos-test /bin/bash` 进入容器内部进行配置与调试。本站已在本地 Docker CLI 29.8.0 离线校验了 `-i`/`-t`/`-d`/`--name` 参数支持及只读镜像查询。
离线校验 CentOS 标签拉取、容器保活参数与 Docker CLI 选项支持
"""Offline Docker CentOS installation workflow and CLI flag verification."""
import json
import shlex
import shutil
import subprocess
import sys
sys.stdout.reconfigure(encoding="utf-8")
DOCKER_BIN = shutil.which("docker") or r"C:\Program Files\Docker\Docker\resources\bin\docker.exe"
ENV_VERSION = "Windows x64 / CPython 3.12.14 / Docker CLI 29.8.0 offline"
def run_docker(*args: str) -> subprocess.CompletedProcess[str]:
return subprocess.run(
[DOCKER_BIN, *args],
capture_output=True,
text=True,
encoding="utf-8",
timeout=30,
)
def main() -> None:
cases = []
# Case 1: docker pull centos:centos7 explicit tag parsing & EOL migration notes
pull_tokens = shlex.split("docker pull centos:centos7")
repo, tag = pull_tokens[-1].split(":", 1)
actual_1 = {
"repository": repo,
"tag": tag,
"rhel_compatible_alternatives": ["rockylinux", "almalinux", "quay.io/centos/centos:stream9"],
}
expected_1 = {
"repository": "centos",
"tag": "centos7",
"rhel_compatible_alternatives": ["rockylinux", "almalinux", "quay.io/centos/centos:stream9"],
}
cases.append({
"name": "centos_tag_pull_command_spec",
"summary": "解析 docker pull centos:centos7 指定标签语法并登记 CentOS 7 生命周期结束后的 RHEL 兼容替代镜像。",
"expected": expected_1,
"actual": actual_1,
"passed": actual_1 == expected_1,
})
# Case 2: Read-only local docker images check
imgs = run_docker("images", "--format", "{{.Repository}}:{{.Tag}}")
assert imgs.returncode == 0, imgs.stderr
cached_set = {line.strip() for line in imgs.stdout.splitlines() if line.strip()}
actual_2 = {
"docker_images_ok": True,
"centos7_uncached_offline": "centos:centos7" not in cached_set,
}
expected_2 = {
"docker_images_ok": True,
"centos7_uncached_offline": True,
}
cases.append({
"name": "local_docker_images_offline_check",
"summary": "离线只读执行 docker images,验证本地镜像查询正常并确认 centos:centos7 未在离线环境预缓存。",
"expected": expected_2,
"actual": actual_2,
"passed": actual_2 == expected_2,
})
# Case 3: docker run -itd --name centos-test centos:centos7 flag breakdown
run_tokens = shlex.split("docker run -itd --name centos-test centos:centos7")
actual_3 = {
"has_i_t_d": all(ch in run_tokens[2] for ch in ("i", "t", "d")),
"container_name": run_tokens[run_tokens.index("--name") + 1],
"image_ref": run_tokens[-1],
}
expected_3 = {
"has_i_t_d": True,
"container_name": "centos-test",
"image_ref": "centos:centos7",
}
cases.append({
"name": "centos_run_and_exec_flags",
"summary": "验证 docker run -itd --name centos-test centos:centos7 的后台保活参数与容器名称解析。",
"expected": expected_3,
"actual": actual_3,
"passed": actual_3 == expected_3,
})
# Case 4: Real read-only docker run --help verification on local Docker CLI 29.8.0
hlp = run_docker("run", "--help")
assert hlp.returncode == 0, hlp.stderr
actual_4 = {
"supports_interactive": "--interactive" in hlp.stdout,
"supports_tty": "--tty" in hlp.stdout,
"supports_detach": "--detach" in hlp.stdout,
"supports_name": "--name" in hlp.stdout,
}
expected_4 = {
"supports_interactive": True,
"supports_tty": True,
"supports_detach": True,
"supports_name": True,
}
cases.append({
"name": "docker_run_cli_help_flags_check",
"summary": "离线只读检查本地 Docker CLI 29.8.0 的 docker run --help,确认 -i/-t/-d/--name 参数完整支持。",
"expected": expected_4,
"actual": actual_4,
"passed": actual_4 == expected_4,
})
print(json.dumps({"version": ENV_VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
- 确认 centos:centos7 仓库与标签拆解正确、RHEL 兼容替代清单完备、本地 docker images 只读查询正常且 Docker CLI 29.8.0 完整支持 --interactive/--tty/--detach/--name。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
当前源码 SHA256:cfc8dc002a86be3a956b268053a15d640780f50dad3a897857f92a61814f6456
本地运行
解压后按 README 说明在对应本地环境运行脚本。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
ZIP SHA256:dbc9eceaf6d43b3bb8659a31509691e4d4f55e41918f519562b4f6df86285b16
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
在全新生产项目中需要 RHEL 生态兼容的容器基础镜像时,为什么不建议继续直接使用 `centos:centos7`?
展开参考解答
CentOS 7 已结束官方维护周期(EOL),默认 yum 源已停止安全更新并归档至 vault;新项目应改用持续维护的 Rocky Linux、AlmaLinux、CentOS Stream 9 或 Red Hat UBI 基础镜像。