Docker基础篇02—安装与卸载

概述

本章,您将学习在 GNU/Linux 下安装与卸载 Docker。

前提条件

由前文可知,在 GNU/Linux 操作系统当中,Docker 容器引擎依赖底层的 Linux 内核(复用操作系统),换言之就是,使用者要让 Docker 真正运行起来,当前的主操作系统必须处于已启动状态。

# 查看当前 GNU/Linux 操作系统的版本信息
Shell > cat /etc/rocky-release ; uname -r

官方文档 中可以查看有关的安装信息。

Docker 采用的是 C/S 架构,因此在服务器上安装的是 Docker Engine,您可以用该容器引擎构建具体的应用程序以及运行具体的容器实例。

作者当前的操作系统为 Rocky Linux 8.10,要安装 Docker Engine,需要满足以下的前提条件:

  1. 启用 extras 存储库

    Shell > grep -E -v "^#|^$" /etc/yum.repos.d/Rocky-Extras.repo
    [extras]
    name=Rocky Linux $releasever - Extras
    mirrorlist=https://mirrors.rockylinux.org/mirrorlist?arch=$basearch&repo=extras-$releasever
    gpgcheck=1
    enabled=1
    countme=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rockyofficial
    
    Shell > dnf repolist
    repo id                             repo name
    appstream                           Rocky Linux 8 - AppStream
    baseos                              Rocky Linux 8 - BaseOS
    devel                               Rocky Linux 8 - Devel WARNING! FOR BUILDROOT AND KOJI USE
    extras                              Rocky Linux 8 - Extras
  2. 卸载旧版本的 Docker

    旧版本的 Docker 被称为 docker 或 docker-engine,如果当前操作系统安装了它们,则需要卸载它们以及相关的依赖包。

    # \ 表示内容未结束
    Shell > dnf remove docker docker-client  docker-client-latest docker-common  docker-latest  \
    docker-latest-logrotate  docker-logrotate  docker-engine

    需要注意的是,在执行上述的卸载动作之后,存储在 /var/lib/docker/ 目录中的镜像、容器、卷以及网络不会自动被移除。

安装 Docker

以下几种方式都可以安装 Docker:

  • 使用 Docker 专有存储库进行安装(推荐方法)
  • 手动下载 .rpm 包然后安装,适合那些没办法联网的机器使用
  • 脚本化安装

作者这里使用存储库进行安装,步骤如下:

# 启用并添加存储库
## 默认只启动了 docker-ce-stable  这个存储库
## 若需要启用 docker-ce-nightly 或者 docker-ce-test 存储库,请使用 vim 编辑 /etc/yum.repos.d/docker-ce.repo 文件
Shell > dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

# 生成缓存
Shell > dnf makecache

# 查询存储库信息
Shell > dnf repolist
repo id                                 repo name
appstream                               Rocky Linux 8 - AppStream
baseos                                  Rocky Linux 8 - BaseOS
devel                                   Rocky Linux 8 - Devel WARNING! FOR BUILDROOT AND KOJI USE
docker-ce-stable                        Docker CE Stable - x86_64
extras                                  Rocky Linux 8 - Extras

# 一些必要的依赖包
Shell > dnf -y install gcc gcc-c++  yum-utils

# 安装 Docker Engine 和 containerd 的最新版
## 这将会安装 Docker,但不会被启动。这将还会创建一个无任何用户的 docker 组
Shell > dnf install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

# 若需要 docker-ce 或 docker-ce-cli 或 containerd.io 的特定版本,您需要使用以下命令进行查询
## 要安装特定的版本,只需要在包名称后面加上连字符(-)后键入特定的版本,如 docker-ce-3:29.0.1-1.el8
Shell > dnf list docker-ce --showduplicates
Shell > dnf list docker-ce-cli --showduplicates
Shell > dnf list containerd.io --showduplicates

安装之后的首次启动

# 启用服务的守护进程
Shell > systemctl start docker.service

# 查看服务的状态
Shell > systemctl status docker.service

# 有些情况下使用者可能还需要将服务设置为开机自启动
Shell > systemctl enable docker.service

# 查看 docker 的版本信息
Shell > docker version
Client: Docker Engine - Community
 Version:           26.1.3
 API version:       1.45
 Go version:        go1.21.10
 Git commit:        b72abbb
 Built:             Thu May 16 08:34:39 2024
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          26.1.3
  API version:      1.45 (minimum version 1.24)
  Go version:       go1.21.10
  Git commit:       8e96db1
  Built:            Thu May 16 08:33:34 2024
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.32
  GitCommit:        8b3b7ca2e5ce38e8f31a34f35b2b68ceb8470d89
 runc:
  Version:          1.1.12
  GitCommit:        v1.1.12-0-g51d5e94
 docker-init:
  Version:          0.19.0

