Ubuntu Docker Install Script - CodeSwordsman

One-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.

Ubuntu 20.04 / 22.04 / 24.04 · Docker 27.xUpdated 2026-10-3023 commands

Preparation & Cleanup

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-architecture

Remove 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 -y

Removal 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 -y

Install dependencies: HTTPS transport, certificates, curl, gnupg and lsb-release

sudo apt update
sudo apt install -y apt-transport-https ca-certificates curl gnupg lsb-release

One-Click Install Script

Save 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 version

The 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.sh

Step-by-Step Install

Import 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.gpg

Add 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.list

Refresh the index and confirm docker-ce comes from the Aliyun repo

sudo apt update
apt-cache policy docker-ce

A 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-plugin

Verify all three work; compose is a plugin, not the old docker-compose binary

docker --version
docker compose version
docker buildx version

Post-Install Configuration

Add the user to the docker group to skip sudo; newgrp applies it immediately

sudo usermod -aG docker "$USER"
newgrp docker

The 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-pager

Set 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": ["<你的镜像加速地址>"]
}
EOF

Reload 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" }
}
EOF

Verify & Troubleshoot

Run the official test image to confirm the engine and image pulling

docker run --rm hello-world

Check these logs when the service fails; malformed daemon.json is the usual cause

systemctl status docker --no-pager
journalctl -u docker -n 50 --no-pager

Inspect 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 -v

With ufw enabled, allow the required ports in addition to Docker rules.

Uninstall & Cleanup

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-plugin
Risky

Delete all images, containers and volumes with no recovery

sudo rm -rf /var/lib/docker /var/lib/containerd

Remove 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 update

This website uses cookies to ensure you get the best experience on our website. Privacy Policy.