Featured image of post Linux 安装 Docker 教程

Linux 安装 Docker 教程

Debian、Ubuntu、Centos安装Docker教程

前言

网上安装 Docker 的教程很多,为什么还要自己写一篇呢?因为质量高低不一,有的是过时的,有的甚至是野路子。为了安装之后能够省心,所以这篇文章是基于 Docker 官方文档涉及多个不同的平台(Debian、Ubuntu、Centos)的安装步骤。

主要涉及 Debian、Ubuntu、Centos 安装 Docker 的教程。

Debian 安装 Docker

前置条件

系统要求:

为了安装 Docker,你需要 64 位的 Ubuntu 版本:

  • Debian Bookworm 12 (stable)
  • Debian Bullseye 11 (oldstable)

如果你之前安装过 Docker,在安装 Docker 之前,需要卸载任何冲突的包。

运行以下命令卸载所有冲突的包:

1
for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove $pkg; done

卸载 Docker 时,/var/lib/Docker/ 中存储的镜像、容器、卷和网络不会自动删除。如果希望从干净的安装开始,并希望清理所有现有的数据,请参考卸载 Docker 步骤。

开始安装 Docker

首次在新主机上安装 Docker 之前,需要设置 Docker 存储库。然后,您可以从存储库安装和更新 Docker。

设置 Docker 的 apt 存储库

复制以下命令到终端粘贴并回车运行

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

注意:如果使用的是衍生发行版,比如 Kali Linux,您可能需要替换这个命令中打印版本代码的部分:

1
(. /etc/os-release && echo "$VERSION_CODENAME")

将此部分替换为相应的 Debian 版本的代号,如 bookworm。

安装 Docker 软件包

安装最新版本,运行:

1
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

如果要安装特定版本的 Docker,从列出存储库中的可用版本开始:

1
2
3
4
5
6
# List the available versions:
apt-cache madison docker-ce | awk '{ print $3 }'

5:25.0.0-1~debian.12~bookworm
5:24.0.7-1~debian.12~bookworm
...

选择所需的版本并安装:

1
2
VERSION_STRING=5:25.0.0-1~debian.12~bookworm
sudo apt-get install docker-ce=$VERSION_STRING docker-ce-cli=$VERSION_STRING containerd.io docker-buildx-plugin docker-compose-plugin

通过运行 hello-world 镜像来验证 Docker 安装是否成功:

1
sudo docker run hello-world

此命令下载测试镜像并在容器中运行它。当容器运行时,它将打印一条确认消息并退出。

比如:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
leo@mini-linux:~$ sudo docker run hello-world
[sudo] leo 的密码:

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (arm64v8)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

如果验证通过,那么现在已经成功地安装并启动了 Docker。

卸载 Docker

卸载 Docker Engine、 CLI、 Container 和 Docker Compose 包:

1
sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras

删除所有镜像、容器和卷:

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

Ubuntu 安装 Docker

前置条件

系统要求:

为了安装 Docker,你需要 64 位的 Ubuntu 版本:

  • Ubuntu Mantic 23.10
  • Ubuntu Lunar 23.04
  • Ubuntu Jammy 22.04 (LTS)
  • Ubuntu Focal 20.04 (LTS)

如果你之前安装过 Docker,在安装 Docker 之前,需要卸载任何冲突的包。

运行以下命令卸载所有冲突的包:

1
for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done

卸载 Docker 时,/var/lib/Docker/ 中存储的镜像、容器、卷和网络不会自动删除。如果希望从干净的安装开始,并希望清理所有现有的数据,请参考卸载 Docker 步骤。

开始安装 Docker

首次在新主机上安装 Docker 之前,需要设置 Docker 存储库。然后,您可以从存储库安装和更新 Docker。

设置 Docker 的 apt 存储库

复制以下命令到终端粘贴并回车运行

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

# Add the repository to Apt sources:
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" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

注意:如果使用的是 Ubuntu 派生发行版,比如 Linux Mint,您可能需要使用 UBUNTU _ CODENAME 而不是 VERION _ CODENAME。

安装 Docker 软件包

安装最新版本,运行:

1
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

如果要安装特定版本的 Docker,从列出存储库中的可用版本开始:

1
2
3
4
5
6
# List the available versions:
apt-cache madison docker-ce | awk '{ print $3 }'

5:24.0.0-1~ubuntu.22.04~jammy
5:23.0.6-1~ubuntu.22.04~jammy
...

选择所需的版本并安装:

