在 CentOS 上安装 Docker 引擎

To get started with Docker Engine on CentOS, make sure you meet the prerequisites, then install Docker.
要在 CentOS 上开始使用 Docker Engine,请确保满足先决条件,然后安装 Docker。

Prerequisites 先决条件

OS requirements 操作系统要求

To install Docker Engine, you need a maintained version of CentOS 7 or 8. Archived versions aren’t supported or tested.
要安装 Docker Engine,你需要一个 CentOS 7或8的维护版本。
The centos-extras repository must be enabled. This repository is enabled by default, but if you have disabled it, you need to re-enable it.
必须启用 centos-extras 存储库。此存储库默认是启用的,但是如果您已经禁用了它,则需要重新启用它。
The overlay2 storage driver is recommended.
建议使用 overlay2存储驱动程序。

Uninstall old versions 卸载旧版本

Older versions of Docker were called docker or docker-engine. If these are installed, uninstall them, along with associated dependencies.
旧版本的 Docker 被称为 Docker 或 Docker-engine。如果已经安装了它们,请卸载它们以及相关的依赖项。

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

It’s OK if yum reports that none of these packages are installed.
如果 yum 报告没有安装这些软件包,也没关系。
The contents of /var/lib/docker/, including images, containers, volumes, and networks, are preserved. The Docker Engine package is now called docker-ce.
保留/var/lib/Docker/的内容,包括图像、容器、卷和网络。Docker Engine 包现在称为 Docker-ce。

Installation methods 安装方法

You can install Docker Engine in different ways, depending on your needs:
根据你的需要,你可以用不同的方式安装 Docker Engine:

  • Most users set up Docker’s repositories and install from them, for ease of installation and upgrade tasks. This is the recommended approach.大多数用户建立了 Docker 的存储库并从中安装,以便于安装和升级任务。这是推荐的方法。
  • Some users download the RPM package and install it manually and manage upgrades completely manually. This is useful in situations such as installing Docker on air-gapped systems with no access to the internet.有些用户下载 RPM 包并手动安装它,完全手动管理升级。这是有用的情况下,如安装 Docker 空气间隙系统无法访问互联网。
  • In testing and development environments, some users choose to use automated convenience scripts to install Docker.在测试和开发环境中,一些用户选择使用自动化的便利脚本来安装 Docker。

Install using the repository 使用存储库进行安装

Before you install Docker Engine for the first time on a new host machine, you need to set up the Docker repository. Afterward, you can install and update Docker from the repository.
首次在新主机上安装 Docker Engine 之前,需要设置 Docker 存储库。之后,您可以从存储库中安装和更新 Docker。

Set up the repository
设置存储库

Install the yum-utils package (which provides the yum-config-manager utility) and set up the stable repository.
安装 yum-utils 包(它提供 yum-config-manager 实用工具)并设置稳定存储库。

sudo yum install -y yum-utils

**提示:**由于docker官方国内访问比较慢,可以通过阿里云的源来完成:
官方:

sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

阿里云:

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

Optional: Enable the nightly or test repositories.
可选: 启用夜间或测试存储库。
These repositories are included in the docker.repo file above but are disabled by default. You can enable them alongside the stable repository. The following command enables the nightly repository.
这些存储库包含在上面的 docker.repo 文件中,但默认情况下是禁用的。您可以在稳定存储库旁边启用它们。下面的命令启用夜间存储库。
$ sudo yum-config-manager --enable docker-ce-nightly
To enable the test channel, run the following command:
要启用测试通道,请运行以下命令:
$ sudo yum-config-manager --enable docker-ce-test
You can disable the nightly or test repository by running the yum-config-manager command with the --disable flag. To re-enable it, use the --enable flag. The following command disables the nightly repository.
您可以通过使用 – disable 标志运行 yum-config-manager 命令来禁用 nightly 或 test 存储库。要重新启用它,请使用 – enable 标志。下面的命令禁用夜间存储库。
$ sudo yum-config-manager --disable docker-ce-nightly
Learn aboutnightlyandtestchannels.
了解夜间和测试频道。

Install Docker Engine
安装 Docker 引擎
  1. Install the latest version of Docker Engine and containerd, or go to the next step to install a specific version:

安装最新版本的 Docker Engine 和 containerd,或者进入下一步安装一个特定版本:

sudo yum install docker-ce docker-ce-cli containerd.io

