Centos

如何在CentOS 8/RHEL 8上安装Docker CE

luobo · 1月22日 · 2020年 · ·

今天重新在虚拟机中安装了Centos 8,想利用docker-compose 来制作一个LNMP的web开发环境,但是发现在Centos 8中docker的源已经被删除,在Centos 8 中Docker已替换为podmanbuildah,所以在官方的Centos 8 安装Docker的教程已经不适用。

如果你不想切换到podmanbuildah,跟着我一起学一下如何在Centos 8 的系统中安装Docker CE 和 docker-compose。

Docker 分为两个版本:

  • Docker CE (社区版)
  • Enterprise Edition (企业版)

在这里我们分享的是如何在Centos 8中安装Docker CE 社区版。

前提要求

在安装之前请确认你是否符合一下条件:

  • 系统版本最低CentOS 8 / RHEL 8
  • 具有Sudo或root 权限
  • 希望可以连接网络

在CentOS 8 / RHEL 8服务器安装Docker CE

如上所述,在CentOS 8或RHEL 8软件包存储库上不再提供docker软件包,因此请运行以下dnf命令以启用Docker CE软件包存储库。

[[email protected] ~]# dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo

成功配置docker ce repo后,请运行以下命令以验证可安装哪个版本的Docker:

[[email protected] ~]# dnf list docker-ce
Available Packages
docker-ce.x86_64              3:19.03.5-3.el7                    docker-ce-stable
[[email protected]~]#

现在,使用下面的dnf命令安装最新版本的docker,

[[email protected] ~]# dnf install docker-ce --nobest -y

不得不吐槽一下这烂网络,国内环境安装过程要十几分钟,最后可能输出如下信息:

Docker CE Stable - x86_64                                                                                                           1.6 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
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.5-3.el7.x86_64                                                                                                       1/4
  Running scriptlet: docker-ce-cli-1:19.03.5-3.el7.x86_64                                                                                                       1/4
  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
  Installing       : container-selinux-2:2.107-2.module_el8.1.0+237+63e26edc.noarch                                                                             3/4
  Running scriptlet: container-selinux-2:2.107-2.module_el8.1.0+237+63e26edc.noarch                                                                             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        : container-selinux-2:2.107-2.module_el8.1.0+237+63e26edc.noarch                                                                             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.5-3.el7.x86_64                                                                                                       4/4

Installed:
  docker-ce-3:18.09.1-3.el7.x86_64               container-selinux-2:2.107-2.module_el8.1.0+237+63e26edc.noarch           containerd.io-1.2.0-3.el7.x86_64
  docker-ce-cli-1:19.03.5-3.el7.x86_64

Skipped:
  docker-ce-3:19.03.5-3.el7.x86_64

Complete!
[[email protected] ~]#

安装Docker之后,使用以下systemctl命令启动并启用其服务:

[[email protected] ~]# systemctl start docker
[[email protected] ~]# systemctl enable docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.

运行以下命令以验证已安装的Docker版本

[[email protected] ~]# docker --version
Docker version 19.03.5, build 633a0ea
[[email protected] ~]#

跟大多数教程一样我们要验证docker ce引擎是否已正确设置,请尝试使用以下docker命令启动hello-world容器,

[[email protected] ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:9572f7cdcee8591948c2963463447a53466950b3fc15a247fcad1917ca215a2f
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/

[[email protected] ~]#

我们可以在上面的输出中看到参考消息,确认Docker引擎已在CentOS 8 / RHEL 8服务器上正确设置。 让我们转到docker compose安装。

在CentOS 8 / RHEL 8服务器上安装Docker-compose

众所周知,docker compose用于通过单个命令链接多个容器。 换句话说,当我们需要启动多个容器并且这些容器相互依赖时,Docker Compose很有用。 诸如启动LAMP LEMP应用程序(WordPress和Joomla等)内部容器的示例。

安装curl

[[email protected] ~]# dnf install -y curl
Failed to set locale, defaulting to C.UTF-8
Last metadata expiration check: 0:29:15 ago on Wed Jan 22 01:01:08 2020.
Package curl-7.61.1-11.el8.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
[[email protected] ~]#

运行以下命令以在CentOS 8 / RHEL 8上安装docker compose

curl -L https://github.com/docker/compose/releases/download/v1.25.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

继续吐槽这垃圾网络,GitHub也不让访问:

curl: (7) Failed to connect to github-production-release-asset-2e65be.s3.amazonaws.com port 443: Connection refused

但是总有办法治他是不?最简单的就是hosts制定GitHub的IP:

vim /etc/hosts

把一下内容粘贴到最后:

151.101.44.249 github.global.ssl.fastly.net
192.30.253.113 github.com
103.245.222.133 assets-cdn.github.com
23.235.47.133 assets-cdn.github.com
203.208.39.104 assets-cdn.github.com
204.232.175.78 documentcloud.github.com
204.232.175.94 gist.github.com
107.21.116.220 help.github.com
207.97.227.252 nodeload.github.com
199.27.76.130 raw.github.com
107.22.3.110 status.github.com
204.232.175.78 training.github.com
207.97.227.243 www.github.com
185.31.16.184 github.global.ssl.fastly.net
185.31.18.133 avatars0.githubusercontent.com
185.31.19.133 avatars1.githubusercontent.com

再次运行安装命令:

[[email protected] ~]# curl -L https://github.com/docker/compose/releases/download/v1.25.2/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    0   617    0     0    456      0 --:--:--  0:00:01 --:--:--   455
100 16.4M  100 16.4M    0     0  26838      0  0:10:41  0:10:41 --:--:-- 27440
[[email protected] ~]#

在本文发表是docker-compose 的版本为1.25.2,你安装的时候可以到GitHub上看一下最新版本是什么。

给他docker-compose 加上可执行权限:

[[email protected] ~]# chmod +x /usr/local/bin/docker-compose

下面确认一下docker-compose 的版本:

[[email protected] ~]# docker-compose --version
docker-compose version 1.25.2, build 698e2846
[[email protected] ~]#

以上就是本教程的全部内容,我希望这些步骤可以帮助您在CentOS 8和RHEL 8服务器上设置docker和docker compose。 请在下面的评论部分中分享您的反馈和评论。

0 条回应