Centos安装Docker容器以及Dockerhub不能访问的解决方案

1
2
3
4
5
6
作者: 夜泊1990
企鹅: 1611756908
Q 群: 948233848
邮箱: hd1611756908@163.com
博客: https://hs-an-yue.github.io/
B 站: https://space.bilibili.com/514155929/

第一章 Docker介绍

1
2
Docker 是一个开源的应用容器引擎
Docker 将应用程序与该程序的依赖,打包在一个文件里面。运行这个文件,就会生成一个虚拟容器.程序在这个虚拟容器里运行,就好像在真实的物理机上运行一样,并且开销很低.

第二章 Docker安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
这里演示在Centos7.x上安装Docker

第一步: Linux内核升级,Linux内核版本需要大于3.10版本
命令:
1. 查看内核 uname -r
2. 内核升级 yum install -y kernel

第二步: 安装 gcc和gcc-c++编译器,如果本机中存在可以不装
命令:
1. 安装gcc yum -y install gcc
2. 安装gcc-c++ yum -y install gcc-c++

第三步: 卸载旧版本的docker
命令:
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine

第四步: 安装相关依赖包
命令:
yum install -y yum-utils device-mapper-persistent-data lvm2

第五步: 设置stable镜像仓库(阿里云镜像仓库)
命令:
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

第六步: 更新yum软件包索引
命令:
yum makecache fast

第七步: 搜索当前的Docker版本,版本由高到低排列
命令:
yum list docker-ce.x86_64 --showduplicates | sort -r

搜索结果展示(部分)
docker-ce.x86_64 3:26.1.4-1.el7 docker-ce-stable
docker-ce.x86_64 3:26.1.3-1.el7 docker-ce-stable
docker-ce.x86_64 3:26.1.2-1.el7 docker-ce-stable
docker-ce.x86_64 3:26.1.1-1.el7 docker-ce-stable
docker-ce.x86_64 3:26.1.0-1.el7 docker-ce-stable
docker-ce.x86_64 3:26.0.2-1.el7 docker-ce-stable
docker-ce.x86_64 3:26.0.1-1.el7 docker-ce-stable

第八步: 安装指定版本的Docker
命令格式:
yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io

VERSION_STRING: 版本查看上面的搜索结果
版本号截取方式在第二列中从冒号(:)开始,短横线(-)结束中间的那段为版本(例如 3:20.10.2-3.el7 的版本为 20.10.2)替换<VERSION_STRING>即可

安装Docker26.1.4版本命令如下:
yum install docker-ce-26.1.4 docker-ce-cli-26.1.4

第九步: 安装完成启动
命令:
1. 启动Docker systemctl start docker
2. 查看Docker是否正常运行 ps -ef | grep docker

第十步: 测试是否安装成功(运行hello-world镜像测试)
命令: docker run hello-world

报错信息:
[root@192 ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Retrying in 1 second
docker: error pulling image configuration: download failed after attempts=6: dial tcp 128.242.240.85:443: connect: connection refused.
See 'docker run --help'.

报错原因: 因为 DockerHub 不能在国内使用,所以运行hello-world时,不能从DockerHub上下载镜像,所以失败

第十一步: 解决 DockerHub 镜像站不能访问的问题

解决方案: 国内有能够替代DockerHub的镜像站
例如: 百度镜像站,南京大学镜像站等,可以替换默认的DockerHub镜像站
1. 百度镜像地址: https://mirror.baidubce.com
2. 南京大学镜像地址: https://docker.nju.edu.cn

替换方式:
第1步: 进入 cd /etc/docker/ 目录下查看是否有daemon.json文件,如果没有就自己手动创建一个
第2步: 如果存在daemon.json文件将下面的信息保存到daemon.json中
{
"registry-mirrors": [
"https://dockerhub.timeweb.cloud",
"https://docker.m.daocloud.io",
"https://noohub.ru",
"https://docker.agsv.top",
"https://hub.iyuu.cn",
"https://docker.shootchat.top",
"https://do.nark.eu.org",
"https://docker.m.daocloud.io"
]
}
第3步: 重启docker 命令: systemctl restart docker

第4步: 重启后继续执行 docker run hello-world 命令测试镜像地址是否成功

如果出现以下日志表示成功

[root@192 ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete
Digest: sha256:1408fec50309afee38f3535383f5b09419e6dc0925bc69891e79d84cc4cdcec6
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开机启动
命令: systemctl enable docker
--------------------------已经到底啦!--------------------------