一文搞懂Docker与Docker Engine的安装

环境介绍:

CentOS 7.5
CentOS-extras库必须启用
Docker 19.03.8

启用centos-extras库:

[root@localhost ~]# vim /etc/yum.repos.d/CentOS-Base.repo
...
[extras]         //extras模块
name=CentOS-$releasever - Extras
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/
gpgcheck=1
enabled=1  //调整为1表示打开
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

 编辑把 “0” 修改为 “1” ,没有enabled=0或1,建议添加 enabled=1 启动 extras 软件模块仓库

卸载旧版本:

[root@localhost ~]# yum remove docker \
                               docker-client \
                               docker-client-latest \
                               docker-common \
                               docker-latest \
                               docker-latest-logrotate \
                               docker-logrotate \
                               docker-engine

安装方法:

您可以根据需要以不同的方式安装Docker Engine:

  • 大多数用户会 设置Docker的存储库并从中进行安装,以简化安装和升级任务。这是推荐的方法。

  • 一些用户下载并手动安装 RPM软件包, 并完全手动管理升级。这在诸如在无法访问互联网的空白系统上安装Docker的情况下很有用。

  • 在测试和开发环境中,一些用户选择使用自动 便利脚本来安装Docker。

使用存储库安装:

 在新主机上首次安装Docker Engine之前,需要设置Docker存储库。之后,您可以从存储库安装和更新Docker。

设置存储库

 安装yum-utils软件包(提供yum-config-manager 实用程序)并设置稳定的存储库。

[root@localhost ~]# yum install -y yum-utils  device-mapper-persistent-data  lvm2
[root@localhost ~]# yum-config-manager --add-repo \
      https://download.docker.com/linux/centos/docker-ce.repo

安装Docker引擎:

1.安装最新版本的Docker Engine和容器,或转到下一步以安装特定版本:

[root@localhost ~]# yum install docker-ce docker-ce-cli containerd.io

 如果提示您接受GPG密钥,请验证指纹是否匹配 060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35,如果是,则接受它。

 如果您启用了多个Docker存储库,则在未在yum install 或 yum update命令中指定版本的情况下进行安装或更新,将始终安装可能的最高版本。

2.要安装特定版本的Docker Engine,请在存储库中列出可用版本,然后选择并安装:

[root@localhost ~]# yum list docker-ce --showduplicates | sort -r
docker-ce.x86_64            18.03.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            18.03.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.12.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.12.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.09.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.09.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.2.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.0.ce-1.el7.centos                    

 返回的列表取决于启用了哪些存储库,并且特定于您的CentOS版本(.el7在本示例中以后缀表示)。

 通过其完全合格的软件包名称安装特定版本,该软件包名称是软件包名称(docker-ce)加上版本字符串(第二列),从第一个冒号(:)到第一个连字符,以连字符(-)分隔。例如:docker-ce-18.03.1

3.启动Docker

[root@localhost ~]# systemctl start docker

4.通过运行hello-world 映像来验证是否正确安装了Docker Engine

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

从RPM包安装:

下载:
https://download.docker.com/linux/centos/7/x86_64/stable/Packages/

[root@localhost ~]# yum install -y docker-ce-19.03.8-3.el7.x86_64.rpm

[root@localhost ~]# systemctl start docker

[root@localhost ~]# docker run hello-world

便利脚本来安装Docker:

#!/bin/bash
# 移除掉旧的版本
sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-selinux \
                  docker-engine-selinux \
                  docker-engine

# 删除所有旧的数据
sudo rm -rf /var/lib/docker

#  安装依赖包
sudo yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2

# 添加源,使用了阿里云镜像
sudo yum-config-manager \
    --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

# 配置缓存
sudo yum makecache fast

# 安装最新稳定版本的docker
sudo yum install -y docker-ce

# 配置阿里云镜像加速器 ----》(需要根据自己阿里云账户的加速地址改一下)
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
 "registry-mirrors": ["https://xxxxx.mirror.aliyuncs.com"]
}
EOF

# 启动docker引擎并设置开机启动
sudo systemctl start docker
sudo systemctl enable docker

# 配置当前用户对docker的执行权限
sudo groupadd docker
sudo gpasswd -a ${USER} docker
sudo systemctl restart docker

注意:

 国内访问docker太慢,一般会配置加速器,此处配置的加速器是阿里云的加速器也可以配置其他的加速器(如:163)。

 ---->配置阿里云Docker 镜像加速器详解 :查看

卸载Docker:

1.卸载Docker Engine,CLI和Containerd软件包:

[root@localhost ~]# yum remove docker-ce docker-ce-cli containerd.io

2.主机上的映像,容器,卷或自定义配置文件不会自动删除。要删除所有图像,容器和卷:

[root@localhost ~]# rm -rf /var/lib/docker

 必须手动删除所有已编辑的配置文件。

发布了71 篇原创文章 · 获赞 12 · 访问量 8035

猜你喜欢

转载自blog.csdn.net/qq_44895681/article/details/105540702