Ubuntu 24.04 安装 Docker 指南
在 Ubuntu 24.04 (noble) 上通过阿里云 docker-ce 源在线安装,以及离线 deb 包、离线静态二进制包安装 Docker CE 29.8.2,并配置国内镜像加速、systemd 管理。所有命令与回显均为 Ubuntu 24.04 实测结果
Ubuntu 24.04 安装 Docker 指南
目标:在 Ubuntu 24.04(noble)上通过阿里云 docker-ce 源在线安装,以及离线 deb 包、离线静态二进制包安装 Docker CE 29.8.2,并配置国内镜像加速、使用 systemd 管理服务。所有命令与回显均为 Ubuntu 24.04 实测结果。
一、环境信息
- 操作系统:Ubuntu 24.04.5 LTS(
VERSION_CODENAME=noble,代号 Noble Numbat) - 架构:amd64
- 包管理器:apt 2.8.3 / dpkg 1.22.6
- Docker:Docker CE
29.8.2(docker-ce 5:29.8.2-1ubuntu.24.04noble) - containerd:containerd.io
2.3.6 - 辅助插件:docker-compose-plugin
v5.6.0、docker-buildx-plugin0.37.1
二、工具概览
Docker 由三部分组成:
| 组件 | 包名 | 说明 |
|---|---|---|
| Docker 引擎与 CLI | docker-ce、docker-ce-cli | 守护进程 dockerd 与命令行客户端 docker |
| 运行时 | containerd.io | 底层运行环境,管理镜像与生命周期 |
| 插件 | docker-buildx-plugin、docker-compose-plugin | 构建与编排能力 |
三、在线安装(阿里云源)
3.1 刷新软件源
apt-get update
Get:1 http://security.ubuntu.com/ubuntu noble-security InRelease [126 kB]
Hit:2 http://archive.ubuntu.com/ubuntu noble InRelease
...
Fetched 4118 kB in 5s (882 kB/s)
Reading package lists...
3.2 安装依赖并导入 Docker GPG 密钥
apt-get install -y apt-transport-https ca-certificates curl gnupg
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
chmod a+r /etc/apt/keyrings/docker.gpg
3.3 添加阿里云 docker-ce 源(Ubuntu 24.04 用 noble 通道)
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu noble stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
apt-get update
deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu noble stable
Get:3 https://mirrors.aliyun.com/docker-ce/linux/ubuntu noble/stable amd64 Packages [82.0 kB]
...
Fetched 130 kB in 1s (88.0 kB/s)
Reading package lists...
3.4 安装 Docker
apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Setting up docker-ce (5:29.8.2-1~ubuntu.24.04~noble) ...
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
Created symlink /etc/systemd/system/sockets.target.wants/docker.socket → /usr/lib/systemd/system/docker.socket.
Setting up docker-ce-rootless-extras (5:29.8.2-1~ubuntu.24.04~noble) ...
Processing triggers for libc-bin (2.39-0ubuntu8.9) ...
3.5 查看版本
docker --version
docker compose version
Docker version 29.8.2, build 7fc2dff
Docker Compose version v5.6.0
四、离线安装(deb 包、静态 tgz)
4.1 离线 deb 包安装
在联网机器上下载 deb 包(实测下载 docker-ce 24.4 MB、docker-ce-cli 17.5 MB、containerd.io 等):
mkdir -p /var/cache/docker-debs && cd /var/cache/docker-debs
apt-get download docker-ce docker-ce-cli containerd.io
Get:2 https://mirrors.aliyun.com/docker-ce/linux/ubuntu noble/stable amd64 docker-ce-cli amd64 5:29.8.2-1~ubuntu.24.04~noble [17.5 MB]
Get:3 https://mirrors.aliyun.com/docker-ce/linux/ubuntu noble/stable amd64 docker-ce amd64 5:29.8.2-1~ubuntu.24.04~noble [24.4 MB]
Fetched 65.1 MB in 1min 44s (624 kB/s)
W: Download is performed unsandboxed as root as file '/var/cache/docker-debs/containerd.io_2.3.6-1~ubuntu.24.04~noble_amd64.deb' couldn't be accessed by user '_apt'. - pkgAcquire::Run (13: Permission denied)
💡 需把依赖闭包一并下载(
iptables、nftables等),否则离线机dpkg -i会因依赖缺失而配置失败。把 deb 拷入离线机后执行:
cd /path/to/debs
dpkg -i *.deb
apt-get install -f
⚠️ 实测:直接
dpkg -i时报依赖缺失属正常现象,apt-get install -f补齐依赖后即可完成配置。
4.2 离线静态二进制包安装(tgz)
Docker 官方静态包内含 dockerd、docker、containerd、runc、ctr 等,解压到 /usr/bin 即可运行。实测阿里云镜像站静态通道已提供 docker-29.7.2.tgz(82 MB;29.8.2 暂未同步,返回 404,可按需选择已有版本):
# 1. 下载静态包(联网机器上获取后拷入)
curl -fsSLo docker.tgz https://mirrors.aliyun.com/docker-ce/linux/static/stable/x86_64/docker-29.7.2.tgz
# 2. 解压并拷到 /usr/bin
tar xzf docker.tgz
cp docker/* /usr/bin/
# 3. 静态包不含 iptables/nftables,真实服务器需自带;缺失时先安装
apt-get install -y iptables nftables
# 4. 启动(无 systemd 环境可 nohup 后台起)
nohup dockerd > /var/log/dockerd.log 2>&1 &
# 或配置 systemd 后:systemctl enable docker 与 systemctl start docker 分别执行
# 5. 验证
docker --version
-rw-r--r-- 1 root root 82M Oct 7 06:43 /tmp/docker-static.tgz
docker/
docker/ctr
docker/docker-init
docker/containerd
docker/docker
docker/runc
五、配置国内镜像加速与 systemd 管理
5.1 配置镜像加速
默认的 Docker Hub 国内访问缓慢,写入 /etc/docker/daemon.json:
mkdir -p /etc/docker
cat > /etc/docker/daemon.json <<'EOF'
{
"registry-mirrors": ["https://docker.1ms.run", "https://docker.m.daocloud.io"]
}
EOF
systemctl restart docker
确认加速生效:
docker info | grep -A1 "Registry Mirrors"
Registry Mirrors:
https://docker.1ms.run/
5.2 systemd 管理
systemctl enable docker
systemctl start docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
Created symlink /etc/systemd/system/sockets.target.wants/docker.socket → /usr/lib/systemd/system/docker.socket.
⚠️ 需要 systemd 的真实服务器(本文在无 systemd 环境中以
nohup dockerd后台方式验证,命令与真实服务器一致)。
5.3 无 systemd 环境下手动启动 dockerd
kill $(cat /var/run/docker.pid) 2>/dev/null; sleep 2
nohup dockerd > /var/log/dockerd.log 2>&1 &
sleep 10
docker info | grep "Server Version"
Server Version: 29.8.2
六、验证运行
docker run --rm hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
...
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
当前用户加入 docker 组(免 sudo 调用):
usermod -aG docker $USER
💡 加入后需重新登录(或执行
newgrp docker)使权限生效。
七、常见问题与真实行为
-
拉取镜像超时/连接被拒:宿主机到 Docker Hub(registry-1.docker.io、index.docker.io)网络不稳定,必须配置镜像加速后才能拉取,见第五节。
-
overlay2 挂载失败(模拟/特殊内核环境):若 dockerd 日志出现
failed to mount overlay: invalid argument,可在 daemon.json 配置"storage-driver":"vfs"解决。真实物理机默认使用 overlay2,无需处理。 -
静态二进制包启动报
iptables not found:静态包不含网络工具,装iptables nftables或dockerd --iptables=false启动。 -
离线 dpkg 报依赖缺失:
docker-ce依赖iptables/nftables等,离线需把依赖闭包一起携带,并执行apt-get install -f修复。 -
get.docker.com连接被重置:国内网络常见,改用本文阿里云源方案即可。
八、卸载方式
systemctl stop docker
apt-get remove -y docker-ce docker-ce-cli containerd.io
rm -rf /var/lib/docker /etc/docker
九、参考链接
- Docker 官方安装文档:https://docs.docker.com/engine/install/ubuntu/
- 阿里云 Docker CE 镜像源:https://mirrors.aliyun.com/docker-ce/linux/ubuntu/