1
2
VERSION_STRING=5:24.0.0-1~ubuntu.22.04~jammy
sudo apt-get install docker-ce=$VERSION_STRING docker-ce-cli=$VERSION_STRING containerd.io docker-buildx-plugin docker-compose-plugin

通过运行 hello-world 镜像来验证 Docker 安装是否成功:

1
sudo docker run hello-world

此命令下载测试镜像并在容器中运行它。当容器运行时,它将打印一条确认消息并退出。

比如:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
leo@mini-linux:~$ sudo docker run hello-world
[sudo] leo 的密码:

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (arm64v8)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

如果验证通过,那么现在已经成功地安装并启动了 Docker。

卸载 Docker

卸载 Docker Engine、 CLI、 Container 和 Docker Compose 包:

1
sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras

删除所有镜像、容器和卷:

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

Centos 安装 Docker

前置条件

系统要求:

要安装 Docker ,需要以下 CentOS 版本之一的维护版本:

  • CentOS 7
  • CentOS 8 (stream)
  • CentOS 9 (stream)

必须启用 centos-excas 存储库。默认情况下启用此存储库。如果您已经禁用了它,则需要重新启用它。

如果你之前安装过 Docker,旧版本的 Docker 使用的是 Docker 或 Docker-engine。在尝试安装新版本之前卸载任何这样的旧版本以及相关的依赖项。

运行以下命令卸载 Docker:

1
2
3
4
5
6
7
8
sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

卸载 Docker 时,/var/lib/Docker/ 中存储的镜像、容器、卷和网络不会自动删除。如果希望从干净的安装开始,并希望清理所有现有的数据,请参考卸载 Docker 步骤。

开始安装 Docker

首次在新主机上安装 Docker 之前,需要设置 Docker 存储库。然后,您可以从存储库安装和更新 Docker。

设置 Docker 的 rpm 存储库

安装 yum-utils 包(它提供了 yum-config-manager 实用程序)并设置存储库。

复制以下命令到终端粘贴并回车运行

1
2
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

安装 Docker 软件包

安装最新版本,运行:

1
sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

如果提示接受 GPG 密钥,请验证指纹是否匹配,然后接受指纹。

这个命令安装了 Docker,但是没有启动 Docker。它还创建了一个 docker 组,但是默认情况下它不会将任何用户添加到组中。

如果要安装特定版本的 Docker,首先从列出存储库中的可用版本开始:

1
2
3
4
5
yum list docker-ce --showduplicates | sort -r

docker-ce.x86_64    3:25.0.0-1.el8    docker-ce-stable
docker-ce.x86_64    3:24.0.7-1.el8    docker-ce-stable
<...>

选择所需的版本并安装:

1
2
3
4
5
VERSION_STRING=3:25.0.0-1.el8
sudo yum install docker-ce-$<VERSION_STRING> docker-ce-cli-$<VERSION_STRING> containerd.io docker-buildx-plugin docker-compose-plugin

# 或者直接指定对应版本
sudo yum install docker-ce-3:25.0.0-1.el8 docker-ce-cli-3:25.0.0-1.el8 containerd.io docker-buildx-plugin docker-compose-plugin

启动 Docker:

1
sudo systemctl start docker

通过运行 hello-world 镜像来验证 Docker 安装是否成功:

1
sudo docker run hello-world

此命令下载测试镜像并在容器中运行它。当容器运行时,它将打印一条确认消息并退出。

比如:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
leo@mini-linux:~$ sudo docker run hello-world
[sudo] leo 的密码:

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (arm64v8)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

如果验证通过,那么现在已经成功地安装并启动了 Docker。

卸载 Docker

卸载 Docker Engine、 CLI、 Container 和 Docker Compose 包:

1
sudo yum remove docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras

删除所有镜像、容器和卷:

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

UOS 安装 Docker

这里讲解一下 UOS/Deepin 如何安装 Docker,适用于 UOS 家庭版/专业版/教育版和 Deepin

因为 UOS 家庭版/专业版/教育版和 Deepin 20.x 版是基于 Debian 10 开发的,所以我们可以按照 Docker 官方文档 使用 Debian 10 的方式安装,也就是本文 Debian 安装 Docker 的方法。

主要区别就是修改这行:

1
echo "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian  buster stable" |  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

$(. /etc/os-release && echo "$VERSION_CODENAME") stable 改为了 buster stable,其他的是一致的。

Licensed under CC BY-NC-SA 4.0
最后更新于 Jan 20, 2024 22:12 +0800