什么是 Docker?
在开始之前,让我们先描述一下 Docker 是什么。 Docker 是一组虚拟化工具,允许我们在专用服务器上快速轻松地创建、测试和部署容器化应用程序。 它已经变得非常流行,并且在我们日常生活中几乎无处不在。 由于容器化,我们可以利用包含运行应用程序所需的所有包、库和配置文件的小包,在不同的私有云主机平台上快速启动应用程序。 这些 docker 包通过已建立的网络通道进行通信。
Docker 有两个主要版本:
- Docker CE(社区版)
- Docker EE(企业版)
我们不会讨论 CentOS 8 的许多与 Docker 一起运行的新功能,因为 RedHat 已决定不为 Docker 提供官方支持。 但是,Redhat 确实提供了一些用于在服务器集群中创建和管理容器镜像的内置工具,尤其是 Buildah 和 Podman。 这些工具与 docker 兼容,但它们需要服务器/客户端架构。 许多人更喜欢使用 Docker 容器化,在本教程中,我们想演示如何使用外部存储库在 CentOS 上安装 Docker。 那么让我们开始吧!
安装 Docker CE
首先,我们应该删除所有现有的 docker 应用程序(可能当前已安装),以确保我们使用的是全新安装。 您可能没有安装其中一些组件。
[root@host ~]# yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
Modular dependency problems:
Problem 1: conflicting requests
- nothing provides module(perl:5.26) needed by module perl-DBD-SQLite:1.58:8010020191114033549:073fa5fe-0.x86_64
Problem 2: conflicting requests
- nothing provides module(perl:5.26) needed by module perl-DBI:1.641:8010020191113222731:16b3ab4d-0.x86_64
No match for argument: docker
No match for argument: docker-client
No match for argument: docker-client-latest
No match for argument: docker-common
No match for argument: docker-latest
No match for argument: docker-latest-logrotate
No match for argument: docker-logrotate
No match for argument: docker-engine
No packages marked for removal.
Dependencies resolved.
Nothing to do.
Complete!
[root@host ~]#
接下来,我们要添加一个外部存储库来帮助我们获取 Docker 软件。
[root@host ~]# dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo
Adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
[root@host ~]#
现在,让我们检查一下我们在存储库中有哪些版本的 docker。
[root@host ~]# dnf list docker-ce
CentOS-8 - AppStream 7.7 kB/s | 4.3 kB 00:00
CentOS-8 - Base 32 kB/s | 3.8 kB 00:00
CentOS-8 - Extras 4.0 kB/s | 1.5 kB 00:00
Docker CE Stable - x86_64 55 kB/s | 22 kB 00:00
Available Packages
docker-ce.x86_64 3:19.03.7-3.el7 docker-ce-stable
[root@host ~]#
不幸的是,最新版本的 Docker 并不总是在所有存储库中都可用。 为了解决这个问题并获得最合适、最稳定的发布版本,我们将使用“nobest”标志。 我们将使用此选项,因为它将允许 CentOS 8 安装最适合我们需要的版本。 如果我们需要安装最新版本的 docker,这个选项对我们来说不是正确的选择。 不过,我们会继续安装最符合我们 CentOS 服务器版本的版本。
[root@host ~]# dnf install docker-ce --nobest
CentOS-8 - AppStream 16 kB/s | 4.3 kB 00:00
CentOS-8 - Base 10 kB/s | 3.8 kB 00:00
CentOS-8 - Extras 12 kB/s | 1.5 kB 00:00
Docker CE Stable - x86_64 15 kB/s | 3.5 kB 00:00
Dependencies resolved.
Problem: package docker-ce-3:19.03.7-3.el7.x86_64 requires containerd.io >= 1.2.2-3, but none of the providers can be installed
- cannot install the best candidate for the job
- package containerd.io-1.2.10-3.2.el7.x86_64 is excluded
- package containerd.io-1.2.13-3.1.el7.x86_64 is excluded
- package containerd.io-1.2.2-3.3.el7.x86_64 is excluded
- package containerd.io-1.2.2-3.el7.x86_64 is excluded
- package containerd.io-1.2.4-3.1.el7.x86_64 is excluded
- package containerd.io-1.2.5-3.1.el7.x86_64 is excluded
- package containerd.io-1.2.6-3.3.el7.x86_64 is excluded
============================================================================
Package Arch Version Repository Size
============================================================================
Installing:
docker-ce x86_64 3:18.09.1-3.el7 docker-ce-stable 19 M
Installing dependencies:
libcgroup x86_64 0.41-19.el8 BaseOS 70 k
containerd.io x86_64 1.2.0-3.el7 docker-ce-stable 22 M
docker-ce-cli x86_64 1:19.03.7-3.el7 docker-ce-stable 40 M
Skipping packages with broken dependencies:
docker-ce x86_64 3:19.03.7-3.el7 docker-ce-stable 25 M
Transaction Summary
============================================================================
Install 4 Packages
Skip 1 Package
Total download size: 80 M
Installed size: 339 M
Is this ok [y/N]: y
Downloading Packages:
(1/4): libcgroup-0.41-19.el8.x86_64.rpm 2.0 MB/s | 70 kB 00:00
(2/4): docker-ce-18.09.1-3.el7.x86_64.rpm 2.9 MB/s | 19 MB 00:06
(3/4): containerd.io-1.2.0-3.el7.x86_64.rpm 2.9 MB/s | 22 MB 00:07
(4/4): docker-ce-cli-19.03.7-3.el7.x86_64.rpm 4.9 MB/s | 40 MB 00:08
----------------------------------------------------------------------------
Total 9.7 MB/s | 80 MB 00:08
warning: /var/cache/dnf/docker-ce-stable-091d8a9c23201250/packages/containerd.io-1.2.0-3.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
Docker CE Stable - x86_64 8.4 kB/s | 1.6 kB 00:00
Importing GPG key 0x621E9F35:
Userid : "Docker Release (CE rpm) <[email protected]>"
Fingerprint: 060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35
From : https://download.docker.com/linux/centos/gpg
Is this ok [y/N]: y
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : docker-ce-cli-1:19.03.7-3.el7.x86_64 1/4
Running scriptlet: docker-ce-cli-1:19.03.7-3.el7.x86_64 1/4
The fast memory caches was not invalidated by NSS responder.
Installing : containerd.io-1.2.0-3.el7.x86_64 2/4
Running scriptlet: containerd.io-1.2.0-3.el7.x86_64 2/4
Running scriptlet: libcgroup-0.41-19.el8.x86_64 3/4
Installing : libcgroup-0.41-19.el8.x86_64 3/4
Running scriptlet: libcgroup-0.41-19.el8.x86_64 3/4
Running scriptlet: docker-ce-3:18.09.1-3.el7.x86_64 4/4
Installing : docker-ce-3:18.09.1-3.el7.x86_64 4/4
Running scriptlet: docker-ce-3:18.09.1-3.el7.x86_64 4/4
Verifying : libcgroup-0.41-19.el8.x86_64 1/4
Verifying : containerd.io-1.2.0-3.el7.x86_64 2/4
Verifying : docker-ce-3:18.09.1-3.el7.x86_64 3/4
Verifying : docker-ce-cli-1:19.03.7-3.el7.x86_64 4/4
Installed:
docker-ce-3:18.09.1-3.el7.x86_64 libcgroup-0.41-19.el8.x86_64
containerd.io-1.2.0-3.el7.x86_64 docker-ce-cli-1:19.03.7-3.el7.x86_64
Skipped:
docker-ce-3:19.03.7-3.el7.x86_64
Complete!
[root@host ~]#
验证 Docker 版本
现在,我们可以使用 version 命令再次检查安装是否成功。
[root@host ~]# docker -v
Docker version 19.03.7, build 7141c199a2
[root@host ~]#
启动 Docker
接下来,让我们使用 systemctl 命令启动 Docker 守护进程。
[root@host ~]# systemctl enable --now docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
[root@host ~]#
确认安装
接下来,我们终于可以确认 Docker 已经启动并正在使用以下命令。
[root@host ~]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor pres>
Active: active (running) since Thu 2020-03-05 06:30:02 EST; 2min 30s ago
Docs: https://docs.docker.com
Main PID: 2798 (dockerd)
Tasks: 21
Memory: 140.7M
CGroup: /system.slice/docker.service
├─2798 /usr/bin/dockerd -H fd://
└─2805 containerd --config /var/run/docker/containerd/containerd.tom>
Mar 05 06:29:52 localhost.localdomain dockerd[2798]: time="2020-03-05T06:29:52.>
Mar 05 06:29:52 localhost.localdomain dockerd[2798]: time="2020-03-05T06:29:52.>
Mar 05 06:29:52 localhost.localdomain dockerd[2798]: time="2020-03-05T06:29:52.>
Mar 05 06:29:52 localhost.localdomain dockerd[2798]: time="2020-03-05T06:29:52.>
Mar 05 06:29:57 localhost.localdomain dockerd[2798]: time="2020-03-05T06:29:57.>
Mar 05 06:29:59 localhost.localdomain dockerd[2798]: time="2020-03-05T06:29:58.>
Mar 05 06:30:01 localhost.localdomain dockerd[2798]: time="2020-03-05T06:30:01.>
Mar 05 06:30:01 localhost.localdomain dockerd[2798]: time="2020-03-05T06:30:01.>
Mar 05 06:30:02 localhost.localdomain dockerd[2798]: time="2020-03-05T06:30:02.>
Mar 05 06:30:02 localhost.localdomain systemd[1]: Started Docker Application Co>
[root@host ~]#
添加用户
下一步是使用“usermod”命令将用户添加到 Docker 用户组。
此外,当我们在添加用户后使用“id root”命令时,这将显示该用户现在是否包含在 docker 组中。
[root@host ~]# usermod -aG docker root
[root@host ~]# id root
uid=0(root) gid=0(root) groups=0(root),976(docker)
[root@host ~]#
禁用防火墙
接下来,我们将暂时关闭 firewalld 以允许 Docker 容器在内部工作。
[root@host ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@host ~]#
注意:我们建议您在此步骤之后执行重新启动。
部署测试应用
在最后一步中,我们将安装一个名为“hello-world”的测试应用程序,以确保我们的安装按预期工作。 我们将使用以下“docker”命令部署程序。
[root@host ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:fc6a51919cfeb2e6763f62b6d9e8815acbf7cd2e476ea353743570610737b752
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/
[root@host ~]#
安装 Docker Compose
作为附加步骤,我们将安装 Docker Compose。 Docker Compose 是一个非常有用的应用程序,它允许我们使用最少的命令将多个容器连接在一起,或者当我们需要一起运行多个相互依赖的容器时。
我们将使用以下“dnf”命令开始安装。
[root@host ~]# dnf install curl -y
Last metadata expiration check: 0:42:26 ago on Thu 05 Mar 2020 06:08:47 AM EST.
Package curl-7.61.1-11.el8.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
[root@host ~]#
获取 Docker Compose 包
我们将从 GitHub 获取 Docker Compose 包。
[root@host ~]# curl -L "https://github.com/docker/compose/releases/download/1.25.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 617 100 617 0 0 1672 0 --:--:-- --:--:-- --:--:-- 1672
100 16.2M 100 16.2M 0 0 3914k 0 0:00:04 0:00:04 --:--:-- 4990k
[root@host ~]
接下来,我们将使用“chmod”命令调整文件的权限以使其可执行。
[root@host ~]# chmod +x /usr/local/bin/docker-compose
[root@host ~]#
要检查我们安装了哪个版本,请运行以下命令。
[root@host ~]# docker-compose -v
docker-compose version 1.25.0, build 0a186604
[root@host ~]#
结论
在本文中,我们讨论了 Docker 的基本概念以及如何在 CentOS 8 中安装它。此外,我们还安装了 Docker compose。 当需要现代容器化平台时,Docker 是最有用的工具之一。
如果您想了解更多关于如何最好地利用这项新技术的信息,请致电 800.580.4985,或打开 聊天 或与我们联系,与我们知识渊博的解决方案或经验丰富的托管顾问之一交谈!