如何在 CentOS 中安装 Docker

不久前,我们发布了一个关于 如何在 Ubuntu 上安装 Docker. 今天,我们将了解如何在 CentOS Linux 操作系统中安装 Docker。 此外,我们还将了解如何安装 Docker compose,这是一个定义和运行多容器 Docker 应用程序的工具。

尽管本指南是专门为 CentOS 编写的,但本指南对于其他基于 RPM 的发行版也是一样的,例如 Fedora、RHEL、Scientific Linux 和 openSUSE 等。

在 CentOS 中安装 Docker

要在 CentOS 上安装 Docker,您的系统必须满足以下最低要求。

  1. 64 位 建筑学;
  2. 内核版本应该是 3.10 或以上。
  3. 您的系统应与 Internet 连接。

Docker 安装相当简单直接。

打开终端并运行以下命令 root 用户。

# curl -fsSL https://get.docker.com/ | sh

安装 Docker 后,运行以下命令启用并启动 Docker 服务。

# systemctl enable docker
# systemctl start docker

使用命令检查 Docker 服务是否正在运行:

# systemctl status docker

示例如下所示。

● docker.service - Docker Application Container Engine
 Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
 Active: active (running) since Wed 2017-03-08 14:09:38 IST; 24s ago
 Docs: https://docs.docker.com
 Main PID: 1190 (dockerd)
 Memory: 54.4M
 CGroup: /system.slice/docker.service
 ├─1190 /usr/bin/dockerd
 └─2327 docker-containerd -l unix:///var/run/docker/libcontainerd/d...

Mar 08 14:09:35 server1.ostechnix.local dockerd[1190]: time="2017-03-08T14:09:3...
Mar 08 14:09:35 server1.ostechnix.local dockerd[1190]: time="2017-03-08T14:09:3...
Mar 08 14:09:35 server1.ostechnix.local dockerd[1190]: time="2017-03-08T14:09:3...
Mar 08 14:09:36 server1.ostechnix.local dockerd[1190]: time="2017-03-08T14:09:3...
Mar 08 14:09:37 server1.ostechnix.local dockerd[1190]: time="2017-03-08T14:09:3...
Mar 08 14:09:38 server1.ostechnix.local dockerd[1190]: time="2017-03-08T14:09:3...
Mar 08 14:09:38 server1.ostechnix.local dockerd[1190]: time="2017-03-08T14:09:3...
Mar 08 14:09:38 server1.ostechnix.local dockerd[1190]: time="2017-03-08T14:09:3...
Mar 08 14:09:38 server1.ostechnix.local dockerd[1190]: time="2017-03-08T14:09:3...
Mar 08 14:09:38 server1.ostechnix.local systemd[1]: Started Docker Application ...
Hint: Some lines were ellipsized, use -l to show in full.

如上所示,Docker 服务正在运行!

测试 Docker

让我们继续,测试 Docker 是否正常工作。

为此,请运行:

# docker run hello-world

上述命令将下载一个测试 Docker 镜像,并执行一个示例 你好世界 容器内的程序。

如果您看到类似下面的输出,恭喜! Docker 在我们的 CentOS 系统中运行良好。

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
78445dd45222: Pull complete 
Digest: sha256:c5515758d4c5e1e838e9cd307f6c6a0d620b5e07e6f927b07d05f6d12a1ac8d7
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.
 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://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

要检查 Docker 版本,请运行:

# docker version

样本输出:

Client:
 Version: 17.03.0-ce
 API version: 1.26
 Go version: go1.7.5
 Git commit: 60ccb22
 Built: Thu Feb 23 10:54:03 2017
 OS/Arch: linux/amd64

Server:
 Version: 17.03.0-ce
 API version: 1.26 (minimum version 1.12)
 Go version: go1.7.5
 Git commit: 60ccb22
 Built: Thu Feb 23 10:54:03 2017
 OS/Arch: linux/amd64
 Experimental: false

目前为止就这样了。 Docker 可以使用了。

安装 Docker Compose

码头工人撰写 是一个可用于定义和运行多容器 Docker 应用程序的工具。 使用 Compose,您可以使用 Compose 文件来配置应用程序的服务。 然后,使用单个命令,您可以从配置中创建并启动所有服务。

我们可以使用以下任何一种方法安装 Docker Compose。

方法一:

从终端运行以下命令 root 用户下载 Docker Compose 二进制文件:

# curl -L "https://github.com/docker/compose/releases/download/1.11.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

然后,使用以下命令对二进制文件应用可执行权限:

# chmod +x /usr/local/bin/docker-compose

方法二:

或者,我们可以使用 PIP 安装 Docker Compose。 Pip 是一个 python 包管理器,用于安装用 Python 编程语言编写的应用程序。

在 Arch Linux 及其衍生版本上,运行以下命令来安装 pip:

# sudo pacman -S python-pip

在 Debian、Ubuntu、Linux Mint 上:

# sudo apt-get install python-pip

在 RHEL 上, Fedora中央操作系统:

# sudo yum install python-pip

或者,

# sudo dnf install python-pip

在 SUSE/openSUSE 上:

# sudo zypper install python-pip

安装 pip 后,运行以下命令安装 docker compose。 以下命令对于所有 Linux 发行版都是相同的!

# pip install docker-compose

安装 Docker Compose 后,您可以使用以下命令检查版本:

# docker-compose --version

您将看到如下所示的输出。

docker-compose version 1.11.2, build dfed245

我们现在知道如何在 CentOS 中安装 Docker 和 Docker Compose。 阅读以下文章以开始使用 Docker。

  • 开始使用 Docker

下载免费的 Docker 电子书:

Download - Free eBook: "Docker Containerization Cookbook"
Download - Free Guide: "Understanding Docker"
Download - Free Guide: "What is Docker and Why is it So Popular?"
Download - Free Guide: "Introduction to Docker"
Download - Free Guide: "Docker in Production"

建议阅读:

  • 如何自动更新正在运行的 Docker 容器
  • ctop – Linux 容器的命令行监控工具
  • Portainer – 管理 Docker 的最简单方法
  • PiCluster – 一个简单的、基于 Web 的 Docker 管理应用程序
  • Dockly – 从终端管理 Docker 容器