官网教程在Install Docker Engine on Ubuntu | Docker Docs

一、安装

1.安装Docker Engine

要安装 Docker Engine,你需要以下 Ubuntu 版本之一的 64 位版本:

  • Ubuntu Resolute 26.04 (LTS)

  • Ubuntu Questing 25.10

  • Ubuntu Noble 24.04 (LTS)

  • Ubuntu Jammy 22.04 (LTS)

1.1 卸载旧版本

在安装 Docker Engine之前,你需要卸载所有冲突的软件包。运行以下命令卸载所有冲突的包:sudo apt remove $(dpkg --get-selections docker.io docker-compose docker-compose-v2 docker-doc podman-docker containerd runc | cut -f1) ,卸载 Docker 时,存储在 /var/lib/docker/ 中的镜像、容器、卷和网络不会自动删除。如果您想进行全新安装,并希望清理任何现有数据,请阅读卸载 Docker Engine 部分。

1.2 apt软件源安装

在新主机上首次安装 Docker 引擎之前,你需要设置 Docker apt 仓库。之后,你就可以从该仓库安装和更新 Docker。

1.2.1 设置 Docker 的 apt 存储库

Docker官方的GPG密钥和软件源地址,国内网络访问会报SSL重置,所以我们需要替换一下

# Add Docker's official GPG key:
sudo apt update
sudo apt install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
sudo tee /etc/apt/sources.list.d/docker.sources <<EOF
Types: deb
URIs: https://mirrors.aliyun.com/docker-ce/linux/ubuntu
Suites: $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}")
Components: stable
Architectures: $(dpkg --print-architecture)
Signed-By: /etc/apt/keyrings/docker.asc
EOF

sudo apt update

1.2.2 安装Docker包

要安装最新的版本,运行命令sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

#注意
#安装后,验证Docker是否正在运行
sudo systemctl status docker

#如果Docker没有运行,手动启动它
sudo systemctl start docker

1.2.3 验证

配置国内镜像加速器

#1.修改Docker配置文件
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<EOF
{
  "registry-mirrors": [
    "https://docker.m.daocloud.io",
    "https://huecker.io",
    "https://dockerhub.timeweb.cloud",
    "https://noohub.net"
  ]
}
EOF

#2.重启 Docker 服务
sudo systemctl daemon-reload
sudo systemctl restart docker

通过运行 hello-world 映像验证安装是否成功:sudo docker run hello-world

1.3 卸载Docker Engine

#1.卸载 Docker Engine、CLI、containerd 和 Docker Compose 软件包:
sudo apt purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras

#2.主机上的映像、容器、卷或自定义配置文件不会自动删除。要删除所有映像、容器和卷:
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd

#3.删除source list和keyrings
sudo rm /etc/apt/sources.list.d/docker.sources
sudo rm /etc/apt/keyrings/docker.asc

1.4 用户配置

为了避免对每个 Docker 命令都使用 sudo,请将您的用户添加到 docker 组:

sudo usermod -aG docker $USER

二、Compose使用

2.1 启动

docker compose up -d

2.2 停止

docker compose down