R / Richie全部文章 ↑

Linux · 2 分钟阅读

02. 安装 Docker

2026 年默认:Linux 装 docker-ce 或直接 Podman;macOS/Windows 用 Docker Desktop 或 OrbStack。

准备工作

资源 建议
内核 4.x+,建议 5.10+
内存 ≥ 2 GB
存储 ≥ 20 GB
网络 与 registry 可达

关掉或放行防火墙:

# firewalld
systemctl disable --now firewalld    # 学习环境可以
# 或
firewall-cmd --add-masquerade --permanent
firewall-cmd --reload

# ufw
ufw allow in 2377/tcp   # swarm 模式才用

仓库配置

# RHEL / Alma / Rocky
yum install -y yum-utils
yum config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

# Ubuntu / Debian
apt update
apt install -y ca-certificates curl gnupg
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo $VERSION_CODENAME) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
apt update

安装

# RHEL
yum install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

# Ubuntu
apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

container-selinux 这种依赖 2024 年起 RHEL 9 仓库已自带,不用单独 wget。

启动

systemctl enable --now docker
docker version
docker info

镜像加速

编辑 /etc/docker/daemon.json:

{
  "registry-mirrors": [
    "https://mirror.ccs.tencentyun.com",
    "https://hub-mirror.c.163.com"
  ]
}
systemctl daemon-reload
systemctl restart docker
docker info | grep -A2 "Registry Mirrors"

镜像基础命令

docker pull nginx:1.27
docker images
docker history nginx:1.27

# 导出 / 导入
docker save nginx:1.27 > nginx.tar
docker load < nginx.tar

# 删除
docker rmi nginx:1.27
docker image prune -a    # 清理未用

macOS / Windows

  • 装 Docker Desktop(免费用于个人和小型公司,超过 250 人 / $10M 营收要付费订阅)
  • 想更轻的方案:OrbStack(macOS only)
  • 都内嵌 Linux VM,无需额外折腾

常见问题

  • 内核太老:升级或换发行版,CentOS 7 的 3.10 已经吃力
  • bridge-nf-call-iptables 报错:编辑 /etc/sysctl.d/docker.conf 加上对应项
  • 启动失败:journalctl -u docker -n 100,别瞎试

参考