七叶笔记 » golang编程 » 「Linux基础」CentOS 7安装docker20.10.6

「Linux基础」CentOS 7安装docker20.10.6

根据 docker 官方文档: Linux /docker-ce/centos/ ,在CentOS系统下安装docker,其它Linux系统请选择对应文档进行安装。

系统版本:CentOS Linux release 7.7.1908 (Core);

内核版本:3.10.0-1062.el7.x86_64;

注意:国内访问docker官方镜像会很慢,大概率会安装失败,可通过配置aliyun的源来完成安装,配置步骤参考文章 。

1、卸载docker旧版本(未安装过docker可跳过,此步骤主要是防止有安装失败的残留包,导致安装异常)

 [root@k8sworker ~]# yum remove docker \
>                   docker-client \
>                   docker-client-latest \
>                   docker-common \
>                   docker-latest \
>                   docker-latest-logrotate \
>                   docker-logrotate \
>                   docker-selinux \
>                   docker-engine-selinux \
>                   docker-engine
Loaded plugins: fastestmirror, langpacks
No Match for argument: docker
No Match for argument: docker-client
No Match for argument: docker-client-latest
No Match for argument: docker-common
No Match for argument: docker-latest
No Match for argument: docker-latest-logrotate
No Match for argument: docker-logrotate
No Match for argument: docker-selinux
No Match for argument: docker-engine-selinux
No Match for argument: docker-engine
No Packages marked for removal
# No Match表示没有残留安装包  

2、 安装相关工具类

 [root@k8sworker ~]# yum install -y yum-utils \
>         device-mapper-persistent-data \
>         lvm2
......
Updated:
  device-mapper-persistent-data.x86_64 0:0.8.5-3.el7_9.2  lvm2.x86_64 7:2.02.187-6.el7_9.5  yum-utils.noarch 0:1.1.31-54.el7_8 

Dependency Updated:
  device-mapper.x86_64 7:1.02.170-6.el7_9.5                         device-mapper-event.x86_64 7:1.02.170-6.el7_9.5             
  device-mapper-event-libs.x86_64 7:1.02.170-6.el7_9.5              device-mapper-libs.x86_64 7:1.02.170-6.el7_9.5              
  lvm2-libs.x86_64 7:2.02.187-6.el7_9.5                            

Complete!
......  

3、 配置docker仓库

 [root@k8smaster tmp]# yum-config-manager --add-repo 
Loaded plugins: fastestmirror, langpacks
adding repo from: 
grabbing file  to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo  

如果报以下错误:

 Loaded plugins: fastestmirror 
adding repo from:  
grabbing file  to /etc/yum.repos.d/docker-ce.repo 
Could not fetch/save url  to file /etc/yum.repos.d/docker-ce.repo : 
[Errno 14] curl#35 - "TCP connection reset by peer
......
......  

则是由于访问不到docker官方镜像的缘故,需使用国内的源来完成(建议使用aliyun的库源来安装):

 [root@k8sworker ~]# yum-config-manager --add-repo 
Loaded plugins: fastestmirror, langpacks
adding repo from: 
grabbing file  to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo  

4、 验证docker-ce.repo文件内的链接是否指向aliyun的镜像库

 [root@k8sworker ~]# cat /etc/yum.repos.d/docker-ce.repo 
[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=
enabled=1
gpgcheck=1
gpgkey=

[docker-ce-stable-debuginfo]
name=Docker CE Stable - Debuginfo $basearch
baseurl=
enabled=0
gpgcheck=1
gpgkey=

[docker-ce-stable-source]
name=Docker CE Stable - Sources
baseurl=
enabled=0
gpgcheck=1
gpgkey=

[docker-ce-test]
name=Docker CE Test - $basearch
baseurl=
enabled=0
gpgcheck=1
gpgkey=

[docker-ce-test-debuginfo]
name=Docker CE Test - Debuginfo $basearch
baseurl=
enabled=0
gpgcheck=1
gpgkey=

[docker-ce-test-source]
name=Docker CE Test - Sources
baseurl=
enabled=0
gpgcheck=1
gpgkey=

[docker-ce-nightly]
name=Docker CE Nightly - $basearch
baseurl=
enabled=0
gpgcheck=1
gpgkey=

[docker-ce-nightly-debuginfo]
name=Docker CE Nightly - Debuginfo $basearch
baseurl=
enabled=0
gpgcheck=1
gpgkey=

[docker-ce-nightly-source]
name=Docker CE Nightly - Sources
baseurl=
enabled=0
gpgcheck=1
gpgkey=  

5、 安装docker-ce

 [root@k8sworker ~]# yum install -y docker-ce
# 部分输出内容如下: 
......
Installed:
  docker-ce.x86_64 3:20.10.6-3.el7                                  
  
Dependency Installed:
  audit-libs-python.x86_64 0:2.8.5-4.el7                                      
  checkpolicy.x86_64 0:2.5-8.el7                                              
  container-selinux.noarch 2:2.119.2-1.911c772.el7_8                          
  containerd.io.x86_64 0:1.4.6-3.1.el7                                        
  docker-ce-cli.x86_64 1:20.10.6-3.el7                                        
  docker-ce-rootless-extras.x86_64 0:20.10.6-3.el7                            
  docker-scan-plugin.x86_64 0:0.7.0-3.el7                                     
  fuse-overlayfs.x86_64 0:0.7.2-6.el7_8                                       
  fuse3-libs.x86_64 0:3.6.1-4.el7                                             
  libcgroup.x86_64 0:0.41-21.el7                                              
  libsemanage-python.x86_64 0:2.5-14.el7                                      
  policycoreutils-python.x86_64 0:2.5-34.el7                                  
  python-IPy.noarch 0:0.75-6.el7                                              
  setools-libs.x86_64 0:3.3.8-4.el7                                           
  slirp4netns.x86_64 0:0.4.3-4.el7_8                                          

Complete!  

6、 启动、验证docker是否安装成功

 # 启动docker
[root@k8sworker ~]# systemctl start docker

# 验证docker(会自动从DockerHub库里面先pull镜像,默认是lastest标签)
[root@k8smaster yum.repos.d]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete 
Digest: sha256:5122f6204b6a3596e048758cabba3c46b1c937a46b5be6225b835d091b90e46c
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:
 

For more examples and ideas, visit:
 
 
  # 镜像列表查询
 [root@k8sworker ~]# docker image list
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   3 months ago   13.3kB

# docker版本
[root@k8smaster yum.repos.d]# docker version
Client: Docker Engine - Community
 Version:           20.10.6
 API version:       1.41
 Go version:        go1.13.15
  Git  commit:        370c289
 Built:             Fri Apr  9 22:45:33 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.6
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       8728dd2
  Built:            Fri Apr  9 22:43:57 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.6
  GitCommit:        d71fcd7d8303cbf684402823e425e9dd2e99285d
 runc:
  Version:          1.0.0-rc95
  GitCommit:        b9ee9c6314599f1b4a7f497e1f1f856fe433d3b7
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0  

7、 添加user到组(使非root用户直接使用docker)

 # 安装docker后自动有docker用户组,正常情况不需要新建
[root@k8sworker ~]# groupadd docker
groupadd: group 'docker' already exists
[root@k8sworker ~]# usermod -aG docker $USER  

8、 设置docker开机自动运行

 [root@k8sworker ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.  

相关文章