Docker架构及CentOS上安装Docker

Docker官网
https://www.docker.com/

Docker架构

Docker 使用客户端-服务器 (C/S) 架构模式,使用远程API来管理和创建Docker容器。
Docker 容器通过 Docker 镜像来创建。
容器与镜像的关系类似于面向对象编程中的对象与类:

Docker面向对象
容器对象
镜像

Docker的网站上提到了Docker的典型场景:
Automating the packaging and deployment of applications(使应用的打包与部署自动化)
Creation of lightweight, private PAAS environments(创建轻量、私密的PAAS环境)
Automated testing and continuous integration/deployment(实现自动化测试和持续的集成/部署)
Deploying and scaling web apps, databases and backend services(部署与扩展webapp、数据库和后台服务)

由于其基于LXC的轻量级虚拟化的特点,Docker相比KVM之类最明显的特点就是启动快,资源占用小。因此对于构建隔离的标准化的运行环境,轻量级的PaaS(如dokku), 构建自动化测试和持续集成环境,以及一切可以横向扩展的应用(尤其是需要快速启停来应对峰谷的web应用)。

CentOS安装Docker

第一步:安装所需的软件包、添加镜像仓库及安装docker-ce社区版,具体命令如下:

// 安装所需的软件包:
// yum-utils 提供了 yum-config-manager
// device mapper 存储驱动程序需要 device-mapper-persistent-data 和 lvm2
yum install -y yum-utils device-mapper-persistent-data lvm2

// 添加镜像仓库(阿里云)
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

// 添加镜像仓库(清华大学)
yum-config-manager --add-repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo

// 选择较快的安装源
yum makecache fast

// 安装docker-ce社区版
yum -y install docker-ce

第二步:安装完docker-ce后,启动运行及查看版本

// 启动docker
service docker start

// 查看是否安装成功
docker version

[root@iZ2zegb ~]# service docker start
Redirecting to /bin/systemctl start docker.service
[root@iZ2zegb ~]# docker version
Client: Docker Engine - Community
 Version:           20.10.18
 API version:       1.41
 Go version:        go1.18.6
 Git commit:        b40c2f6
 Built:             Thu Sep  8 23:14:08 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.18
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.18.6
  Git commit:       e42327a
  Built:            Thu Sep  8 23:12:21 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.8
  GitCommit:        9cd3357b7fd7218e4aec3eae239db1f68a5a6ec6
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
[root@iZ2zegb ~]# 

如上所示,即表示Docker安装成功。

第三步:拉取hello-world镜像并创建运行容器,具体命令如下:

// 拉取hello-world镜像
docker pull hello-world

// 创建并运行容器
docker run hello-world

[root@iZ2zegb ~]# 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/

[root@iZ2zegb ~]#

运行之后,如上所示:显示Hello from Docker!即表示容器运行成功!

Docker卸载

卸载Docker Engine, CLI, Containerd, and Docker Compose packages命令:

sudo yum remove docker-ce docker-ce-cli containerd.io docker-compose-plugin

镜像, 容器或其他自定义的配置文件不会被自动删除,需要自己手动删除,命令如下:

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

centos官方安装文档:
https://docs.docker.com/engine/install/centos/

(完)

最后修改于:2022年09月17日 13:36

添加新评论