If prompted to accept the GPG key, verify that the fingerprint matches 060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35, and if so, accept it.
如果提示接受 GPG 密钥,验证指纹是否与060a 61c51b558a7f 742B 77AA C52F EB6B 621E 9f35匹配,如果是,接受它。

Got multiple Docker repositories?
得到多个 Docker 仓库?
If you have multiple Docker repositories enabled, installing or updating without specifying a version in the yum install or yum update command always installs the highest possible version, which may not be appropriate for your stability needs.
如果启用了多个 Docker 存储库,在 yum install 或 yum update 命令中安装或更新时不指定版本,那么总是安装尽可能高的版本,这可能不适合您的稳定性需求。

This command installs Docker, but it doesn’t start Docker. It also creates a docker group, however, it doesn’t add any users to the group by default.
这个命令安装了 Docker,但是它没有启动 Docker。它还创建了一个 docker 组,但是默认情况下不会向组中添加任何用户。

  1. To install a specific version of Docker Engine, list the available versions in the repo, then select and install:

要安装一个特定版本的 Docker Engine,在 repo 中列出可用的版本,然后选择并安装:
a. List and sort the versions available in your repo. This example sorts results by version number, highest to lowest, and is truncated:
a. 列出并排序在你的repo中可用的版本。这个例子根据版本号对结果进行排序,从最高到最低,并被截断:

yum list docker-ce --showduplicates | sort -r

docker-ce.x86_64 3:18.09.1-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.0.ce-3.el7 docker-ce-stable

The list returned depends on which repositories are enabled, and is specific to your version of CentOS (indicated by the .el7 suffix in this example).
返回的列表取决于启用了哪些存储库,并且特定于您的 CentOS 版本(由。这个例子中的 el7后缀)。
b. Install a specific version by its fully qualified package name, which is the package name (docker-ce) plus the version string (2nd column) starting at the first colon (:), up to the first hyphen, separated by a hyphen (-). For example, docker-ce-18.09.1.B.
通过其完全限定的包名安装特定版本,即包名(docker-ce)加上从第一个冒号(:)开始的版本字符串(第2列) ,直到第一个连字符,中间用连字符(-)分隔。例如,docker-ce-18.09.1。

sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io

This command installs Docker, but it doesn’t start Docker. It also creates a docker group, however, it doesn’t add any users to the group by default.
这个命令安装了 Docker,但是它没有启动 Docker。它还创建了一个 docker 组,但是默认情况下不会向组中添加任何用户。

  1. Start Docker.启动 Docker。
sudo systemctl start docker 
  1. Verify that Docker Engine is installed correctly by running the hello-world image.通过运行 hello-world 映像来验证 Docker Engine 安装是否正确。
sudo docker run hello-world

This command downloads a test image and runs it in a container. When the container runs, it prints a message and exits.
此命令下载测试映像并在容器中运行它。当容器运行时,它打印一条消息并退出。
This installs and runs Docker Engine. Use sudo to run Docker commands. Continue to Linux postinstall to allow non-privileged users to run Docker commands and for other optional configuration steps.
这将安装并运行 Docker 引擎。使用 sudo 运行 Docker 命令。继续安装 Linux 后,允许非特权用户运行 Docker 命令和其他可选配置步骤。

配置镜像

Docker默认拉取镜像是从这里拉取(https://hub.docker.com),国外地址拉取的速度比较慢。我们也可以配置国内镜像源。

编辑配置文件/etc/docker/daemon.json(没有时新建该文件)

vim /etc/docker/daemon.json
# 添加如下内容,可以自己替换。默认地址是我的创建的
{
"registry-mirrors": ["https://mr63yffu.mirror.aliyuncs.com"]
}

重启docker守护进程

systemctl daemon-reload
systemctl restart docker

设置全局容器日志大小

创建或修改文件 /etc/docker/daemon.json,并增加以下配置:

{
  "log-driver":"json-file",
  "log-opts":{
    "max-size" :"500m","max-file":"3"
  }
}

重启docker守护进程

systemctl daemon-reload
systemctl restart docker

参考文档
Install Docker Engine on CentOS
Configure logging drivers
Docker——修改镜像源_oSmile_的博客-CSDN博客_修改docker镜像源
解决Docker容器日志占用空间过大_Java3L的博客-CSDN博客_docker日志太大

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值