CentOS安装Docker

一、安装Docker
1、查看内核版本
root账户登录,查看内核版本如下
[root@localhost ~]# uname -a
Linux localhost.localdomain 3.10.0-1160.25.1.el7.x86_64 #1 SMP Wed Apr 28 21:49:45 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

关于内核,docker官方建议3.10以上
2、安装需要的软件包
yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的
[root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2

3、设置yum源
这里提供两个

  • http://download.docker.com/linux/centos/docker-ce.repo
  • http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    一个是docker的中央仓库,一个是阿里的仓库,如果中央仓库无法连接,就用阿里的仓库
    [root@localhost ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

4、查看docker版本
[root@localhost ~]# yum list docker-ce --showduplicates | sort -r

  • updates: mirrors.aliyun.com
    Loading mirror speeds from cached hostfile
    Loaded plugins: fastestmirror
    Installed Packages
  • extras: mirrors.aliyun.com
    docker-ce.x86_64 3:20.10.6-3.el7 docker-ce-stable
    docker-ce.x86_64 3:20.10.6-3.el7 @docker-ce-stable
    docker-ce.x86_64 3:20.10.5-3.el7 docker-ce-stable
    docker-ce.x86_64 3:20.10.4-3.el7 docker-ce-stable
    ...

5、安装docker
这里安装最新版本
[root@localhost ~]# yum install docker-ce-20.10.6-3.el7

注意docker版本必须大于1.19.x才能编译version>3.8(此处version指的是docker-compose.yml中的version)
6、启动docker
[root@localhost ~]# systemctl start docker

将docker加入开机自启
[root@localhost ~]# systemctl enable docker

7、查看docker版本
[root@localhost ~]# 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.4
GitCommit: 05f951a3781f4f2c1911b05e61c160e9c30eaa8e
runc:
Version: 1.0.0-rc93
GitCommit: 12644e614e25b05da6fd08a38ffa0cfe1903fdec
docker-init:
Version: 0.19.0
GitCommit: de40ad0

二、安装Docker-Compose
[root@localhost ~]# curl -L "https://github.com/docker/compose/releases/download/1.26.1/docker-compose-(uname -m)" -o /usr/local/bin/docker-compose
[root@localhost ~]# chmod +x /usr/local/bin/docker-compose
[root@localhost ~]# ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

注意docker-compose的版本得大于1.25.x才能编译version>3.8(此处version指的是docker-compose.yml中的version)
查看docker-compose版本
[root@localhost ~]# docker-compose version
docker-compose version 1.26.2, build eefe0d31
docker-py version: 4.2.2
CPython version: 3.7.7
OpenSSL version: OpenSSL 1.1.0l 10 Sep 2019

三、配置Docker
加载到/etc/docker,新建或者编辑daemon.json文件
注意此处daemon.json的文件名是固定的
[root@localhost ~]# cd /etc/docker/
[root@localhost docker]# ls
daemon.json key.json

1、配置镜像源
在daemon.json中添加如下代码段
{
...
"registry-mirrors": [
"https://registry.aliyuncs.com"
],
...
}

此处添加的是阿里镜像源
2、配置docker默认网桥
在docker启动时,会启动一个叫docker0的网桥,此网桥的ip地址默认为172.18.x.x,为了避免其与我们虚拟机的ip地址重合,我们需要配置docker默认网桥的ip地址,在daemon.json中添加如下代码段
...
"bip": "192.188.0.1/16",
...

重启docker,输入ifconfig查看
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# ifconfig
docker0: flags=4099 mtu 1500
inet 192.188.0.1 netmask 255.255.0.0 broadcast 192.188.255.255
inet6 fe80::42:a1ff:fe7e:d0b prefixlen 64 scopeid 0x20
ether 02:42:a1:7e:0d:0b txqueuelen 0 (Ethernet)
RX packets 420 bytes 22800 (22.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 622 bytes 2092383 (1.9 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

eth0: flags=4163 mtu 1500
inet 172.18.3.171 netmask 255.255.252.0 broadcast 172.18.3.255
inet6 fe80::5054:ff:fe83:608b prefixlen 64 scopeid 0x20
ether 52:54:00:83:60:8b txqueuelen 1000 (Ethernet)
RX packets 2449051 bytes 5347352163 (4.9 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1703247 bytes 117348816 (111.9 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1000 (Local Loopback)
RX packets 216 bytes 38880 (37.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 216 bytes 38880 (37.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

3、配置Docker网络的默认值
通常情况下,使用docker-compose up或者自己通过 docker network create 创建自定义Docker网络,默认会从172.19.0.0/16选取可用网段,也是为了防止其与我们虚拟机的ip发生冲突,我们需要指定ip,在daemon.json中添加如下代码段
"default-address-pools": [{"base":"10.10.0.0/16","size":24}]

其中的ip地址可以自行指定
4、完整的配置
配置文件地址: /etc/docker/daemon.json

你可能感兴趣的:(CentOS安装Docker)