# Docker 安装

#### 博客作者：联系请[点击](https://hezhiqiang.gitbook.io/about-the-author/lian-xi-zuo-zhe)，搬运不易，希望请作者喝咖啡，可以点击[联系博客作者](https://hezhiqiang.gitbook.io/about-the-author/lian-xi-zuo-zhe)

## CentOS 上安装 Docker <a href="#title" id="title"></a>

### 先决条件 <a href="#prerequisites" id="prerequisites"></a>

#### 操作系统要求 <a href="#os-requirements" id="os-requirements"></a>

Docker CE 支持 64 位版本 CentOS 7，并且要求内核版本不低于 3.10，CentOS 7 满足最低内核的要求，但由于内核版本比较低，部分功能（如 `overlay2` 存储层驱动）无法使用，并且部分功能可能不太稳定。

#### 卸载旧版本 <a href="#uninstall-old-versions" id="uninstall-old-versions"></a>

旧版本的 Docker 名称为`docker`或`docker-engine`。在尝试安装新版本之前卸载任何此类旧版本以及相关的依赖项：

```sh
sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine
```

`yum`如果报告没有安装这些软件包，那也没关系。

当您卸载 Docker 时，存储在其中的图像、容器、卷和网络`/var/lib/docker/`不会自动删除。

#### 使用存储库安装 <a href="#install-using-the-repository" id="install-using-the-repository"></a>

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

**设置存储库**

安装`yum-utils`包（提供`yum-config-manager` 实用程序）并设置存储库。

```sh
sudo yum install -y yum-utils sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
```

鉴于国内网络问题，强烈建议使用国内源，官方源请在注释中查看。

执行下面的命令添加 `yum` 软件源：

```bash
sudo yum-config-manager \
    --add-repo \
    https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo


# 官方源
# sudo yum-config-manager \
#     --add-repo \
#     https://download.docker.com/linux/centos/docker-ce.repo
```

### 安装之前需要关闭防火墙和selinux

#### 关闭防火墙

```
systemctl stop firewalld
systmctl disable firewalld
firewall-cmd --state
```

#### 关闭**selinux**

```
setenforce 0
sed -ri 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
sestatus
```

### 安装最新版本Docker CE

```sh
 sudo yum install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
```

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

### 加入 Docker 用户组

默认情况下，`docker` 命令会使用 [Unix socket](https://en.wikipedia.org/wiki/Unix_domain_socket) 与 Docker 引擎通讯。而只有 `root` 用户和 `docker` 组的用户才可以访问 Docker 引擎的 Unix socket。出于安全考虑，一般 Linux 系统上不会直接使用 `root` 用户，因此，更好地做法是将需要使用 `docker` 的用户加入 `docker` 用户组。

将当前用户加入 `docker` 组：

```bash
$ sudo usermod -aG docker $USER
```

## 启动Docker

```
sudo systemctl enable --now docker
```

检查docker是否安装成功并检查安装的版本

```
docker -v
```

## 镜像加速

如果在使用过程中发现拉取 Docker 镜像十分缓慢，可以配置 Docker 国内镜像加速。

您可以通过修改daemon配置文件`/etc/docker/daemon.json`来使用加速器

```
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://u13zwy2w.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
```

## 添加内核参数

如果在 CentOS 使用 Docker CE 看到下面的这些警告信息：

```bash
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
```

请添加内核配置参数以启用这些功能。

```bash
sudo tee -a /etc/sysctl.conf <<-EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
```

然后重新加载 `sysctl.conf` 即可

```bash
sudo sysctl -p
```

## 参考文档

* [Docker 官方 CentOS 安装文档](https://docs.docker.com/install/linux/docker-ce/centos/)。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hezhiqiang.gitbook.io/about-the-author/yun-yuan-sheng-yun-wei-jiao-cheng/docker-an-zhuang.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
