Docker+NETCore系列文章(二、Docker安装与卸载)

Docker安装与卸载

一、Docker的组成

镜像(image)

(1)Docker镜像就好比一个模板,可以通过该模板创建容器服务。
(2)一个镜像可以创建多个容器(服务或项目都是运行在容器中)。
(3)镜像可以将它理解为发布出来的“项目”,如.net web项目发布后的publish文件夹。
(4)镜像通过run(create+start) 命令生成容器。

容器(container)

(1)Docker利用容器技术,可独立运行一个或一组应用。
(2)容器是通过镜像来创建的。
(3)容器的环境是一个微型Linux,有最小可用的Linux内核。

仓库(repository)

(1)仓库是用于存放镜像的地方。
(2)仓库分为私有和公有仓库(思想类似git);
(3)Docker Hub(国外)。
(4)使用阿里云、腾讯云等可搭建私有仓库。
(5)可使用Harbor搭建当前Linux服务器的私有镜像仓库。
(6)镜像加速:阿里云、腾讯云、七牛云等。
在这里插入图片描述

二、安装Docker

1、设置仓库

​ (1)安装yum-utils工具包
yum-utils是管理repository(仓库)及扩展包的工具 (主要是针对repository),提供了 yum-config-manager ,并且 device mapper 存储驱动程序需要 device-mapper-persistent-data 和 lvm2。

​ (2)yum-config-manager命令是对资源库(/etc/yum.repos.d/下的文件)进行增删改查
#系统默认没有安装这个命令,这个命令在yum-utils包里,可以通过yum install -y yum-utils安装。

​ (3)sudo命令以系统管理者的身份执行指令,即通过sudo所执行的指令就好像是root亲自执行。

sudo yum install -y yum-utils device-mapper-persistent-data lvm2
2、设置稳定仓库

​ 下面是设置拉取镜像的仓库地址为阿里云开源的地址,可以加快拉去镜像的速度(官方的在国外,会慢很多)。

sudo yum-config-manager \
    --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
3、安装Docker

​ (1)列出并排序存储库中可用的docker版本。此示例按版本号(从高到低)对结果进行排序(以下截取部分结果)。

[root@VM-0-6-centos ~]# yum list docker-ce --showduplicates | sort -r
docker-ce.x86_64            3:20.10.2-3.el7                     docker-ce-stable
docker-ce.x86_64            3:20.10.1-3.el7                     docker-ce-stable
docker-ce.x86_64            3:20.10.0-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.9-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.8-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.7-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.6-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.5-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.4-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.3-3.el7                     docker-ce-stable

​ (2)安装指定版本的docker(命令:sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io)。

