0%

CentOS7安装Docker

操作系统:CentOS Linux release 7.8.2003 (Core)

安装方式:yum

官方文档

添加Docker的yum源

Install the yum-utils package (which provides the yum-config-manager utility) and set up the stable repository.

1
2
3
$ sudo yum install -y yum-utils

$ wget https://download.docker.com/linux/centos/docker-ce.repo -P /etc/yum.repos.d

国内访问docker官方镜像可能会有异常
可以通过aliyun的源来完成

1
$ yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

安装Docker Engine

安装最新版本的Docker Engine

1
$ sudo yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin

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

1
2
3
4
5
6
7
8
$ yum list docker-ce --showduplicates | sort -r
......
docker-ce.x86_64 3:20.10.13-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.13-3.el7 @docker-ce-stable
docker-ce.x86_64 3:20.10.12-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.11-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.10-3.el7 docker-ce-stable
......
1
$ sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io docker-compose-plugin

此命令会安装 Docker,但不会启动 Docker。它还会创建一个 docker组,但是默认情况下它不会将任何用户添加到该组中。

1
$ sudo yum install docker-ce-20.10.10 docker-ce-cli-20.10.10 containerd.io docker-compose-plugin -y

启动 Docker

1
$ sudo systemctl start docker

配置网络代理(非必选)

很多时候,我们的服务器是没有直接访问外网的权限,只能通过网络代理的方式实现外网访问。这个时候,就需要配置网络代理

  • 编辑配置文件
1
$ mkdir /etc/systemd/system/docker.service.d/ && vim /etc/systemd/system/docker.service.d/http-proxy.conf 
1
2
[Service]
Environment="HTTP_PROXY=http://192.168.0.10:8118/" "HTTPS_PROXY=http://192.168.0.10:8118/" "NO_PROXY=localhost,127.0.0.1"

【注】HTTP_PROXY 用于代理访问 http 请求,HTTPS_PROXY 用于代理访问 https 请求,如果想某个 IP域名不走代理则配置到 NO_PROXY中。

  • 刷新更改并重新启动 Docker
1
2
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker

配置镜像仓库

  • 编辑docker的配置文件
1
$ vim /etc/docker/daemon.json
1
2
3
4
5
6
{
"registry-mirrors": ["https://45ucv2hi.mirror.aliyuncs.com",
"https://registry.docker-cn.com",
"http://hub-mirror.c.163.com",
"https://docker.mirrors.ustc.edu.cn"]
}
  • 刷新更改并重新启动 Docker
1
2
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker

hello-world

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
$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
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/

此命令下载测试映像并在容器中运行它。当容器运行时,它会打印一条消息并退出。

调整Cgroup Driver

由于Docker的Cgroup Driver默认为cgroupfs, 与k8s的不一致。所以需要调整

1
2
$ docker info | grep " Cgroup Driver"
Cgroup Driver: cgroupfs
  • 调整配置文件
1
$ vim /etc/docker/daemon.json
1
2
3
{
"exec-opts": ["native.cgroupdriver=systemd"]
}
  • 刷新更改并重新启动 Docker
1
2
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker
  • 验证是否修改成功
1
2
$ docker info | grep " Cgroup Driver"
Cgroup Driver: systemd

安装 Docker Compose

Docker Compose 存放在Git Hub,不太稳定。
你可以也通过执行下面的命令,高速安装Docker Compose。

1
2
$ curl -L https://get.daocloud.io/docker/compose/releases/download/v2.6.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
$ chmod +x /usr/local/bin/docker-compose

安装过程中可能遇到的问题

问题一

  • 在执行yum install之后,有如下报错
1
2
3
4
5
6
7
8
错误:软件包:3:docker-ce-20.10.10-3.el7.x86_64 (docker-ce-stable)
需要:container-selinux >= 2:2.74
错误:软件包:docker-ce-rootless-extras-20.10.16-3.el7.x86_64 (docker-ce-stable)
需要:fuse-overlayfs >= 0.7
错误:软件包:containerd.io-1.6.4-3.1.el7.x86_64 (docker-ce-stable)
需要:container-selinux >= 2:2.74
错误:软件包:docker-ce-rootless-extras-20.10.16-3.el7.x86_64 (docker-ce-stable)
需要:slirp4netns >= 0.4
  • 编辑yum源

    1
    $ vim /etc/yum.repos.d/docker-ce.repo 
  • 在文件顶部添加一个条目

    1
    2
    3
    4
    5
    [centos-extras]
    name=Centos extras - $basearch
    baseurl=http://mirror.centos.org/centos/7/extras/x86_64
    enabled=1
    gpgcheck=0
  • 然后安装

    1
    $ yum -y install slirp4netns fuse-overlayfs container-selinux

参考链接

-------- 本文结束 感谢阅读 --------