为了安装并配置 Docker ,你的系统必须满足下列最低要求:64 位 Linux 或 Windows 系统 如果使用 Linux ,内核版本必须不低于 3.10 能够使用 sudo 权限的用户 在你系统 BIOS 上启用了 VT(虚拟化技术)支持 on your system BIOS(参考: 如何查看 CPU 支持 虚拟化技术(VT)) 你的系统应该联网 在 Linux ,在终端上运行以下命令验证内核以及架构详细信息:
uname -a
输出样例:
Linux Ubuntu22CT 5.15.35-3-pve #1 SMP PVE 5.15.35-6 (Fri, 17 Jun 2022 13:42:35 +0200) x86_64 x86_64 x86_64 GNU/Linux
正如上面你看到的那样,我的 Ubuntu 系统内核版本是 5.15.35-3-pve 并且系统架构是 64 位(x86_64 x86_64 x86_64 GNU/Linux)。 注意: 这里,我在 Proxmox 中使用 Ubuntu 22.04 容器。这是你看到上方内核版本中有 “pve” 字符的原因。如果你正在使用 Ubuntu 实体(或者虚拟)机,你将看到系统版本为 5.15.35-3-generic 。 内核版本需要不低于最低要求的版本,并且是 64 位机器。这样不会有任何问题,我们能顺利安装并使用 Docker 。
只要你的系统内核版本不低于 3.10 ,并且是 64 位系统,Docker 都会正常运行。
首先更新你的 Ubuntu 系统。 打开终端,依次运行下列命令:
sudo apt update sudo apt upgrade sudo apt full-upgrade
安装必要的证书并允许 apt 包管理器使用以下命令通过 HTTPS 使用存储库:
sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg lsb-release
运行下列命令添加 Docker 的官方 GPG 密钥:
curl -fsSL <https://download.docker.com/linux/ubuntu/gpg> | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
添加 Docker 官方库:
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] <https://download.docker.com/linux/ubuntu> $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
使用命令更新 Ubuntu 源列表:
sudo apt update
最后,运行下列命令在 Ubuntu 22.04 LTS 服务器中安装最新 Docker CE:
sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin
安装完成后,运行如下命令验证 Docker 服务是否在运行:
systemctl status docker
你会看到类似下面的输出:
docker.service - Docker Application Container Engine Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2022-06-27 13:07:43 UTC; 3min 4s ago TriggeredBy: * docker.socket Docs: https://docs.docker.com Main PID: 2208 (dockerd) Tasks: 8 Memory: 29.6M CPU: 126ms CGroup: /system.slice/docker.service `-2208 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock Jun 27 13:07:43 Ubuntu22CT dockerd[2208]: time="2022-06-27T13:07:43.071453522Z" level=info msg="ccResolverWrapper: sending update to cc: {[{unix:> Jun 27 13:07:43 Ubuntu22CT dockerd[2208]: time="2022-06-27T13:07:43.071459974Z" level=info msg="ClientConn switching balancer to \"pick_first\"" > Jun 27 13:07:43 Ubuntu22CT dockerd[2208]: time="2022-06-27T13:07:43.130989294Z" level=info msg="Loading containers: start." Jun 27 13:07:43 Ubuntu22CT dockerd[2208]: time="2022-06-27T13:07:43.187439756Z" level=info msg="Default bridge (docker0) is assigned with an IP a> Jun 27 13:07:43 Ubuntu22CT dockerd[2208]: time="2022-06-27T13:07:43.235966874Z" level=info msg="Loading containers: done." Jun 27 13:07:43 Ubuntu22CT dockerd[2208]: time="2022-06-27T13:07:43.240149866Z" level=warning msg="Not using native diff for overlay2, this may c> Jun 27 13:07:43 Ubuntu22CT dockerd[2208]: time="2022-06-27T13:07:43.240281966Z" level=info msg="Docker daemon" commit=a89b842 graphdriver(s)=over> Jun 27 13:07:43 Ubuntu22CT dockerd[2208]: time="2022-06-27T13:07:43.240386856Z" level=info msg="Daemon has completed initialization" Jun 27 13:07:43 Ubuntu22CT systemd[1]: Started Docker Application Container Engine. Jun 27 13:07:43 Ubuntu22CT dockerd[2208]: time="2022-06-27T13:07:43.276336600Z" level=info msg="API listen on /run/docker.sock"
好极了!Docker 服务已启动并运行!
如果没有运行,运行以下命令运行 Docker 服务:
sudo systemctl start docker
使 Docker 服务在每次重启时自动启动:
sudo systemctl enable docker
可以使用以下命令查看已安装的 Docker 版本:
sudo docker version
输出样例:
Client: Docker Engine - Community Version: 20.10.17 API version: 1.41 Go version: go1.17.11 Git commit: 100c701 Built: Mon Jun 6 23:02:46 2022 OS/Arch: linux/amd64 Context: default Experimental: true Server: Docker Engine - Community Engine: Version: 20.10.17 API version: 1.41 (minimum version 1.12) Go version: go1.17.11 Git commit: a89b842 Built: Mon Jun 6 23:00:51 2022 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.6.6 GitCommit: 10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1 runc: Version: 1.1.2 GitCommit: v1.1.2-0-ga916309 docker-init: Version: 0.19.0 GitCommit: de40ad0
让我们继续,测试 Docker 是否运行正常:
sudo docker run hello-world
上述命令会下载一个 Docker 测试镜像,并在容器内执行一个 “hello_world” 样例程序。 如果你看到类似下方的输出,那么祝贺你!Docker 正常运行在你的 Ubuntu 系统中了。
Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 2db29710123e: Pull complete Digest: sha256:13e367d31ae85359f42d637adf6da428f76d75dc9afeb3c21faea0d976f5c651 Status: Downloaded newer image for hello-world:latestHello 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. (amd64) 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 bashShare 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/
最后,如果您的服务器是ipv6服务器,需要明确您的宿主机是否已禁用IPv6。可以通过以下命令检查:
cat /proc/sys/net/ipv6/conf/all/disable_ipv6
如果输出为“1”,则表明IPv6已被禁用。
配置Docker以支持IPv6
编辑Docker守护进程配置文件: 通常位于/etc/docker/daemon.json,如果文件不存在则创建之:
sudo nano /etc/docker/daemon.json
添加以下配置以启用IPv6:
{ "ipv6": true, "fixed-cidr-v6": "2001:db8:1::/64" }
这里2001:db8:1::/64是一个示例IPv6子网,您可以根据实际情况进行替换。