Ubuntu 安装 Docker

记录下 Ubuntu 安装 Docker

  • 本文环境
    • 云服务器:阿里云(不同厂商云服务器上的操作基本相同)
    • 系统:Ubuntu 22.04
    • 终端:腾讯云 OrcaTerm(腾讯云的 WebShell(个人感觉使用体验不错,因为 ui 好看),不同厂商的云服务器都可以使用,可以代替 Xshell+Xftp 使用)

本文不具体介绍 Docker
下面步骤均根据写本文时最新的官方文档

安装 docker

使用 APT 安装

首次在新的主机上安装 Docker Engine 之前,需要先设置 Docker 的软件源,之后就可以通过软件源安装和更新 Docker

1.设置 Docker 的软件源

依次执行以下命令

# 更新软件包列表
sudo apt-get update
# 安装相关依赖
sudo apt-get install ca-certificates curl gnupg
# 创建目录用于存放软件源的 GPG 密钥文件
sudo install -m 0755 -d /etc/apt/keyrings

鉴于国内网络问题,建议使用国内源,官方源请在注释中查看

# 添加软件源的 GPG 密钥(阿里镜像源)
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

# 官方源
# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

# 修改 docker.gpg 的文件权限
sudo chmod a+r /etc/apt/keyrings/docker.gpg

# 将 docker 软件源添加到 apt 源(阿里镜像源)
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# 官方源
# 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

2.安装 Docker

执行以下命令

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

使用脚本安装

Docker 在 https://get.docker.com/ 提供了一个方便的脚本,可以在开发环境中非交互式地安装Docker,不推荐在生产环境使用,但对于创建适合自己需求的配置脚本非常有用,该脚本的源代码开源在GitHub 上的 docker-install 仓库

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

这个示例会从 https://get.docker.com/ 下载脚本并运行,在 Linux 上安装最新稳定版本的 Docker

可以使用--dry-run选项运行脚本,以了解脚本在调用时会运行哪些步骤

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh ./get-docker.sh –dry-run

如果想安装最新的预发布版本,可以通过测试通道

curl -fsSL https://test.docker.com -o test-docker.sh
sudo sh test-docker.sh

查看 Docker 状态

安装完成后,Docker 服务将会自动启动,可以输入下面命令查看 Docker 服务状态

sudo systemctl status docker


输出显示 Docker 正常运行

测试 Docker 是否安装正确

sudo docker run hello-world

若能正常输出以下信息,则说明安装成功

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
719385e32844: Pull complete
Digest: sha256:c79d06dfdfd3d3eb04cafd0dc2bacab0992ebc243e083cabe208bac4dd7759e0
Status: Downloaded newer image for hello-world:latest

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.
(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 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 服务时

sudo systemctl stop docker

会提示

Warning: Stopping docker.service, but it can still be activated by:
docker.socket

此时再次查看 Docker 状态

会发现有一个TriggeredBy: docker.socket依旧在运行
实际上是 Docker 在关闭状态下被访问的自动唤醒机制,即在执行任意 Docker 命令时会自动启动
如果不希望 Docker 被自动唤醒,在停止 Docker 服务时应该依次执行下面两条命令

sudo systemctl stop docker.socket
sudo systemctl stop docker

附上启动的命令

sudo systemctl start docker

注:docker.socket也会一起跟着重启

卸载 Docker

  1. 卸载 Docker Engine, CLI, containerd 和 Docker Compose 软件包
    sudo apt-get 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
    以及相关编辑过的配置文件

非 root 用户管理 Docker

Docker 守护进程绑定Unix socket而不是 TCP 端口,默认情况下,Unix socket的所有者是 root 用户,其他用户想要访问只能使用 sudo,Docker 守护进程始终以 root 用户身份运行
出于安全考虑,一般 Linux 系统上不会直接使用 root 用户,如果希望非 root 用户能够不通过 sudo 访问,可以创建一个名为docker的 Unix 组,并将需要使用 Docker 的用户添加进用户组

创建docker

注:Ubuntu 在使用 apt 安装 Docker 时会自动创建该组

sudo groupadd docker

将用户添加到docker

# 将 $USER 修改为你要添加的用户名
sudo usermod -aG docker $USER

刷新组成员权限

重新登陆或运行以下命令

newgrp docker

测试

不使用 sudo 运行以下命令(使用添加到docker用户组的用户)

docker run hello-world

正常输出以下信息,则说明添加成功

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.
(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 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 是否安装正确时运行过该命令,则会多出拉取镜像的信息

可能出现的问题

WARNING: Error loading config file: /home/user/.docker/config.json -
stat /home/user/.docker/config.json: permission denied

原因:当前用户没有加入docker用户组
解决方法:按上述流程将该用户加入用户组,并依次执行以下命令

# 将 $USER 修改为当前用户的用户名
sudo chown "$USER":"$USER" /home/"$USER"/.docker -R
sudo chmod g+rwx "$HOME/.docker" -R