Ubuntu 安装 Docker
2024-10-08 17:22:50

卸载 Docker

卸载 Docker 及其相关组件

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

删除 Docker 创建的目录

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

安装 Docker

第 1 步:更新软件包并安装必要软件

更新软件包索引并安装添加 Docker 仓库所需的前置软件包:

sudo apt update
sudo apt install apt-transport-https curl

第 2 步:导入 Docker 官方 GPG 密钥

# 删除以前的GPG秘钥 (首次可忽略)
# rm /etc/apt/keyrings/docker.gpg

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

第 3 步:添加 Docker 官方仓库

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

第 4 步:更新软件包列表

sudo apt update

第 5 步:安装 Docker

sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  • docker-ce:Docker Engine。
  • docker-ce-cli:用于与 Docker 守护进程通信的命令行工具。
  • containerd.io:管理容器生命周期的容器运行时环境。
  • docker-buildx-plugin:增强镜像构建功能的 Docker 扩展工具,特别是在多平台构建方面。
  • docker-compose-plugin:通过单个 YAML 文件管理多容器 Docker 应用的配置管理插件。

第 6 步:检查 Docker 服务状态

sudo systemctl is-active docker
$ sudo systemctl is-active docker
active

第 7 步:运行测试容器

运行 hello-world 测试容器,验证 Docker 是否安装成功并正常工作:

sudo docker run hello-world
$ sudo docker run hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete 
Digest: sha256:91fb4b041da273d5a3273b6d587d62d518300a6ad268b28628f74997b93171b2
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/

替换 DockerHub 国内镜像源

sudo vim /etc/docker/daemon.json


{
  "registry-mirrors": [
    "https://docker.m.daocloud.io",
    "https://mirror.baidubce.com",
    "http://hub-mirror.c.163.com"
  ]
}
  • DaoCloud:https://docker.m.daocloud.io
  • 百度云镜像站:https://mirror.baidubce.com
  • 网易云镜像站: http://hub-mirror.c.163.com
  • 南京大学镜像站: https://docker.nju.edu.cn

重启 Docker 服务:保存配置文件并重启 Docker 服务以应用更改。

sudo systemctl daemon-reload
sudo systemctl restart docker

验证配置:重启 Docker 后,使用docker info命令来检查镜像源是否已经替换成功:

$ sudo docker info

Client: Docker Engine - Community
 Version:    27.3.1
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.17.1
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.29.7
    Path:     /usr/libexec/docker/cli-plugins/docker-compose

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 27.3.1
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: systemd
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 7f7fdf5fed64eb6a7caf99b3e12efcf9d60e311c
 runc version: v1.1.14-0-g2c9f560
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: builtin
  cgroupns
 Kernel Version: 6.8.0-40-generic
 Operating System: Ubuntu 22.04.4 LTS
 OSType: linux
 Architecture: x86_64
 CPUs: 8
 Total Memory: 7.71GiB
 Name: scolint
 ID: 21cf8f11-19b2-4158-a6d5-26159a718283
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Registry Mirrors:
  https://docker.m.daocloud.io/
  https://mirror.baidubce.com/
  http://hub-mirror.c.163.com/
 Live Restore Enabled: false

允许非 root 用户运行 Docker 命令

默认情况下,只有 root 用户或具有 sudo 权限的用户才能够执行 Docker 命令。如果不加sudo前缀直接运行docker命令,系统会报权限错误。

我们可以运行以下命令将当前登录的用户添加到 docker 组,以便以非 root 用户身份运行 Docker 命令:

sudo usermod -aG docker ${USER}