# 拉取 hello-world 测试镜像
Shell > docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
4f55086f7dd0: Pull complete
Digest: sha256:0e760fdfbc48ba8041e7c6db999bb40bfca508b4be580ac75d32c4e29d202ce1
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 的守护进程
Shell > systemctl stop docker.service

## 移除相关的软件包
Shell > dnf -y remove docker-ce docker-ce-cli containerd.io

## 手动删除相关目录
Shell > rm -rf /var/lib/docker/ ; rm -rf /var/lib/containerd/

使用 ansible 批量安装与卸载 Docker

在一些情况下,可能需要使用类似 ansible 这样的工具给多台机器安装 Docker,我们这里使用的是 ansible 角色。

Q:什么是角色呢?

用官方的话来说,就是层次化的结构化的组织 playbook,用来完成一些复杂的使用场景。简单来说就是将不同的类型资源(变量、tasks、handlers、模板、tags等)分门别类,放在不同的目录位置,playbook 文件之间可以通过关键字 include 互相调用。如果用编程语言思想的话来说,就是解耦(解除耦合关系),降低耦合度,增加代码的重复利用,完成复杂的事情。

批量安装 Docker

前提条件:

  • 您已经将 ansible 的主机清单文件(/etc/ansible/hosts)配置完成;
  • ansible 的主控端与被控端可互相通信
# 主控端列出当前名称为 vm 主机组的信息
Shell > ansible vm --list
  hosts (2):
    10.1.1.5
    10.1.1.6
# 创建角色需要的目录
## 众所周知,ansible 的角色需要三级目录
## 一个 roles 目录--->创建项目角色名称(二级目录,可以有多个)---> \
### 根据需要,创建第三级目录。不需要的目录可以为空目录或者不创建--->角色创建完成,使用 playbook 进行调用
Shell > mkdir -p /root/roles/docker_role/{tasks,vars,templates,files}

Shell > cd /root/roles/docker_role/tasks/

Shell > vim  main.yaml
- include: dep.yaml
- include: config_repo.yaml
- include: install.yaml
- include: start_service.yaml

Shell > vim dep.yaml
---
- name: update package
  dnf: state=latest update_only=yes
- name: install Dependency package 
  dnf: state=latest name="gcc,gcc-c++,yum-utils"
...

Shell > vim config_repo.yaml
---
- name: configure repo
  shell: "dnf config-manager --enable --add-repo https://download.docker.com/linux/centos/docker-ce.repo"
- name: make cache
  dnf: state=latest update_cache=yes 
...

Shell > vim install.yaml
---
- name: install docker
  dnf: state=latest name="docker-ce,docker-ce-cli,containerd.io"
...

Shell > vim start_service.yaml
---
- name: start docker service
  systemd: state=started name=docker.service
...

# 单独创建一个 yaml 文件,与 roles 目录位于同一个目录下。
Shell > cd /root
Shell > vim docker.yaml
---
- hosts: vm
  remote_user: root
  roles:
    - role: docker_role
...

# 测试
Shell > ansible-playbook -C docker.yaml

# 运行 playbook
Shell > ansible-playbook docker.yaml

# 查看 docker 的版本信息
Shell > ansible vm -m shell -a "docker version"

批量卸载 docker

# systemd 模块
Shell > ansible vm -m systemd -a "state=stopped name=docker.service"

# dnf 模块
Shell > ansible vm -m dnf -a 'state=absent name="docker-ce,docker-ce-cli,containerd.io"'

# file 模块
Shell > ansible vm -m file -a "state=absent path=/var/lib/dokcer/"

# file 模块
Shell > ansible vm -m file -a "state=absent path=/var/lib/containerd/"
Avatar photo

关于 陸風睿

GNU/Linux 从业者、开源爱好者、技术钻研者,撰写文档既是兴趣也是工作内容之一。Q - "281957576";WeChat - "jiulongxiaotianci",Github - https://github.com/jimcat8
用一杯咖啡支持我们,我们的每一篇[文档]都经过实际操作和精心打磨,而不是简单地从网上复制粘贴。期间投入了大量心血,只为能够真正帮助到您。
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