Confirm codename and architecture: focal, jammy and noble for 20.04, 22.04 and 24.04
lsb_release -cs
cat /etc/os-release | head -3
dpkg --print-architectureOne-click install script for Ubuntu 20.04 / 22.04 / 24.04: detects the codename and architecture, imports the GPG key and Aliyun Docker repo, installs docker-ce with the compose plugin, plus cleanup, mirror, non-root and uninstall steps.
Confirm codename and architecture: focal, jammy and noble for 20.04, 22.04 and 24.04
lsb_release -cs
cat /etc/os-release | head -3
dpkg --print-architectureRemove distro or legacy Docker to avoid containerd conflicts with docker-ce
sudo apt remove -y docker docker-engine docker.io containerd runc
sudo apt autoremove -yRemoval keeps /var/lib/docker, so images and containers survive.
Ubuntu 24.04 ships containerd; remove it on fresh installs before docker-ce
sudo apt remove -y containerd
sudo apt autoremove -yInstall dependencies: HTTPS transport, certificates, curl, gnupg and lsb-release
sudo apt update
sudo apt install -y apt-transport-https ca-certificates curl gnupg lsb-releaseSave as install-docker.sh and run bash install-docker.sh to finish the install
#!/bin/bash
set -euo pipefail
CODENAME=$(lsb_release -cs)
echo "当前系统版本代号: ${CODENAME}"
# 前置依赖
sudo apt update
sudo apt install -y apt-transport-https ca-certificates curl gnupg lsb-release
# 导入阿里云 Docker GPG 公钥
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | \
sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/docker.gpg
# 添加阿里云 Docker 软件源,架构与版本代号自动识别
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/trusted.gpg.d/docker.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu ${CODENAME} stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# 安装 Docker 引擎、Buildx 与 Compose 插件
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
# 验证
docker --version
docker compose versionThe script is re-runnable; verify the repo line in docker.list when re-running.
Make it executable and run as root to avoid repeated sudo prompts
chmod +x install-docker.sh
sudo bash install-docker.shImport the Aliyun Docker repository key into a dedicated keyring file
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/docker.gpgAdd the repository; keep arch and signed-by or apt rejects the source
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/trusted.gpg.d/docker.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.listRefresh the index and confirm docker-ce comes from the Aliyun repo
sudo apt update
apt-cache policy docker-ceA NO_PUBKEY error means the key was not imported; redo the previous step.
Install the engine, CLI, containerd, buildx and the compose plugin
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginVerify all three work; compose is a plugin, not the old docker-compose binary
docker --version
docker compose version
docker buildx versionAdd the user to the docker group to skip sudo; newgrp applies it immediately
sudo usermod -aG docker "$USER"
newgrp dockerThe docker group is effectively root-level access; grant it carefully.
Enable at boot, start now and confirm the active state
sudo systemctl enable --now docker
systemctl status docker --no-pagerSet a registry mirror from your cloud console instead of copying public links
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<'EOF'
{
"registry-mirrors": ["<你的镜像加速地址>"]
}
EOFReload and restart; the mirror should appear in docker info output
sudo systemctl daemon-reload
sudo systemctl restart docker
docker info | grep -A2 "Registry Mirrors"Cap container log size to protect the disk; merge keys into the same JSON file
sudo tee /etc/docker/daemon.json <<'EOF'
{
"log-driver": "json-file",
"log-opts": { "max-size": "100m", "max-file": "3" }
}
EOFRun the official test image to confirm the engine and image pulling
docker run --rm hello-worldCheck these logs when the service fails; malformed daemon.json is the usual cause
systemctl status docker --no-pager
journalctl -u docker -n 50 --no-pagerInspect cgroup version and storage driver; 24.04 defaults to systemd cgroup with overlay2
docker info | grep -iE "cgroup|storage driver|server version"Ubuntu 24.04 uses iptables-nft and Docker adapts; inspect NAT rules for port mapping issues
sudo iptables -t nat -L DOCKER -n -vWith ufw enabled, allow the required ports in addition to Docker rules.
Remove Docker packages while keeping data in /var/lib/docker
sudo apt purge -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginDelete all images, containers and volumes with no recovery
sudo rm -rf /var/lib/docker /var/lib/containerdRemove the repository and key so reinstalls start clean
sudo rm -f /etc/apt/sources.list.d/docker.list /etc/apt/trusted.gpg.d/docker.gpg
sudo apt updateThis website uses cookies to ensure you get the best experience on our website. Privacy Policy.