​ (3)上述中的<VERSION_STRING>对应第一列docker-ce.x86_64中的-ce.x86_64<VERSION_STRING>对应第二列3:20.10.2-3.el7中的**冒号(😃横杠(-)**中间的内容,也就是20.10.2。(注意:这里我用的是第(1)中的最新版本,读者的版本可能与我不同)。

#安装最新版本	推荐使用这种
sudo yum install docker-ce docker-ce-cli containerd.io
#安装指定版本
#sudo yum install docker-ce-ce.x86_64 docker-ce-cli-20.10.2 containerd.io

​ (4)执行结果如下(看到**Complete!**表示安装成功):

[root@VM-0-6-centos ~]# sudo yum install docker-ce docker-ce-cli containerd.io
Package 1:docker-ce-cli-20.10.2-3.el7.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package containerd.io.x86_64 0:1.4.3-3.1.el7 will be installed
--> Processing Dependency: container-selinux >= 2:2.74 for package: containerd.io-1.4.3-3.1.el7.x86_64
---> Package docker-ce.x86_64 3:20.10.2-3.el7 will be installed
--> Processing Dependency: docker-ce-rootless-extras for package: 3:docker-ce-20.10.2-3.el7.x86_64
--> Running transaction check
---> Package container-selinux.noarch 2:2.119.2-1.911c772.el7_8 will be installed
---> Package docker-ce-rootless-extras.x86_64 0:20.10.2-3.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===========================================================================================================================================================================================
 Package                                             Arch                             Version                                             Repository                                  Size
===========================================================================================================================================================================================
Installing:
 containerd.io                                       x86_64                           1.4.3-3.1.el7                                       docker-ce-stable                            33 M
 docker-ce                                           x86_64                           3:20.10.2-3.el7                                     docker-ce-stable                            27 M
Installing for dependencies:
 container-selinux                                   noarch                           2:2.119.2-1.911c772.el7_8                           extras                                      40 k
 docker-ce-rootless-extras                           x86_64                           20.10.2-3.el7                                       docker-ce-stable                           9.0 M

Transaction Summary
===========================================================================================================================================================================================
Install  2 Packages (+2 Dependent packages)

Total download size: 69 M
Installed size: 266 M
Is this ok [y/d/N]: y
Downloading packages:
(1/4): container-selinux-2.119.2-1.911c772.el7_8.noarch.rpm                                                                                                         |  40 kB  00:00:00     
containerd.io-1.4.3-3.1.el7.x8 FAILED                                          16% [===========-                                                         ] 9.4 kB/s |  12 MB  01:43:35 ETA 
https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.4.3-3.1.el7.x86_64.rpm: [Errno 12] Timeout on https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.4.3-3.1.el7.x86_64.rpm: (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')
Trying other mirror.
(2/4): docker-ce-rootless-extras-20.10.2-3.el7.x86_64.rpm                                                                                                           | 9.0 MB  00:01:17     
docker-ce-20.10.2-3.el7.x86_64 FAILED                                          34% [=======================                                             ]  2.0 B/s |  24 MB 6426:50:53 ETA 
https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-20.10.2-3.el7.x86_64.rpm: [Errno 12] Timeout on https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-20.10.2-3.el7.x86_64.rpm: (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')
Trying other mirror.
containerd.io-1.4.3-3.1.el7.x8 FAILED                                          
https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.4.3-3.1.el7.x86_64.rpm: [Errno 12] Timeout on https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.4.3-3.1.el7.x86_64.rpm: (28, 'Operation timed out after 30000 milliseconds with 0 out of 0 bytes received')
Trying other mirror.
docker-ce-20.10.2-3.el7.x86_64 FAILED                                          49% [==================================                                   ]  213 B/s |  34 MB  47:16:22 ETA 
https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-20.10.2-3.el7.x86_64.rpm: [Errno 12] Timeout on https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-20.10.2-3.el7.x86_64.rpm: (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')
Trying other mirror.
(3/4): containerd.io-1.4.3-3.1.el7.x86_64.rpm                                                                                                                       |  33 MB  00:00:38     
(4/4): docker-ce-20.10.2-3.el7.x86_64.rpm                                                                                                                           |  27 MB  00:00:04     
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                       41 kB/s |  69 MB  00:28:44     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 2:container-selinux-2.119.2-1.911c772.el7_8.noarch                                                                                                                      1/4 
setsebool:  SELinux is disabled.
  Installing : containerd.io-1.4.3-3.1.el7.x86_64                                                                                                                                      2/4 
  Installing : docker-ce-rootless-extras-20.10.2-3.el7.x86_64                                                                                                                          3/4 
  Installing : 3:docker-ce-20.10.2-3.el7.x86_64                                                                                                                                        4/4 
  Verifying  : 3:docker-ce-20.10.2-3.el7.x86_64                                                                                                                                        1/4 
  Verifying  : containerd.io-1.4.3-3.1.el7.x86_64                                                                                                                                      2/4 
  Verifying  : docker-ce-rootless-extras-20.10.2-3.el7.x86_64                                                                                                                          3/4 
  Verifying  : 2:container-selinux-2.119.2-1.911c772.el7_8.noarch                                                                                                                      4/4 

Installed:
  containerd.io.x86_64 0:1.4.3-3.1.el7                                                           docker-ce.x86_64 3:20.10.2-3.el7                                                          

Dependency Installed:
  container-selinux.noarch 2:2.119.2-1.911c772.el7_8                                            docker-ce-rootless-extras.x86_64 0:20.10.2-3.el7                                           

Complete!

4、启动docker服务
sudo systemctl start docker
5、查看当前docker版本
#输入docker -v查看到当前docker版本,则表示docker安装成功
#docker version查看docker详细信息
docker -v
# docker -v 执行结果
[root@VM-0-6-centos ~]# docker -v
Docker version 20.10.2, build 2291f61
# docker version 执行结果
[root@VM-0-6-centos ~]# docker version
Client: Docker Engine - Community
 Version:           20.10.2
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        2291f61
 Built:             Mon Dec 28 16:17:48 2020
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.2
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       8891c58
  Built:            Mon Dec 28 16:16:13 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.3
  GitCommit:        269548fa27e0089a8b8278fc4fc781d7f65a939b
 runc:
  Version:          1.0.0-rc92
  GitCommit:        ff819c7e9184c13b7c2607fe6c30ae19403a7aff
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
6、运行hello-world镜像

​ 看到"Hello from Docker!"表示docker环境没问题。其中hello-world是docker官方提供的测试镜像,就像学一门新语言时都会先打印hello world一样。

#运行docker镜像
sudo docker run hello-world

#执行结果如下:
[root@VM-0-6-centos ~]# sudo 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/
7、停止docker的服务

​ (1)执行systemctl stop docker命令后,docker状态会更改为:Active: inactive (dead),此时如果执行sudo docker run hello-world则会让docker恢复到运行状态。

​ (2)使用systemctl status docker可查看当前docker服务的运行状态(注:可在docker启动和停止时分别调用看看返回的结果)。

#输入以下命令停止docker服务
systemctl stop docker

#执行结果
#警告的意思是:警告:停止docker服务,但仍可以通过以下方式激活:docker.socket
[root@VM-0-6-centos ~]# systemctl stop docker
Warning: Stopping docker.service, but it can still be activated by:
  docker.socket

#查看当前docker服务的运行状态
[root@VM-0-6-centos ~]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: active (running) since Sun 2021-01-24 01:21:32 CST; 1min 33s ago
     Docs: https://docs.docker.com
 Main PID: 19705 (dockerd)
    Tasks: 10
   Memory: 38.7M
   CGroup: /system.slice/docker.service
           └─19705 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

三、卸载Docker

1、查看系统安装的docker的包
[root@VM-0-6-centos ~]# yum list installed | grep docker
containerd.io.x86_64                  1.4.3-3.1.el7                   @docker-ce-stable
docker-ce.x86_64                      3:20.10.2-3.el7                 @docker-ce-stable
docker-ce-cli.x86_64                  1:20.10.2-3.el7                 @docker-ce-stable
docker-ce-rootless-extras.x86_64      20.10.2-3.el7                   @docker-ce-stable
2、卸载docker

​ (1)依次删除docker相关包

#依次删除相关依赖包
yum -y remove docker-ce.x86_64
yum -y remove docker-ce-cli.x86_64
yum -y remove containerd.io.x86_64

​ (2)删除储存docker相关文件、镜像、容器等的目录,这些类型的文件存放在 /var/lib/docker 目录下:

rm -rf /var/lib/docker

​ (3)卸载其他与docker相关的依赖项

yum -y  remove  docker  docker-common  docker-selinux  docker-engine

四、配置镜像加速器

Docker Pull非常缓慢(使用阿里云镜像加速器)

赞赏

​ 如果您觉得文章还不错,那就请作者喝杯咖啡吧!
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值