Skip to content

通过知识获得解放,通过技术获得自由

Menu
  • 专题目录
  • 液压相关
    • 液压技术
    • 液压相邻技术
    • 液压应用
  • 计算机相关
    • 计算机和软件
    • 网络和网站技术
  • 哲学
  • 关于本站
Menu

centos8 安装docker[0]

Posted on 2023年11月14日 by
鉴于在linux不同的发行版间运行软件的困难性,在不采用完全虚拟机的情况下,linux社区开发除了两种解决方案,一种采用appimage(linux使用appimage),另一种是docker容器。两种都是类似windows下的便携软件的解决方案(在x86架构下的移植性)。其中docker是应用最为广泛的vps下的便携软件的解决方案,在docker中可以虚拟出另外一个操作系统,和主机的交互采用端口映射的方式,且性能牺牲较少。以上两种解决方案豆避免了在常见的x86系统下的重复编译的问题。

在 RHEL 8 / CentOS 8上安装 Docker CE

最新版的 RHEL 8 / CentOS 8,Red Hat 已经构建了自己的工具,buildah和podman。 这些工具可以兼容现有的 docker 镜像,并且工作的时候不需要和守护进程进行交互。它们允许像正常用户一样创建容器,而不需要申请特殊权限。(还是有一点限制:例如,在写文章的时候,没有权限的情况下,还是不能映射容器端口。)

一些特殊工具,不管怎么样,还是缺失的:docker-compose,目前就还不存在。在这篇教程中,我们将会看看通过使用适用于 CentOS7 的 Docker 官方源仓库,如何在 Rhel8 上安装和运行原来的 Docker CE。

在这篇教程中,你将会学习到:

  • 在 RHEL 8 / CentOS 8 上如何启用 docker-ce 源仓库
  • 在 RHEL 8 / CentOS 8 上如何安装 docker 和 docker-compose

一、软件要求 和 使用的约定

二、Docker 是什么?

Docker 是一个开源项目,它允许你在把应用的创建和发布和容器捆绑在一起。容器是一种标准化环境,它独立主机系统,并且可以很容易被复制。在 Red Hat Enterprise Linux 7 中,Docker 得到官方支持。同时,Red Hat 自己独立开发了一系列工具:buildah和podman来支持 Docker。

通过使用第三方源仓库,我们还可以安装 Docker CE(社区版本)。在这篇指南中,我们将会安装这个源仓库。请注意,对于CentOS 7(一个 RHEL 克隆版本)来说,这意味着没有像 Red Hat Enterprise Linux 那样的官方支持。因为这个,存在问题-后面我们会具体讨论。

三、添加第三方源

因为 Docker 在 RHEL 8 / CentOS 8 中还不可用,我们需要添加第三方源来获取这个软件。在这里我们将会使用官方 Docker CE CentOS 源:在写这篇文章的时候,这是唯一在 RHEL 8 / CentOS 8 上安装 Docker CE 的方法。

dnf config-manager工具可以让我们很容易的在我们的 Linux 发行版中启用或者禁用一个源,而不用担心其他琐事。默认情况下,仅仅appstream和baseos源在 Rhel8 上被启用了。我们需要添加并且启用docker-ce源。为了完成这个任务,我们需要做的就是运行下面的命令:

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

我们可以通过查看下面命令的输出,来验证这个源是否被成功启用。

sudo dnf repolist -v

这个命令默认会返回所有被启用的源仓库的详细信息。此时你可能看到下面的输出:

Repo-id      : docker-ce-stable
Repo-name    : Docker CE Stable - x86_64
Repo-revision: 1549905809
Repo-updated : Mon 11 Feb 2019 06:23:29 PM CET
Repo-pkgs    : 30
Repo-size    : 618 M
Repo-baseurl : https://download.docker.com/linux/centos/7/x86_64/stable
Repo-expire  : 172,800 second(s) (last: Mon 18 Feb 2019 10:23:54 AM CET)
Repo-filename: /etc/yum.repos.d/docker-ce.repo

Repo-id      : rhel-8-for-x86_64-appstream-rpms
Repo-name    : Red Hat Enterprise Linux 8 for x86_64 - AppStream Beta (RPMs)
Repo-revision: 1542158694
Repo-updated : Wed 14 Nov 2018 02:24:54 AM CET
Repo-pkgs    : 4,594
Repo-size    : 4.9 G
Repo-baseurl : https://cdn.redhat.com/content/beta/rhel8/8/x86_64/appstream/os
Repo-expire  : 86,400 second(s) (last: Mon 18 Feb 2019 10:23:55 AM CET)
Repo-filename: /etc/yum.repos.d/redhat.repo

Repo-id      : rhel-8-for-x86_64-baseos-rpms
Repo-name    : Red Hat Enterprise Linux 8 for x86_64 - BaseOS Beta (RPMs)
Repo-revision: 1542158719
Repo-updated : Wed 14 Nov 2018 02:25:19 AM CET
Repo-pkgs    : 1,686
Repo-size    : 925 M
Repo-baseurl : https://cdn.redhat.com/content/beta/rhel8/8/x86_64/baseos/os
Repo-expire  : 86,400 second(s) (last: Mon 18 Feb 2019 10:23:56 AM CET)
Repo-filename: /etc/yum.repos.d/redhat.repo
Total packages: 6,310

3.1安装 docker-ce

docker-ce-stable源现在已经在你的系统上被启用了。这个源仓库包含了好几个版本的docker-ce包。想要显示他们,我们可以运行:

dnf list docker-ce --showduplicates | sort -r
docker-ce.x86_64            3:19.03.2-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.1-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.0-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.9-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.8-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.7-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.6-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.5-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.4-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.3-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.2-3.el7                     docker-ce-stable
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.3.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.06.2.ce-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
docker-ce.x86_64            18.03.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            18.03.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.12.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.12.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.09.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.09.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.2.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.03.3.ce-1.el7                    docker-ce-stable
docker-ce.x86_64            17.03.2.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.03.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.03.0.ce-1.el7.centos             docker-ce-stable

我们要安装哪个版本?当docker-ce依赖的containerd.io版本号大于1.2.0-3.el7时,不知道被什么原因终端了安装进程。因此,简单的运行sudo dnf install docker-ce命令,不会正常运行。解决完这个问题,我们还会碰到另外一个问题。由于系统防火墙管理器firewalld被启用,Docker 容器中的DNS resolution将无法正常运行。

当然,这是一个严重问题。但是,不管怎样,如果你还想继续安装 Docker 的话,这里还是有一些办法可以避免依赖问题的:

  • 安装指定版本的docker-ce,这些版本的 docker 只要求可安装的containerd.io软件包
  • 通过提供--nobest选项,强制安装过程进行下去。
  • 手动安装最新可用的containerd.iorpm 软件包

3.1.1、安装指定版本的 docker-ce

在我们写这篇文章的时候,可以安装没问题的docker-ce版本是:

  • docker-ce-3:18.09.1-3.el7
  • docker-ce-18.06.3.ce-3.el7;
  • docker-ce-17.12.1.ce-1.el7.centos

想要安装指定版本,我们只需要提供软件包全名,例如:

$ sudo dnf install docker-ce-3:18.09.1-3.el7

3.1.2 使用 –nobest 选项强制安装 docker-ce

正常情况下,当安装一个软件包时,最好的可用版本被从源仓库中选出来。在这个例子中,我们尝试安装最新版本的docker-ce,但是失败了。 通过使用--nobest选项,我们可以将这个行为,使得满足依赖条件的第一个docker-ce版本被选出来。这里,我们选出了3:18.09.1-3.el7。

$ sudo dnf install --nobest docker-ce
Dependencies resolved.

Problem: package docker-ce-3:19.03.2-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.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:
 containerd.io                      x86_64       1.2.0-3.el7                                              docker-ce-stable                        22 M
 docker-ce-cli                      x86_64       1:19.03.2-3.el7                                          docker-ce-stable                        39 M
 container-selinux                  noarch       2:2.94-1.git1e99f1d.module+el8.0.0+4017+bbba319f         rhel-8-for-x86_64-appstream-rpms        43 k
 tar                                x86_64       2:1.30-4.el8                                             rhel-8-for-x86_64-baseos-rpms          838 k
 libcgroup                          x86_64       0.41-19.el8                                              rhel-8-for-x86_64-baseos-rpms           70 k
 python3-policycoreutils            noarch       2.8-16.1.el8                                             rhel-8-for-x86_64-baseos-rpms          2.2 M
 python3-libsemanage                x86_64       2.8-5.el8                                                rhel-8-for-x86_64-baseos-rpms          127 k
 python3-setools                    x86_64       4.2.0-2.el8                                              rhel-8-for-x86_64-baseos-rpms          598 k
 checkpolicy                        x86_64       2.8-2.el8                                                rhel-8-for-x86_64-baseos-rpms          338 k
 python3-audit                      x86_64       3.0-0.10.20180831git0047a6c.el8                          rhel-8-for-x86_64-baseos-rpms           85 k
 policycoreutils-python-utils       noarch       2.8-16.1.el8                                             rhel-8-for-x86_64-baseos-rpms          228 k
Skipping packages with broken dependencies:
 docker-ce                          x86_64       3:19.03.2-3.el7                                          docker-ce-stable                        24 M

Transaction Summary
=======================================================================================================================================================
Install  12 Packages
Skip      1 Package

Total download size: 85 M
Installed size: 351 M
Is this ok [y/N]:

3.1.3 手动安装最新可用的 containerd.io 软件包

如果我们需要安装最新版的docker-ce,我们可以根据需求版本号,来手动安装containerd.io,运行:

$ sudo dnf install https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.2.6-3.3.el7.x86_64.rpm

等待这个软件包安装完成后,我们就可以很简单的安装最新的docker-ce:

$ sudo dnf install docker-ce
Dependencies resolved.
=======================================================================================================================================================
  Package                          Arch                      Version                             Repository                                        Size
=======================================================================================================================================================
Installing:
  docker-ce                        x86_64                    3:19.03.2-3.el7                     docker-ce-stable                                  24 M
Installing dependencies:
  docker-ce-cli                    x86_64                    1:19.03.2-3.el7                     docker-ce-stable                                  39 M
  tar                              x86_64                    2:1.30-4.el8                        rhel-8-for-x86_64-baseos-rpms                    838 k
  libcgroup                        x86_64                    0.41-19.el8                         rhel-8-for-x86_64-baseos-rpms                     70 k

Transaction Summary
=======================================================================================================================================================
Install  4 Packages

Total download size: 65 M
Installed size: 275 M
Is this ok [y/N]:

这个选择不太方便,因为containerd.io不是作为docker-ce的依赖来安装的。如果以后从系统中卸载 Docker 的时候,containerd.io也不会被自动移除。

不管我们通过什么方法安装docker-ce,就像我们前面说的,为了保证 Docker 容器内部的DNS resolution正常运行,我们需要禁用 firewalld(这里可能需要系统重新启动)

$ sudo systemctl disable firewalld

四、 开启和启用 docker 守护进程

一旦docker-ce安装完成,我们必须启动并且启用 Docker 守护进程。这样做,下次系统启动时,Docker 会跟随系统自动启动。我们需要运行下面的命令:

$ sudo systemctl enable --now docker

此时,我们通过运行下面的命令,来确认守护进程是否正常运行:

$ systemctl is-active docker
active

类似的,我们还可以运行下面的命令,来确认它是否会随系统启动:

$ systemctl is-enabled docker
enabled

五、 安装 docker-compose

Docker compose 是一个非常有用的软件包,它可以让我们管理多个容器的应用。例如,那些基于LAMP的应用,环境中的每一部分(PHP, Apache, MariaDB)都由一个明确的容器提供。这个软件包在 Rhel8 中不可用,也没有类似的 Rhel tools 工具可以用。不管怎样,我们还有很多方式可以安装它:请继续阅读,并选择适合你的方式:

5.1 全局安装

我们应该通过什么方式来安装docker-compose,取决于我们是要全局安装它还是仅仅为了单个用户安装它。在写这篇文章的时候,唯一全局安装的方法就是:从项目的 Github 页面下载二进制包:

$ curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o docker-compose

一旦二进制包下载完成,我们将它移动到/usr/local/bin目录,并且让它可执行。

$ sudo mv docker-compose /usr/local/bin && sudo chmod +x /usr/local/bin/docker-compose

这个/usr/local不是随机选的。这个目录被用来存放本地管理员手动安装的软件(例如源码编译的软件),同时为了确保和系统软件管理器安装的软件分隔开来。

虽然一个普通用户如果加入了docker用户组(这个组在安装 docker-ce 的时候自动创建)的话,可以运行 docker 相关的命令,但是因为安全原因,他们必须要 root 权限运行。因为/usr/local/bin不在 root 用户的PATH环境变量中,我们需要指定二进制软件的位置,或者将/usr/local/bin添加到PATH中。在这里我们推荐前面的方式。

5.2 单用户安装

如果我们的用户是docker用户组的成员,我们可以运行 docker 命令。因为docker-composeh 还提供了 python 软件包,因此我们也能使用pip来安装它。首先确认 pip 已经被安装:

$ sudo dnf install python3-pip

为了获取 docker-compose,我们运行:

$ pip3.6 install docker-compose --user

请注意,即使我们可以使用 root 运行 pip 的方式来全局安装这个包(docker-compose),这种做法也是不被推荐,不被鼓励的。

六、 测试 Docker

我们安装了 docker 和 docker-compose,现在检查所有东西是否按照期望的那样正常运行。我们可以尝试构建一个镜像,并且运行一个容器。在这里我们使用官方的httpd镜像。我们需要做的就是运行下面的命令:

sudo docker run --rm --name=linuxconfig-test -p 80:80 httpd

因为httpd镜像本地不存在,因此它会被自动获取,构建。最后,一个基于它的容器就在前台运行起来了(它会在停止的时候被自动移除)。当我们在本地电脑的浏览器上输入本机 ip 时,应该能看到消息”It works!”。

七、总结

Red Hat Enterprise Linux 8 不支持 Docker:在这个发行版上,Docker 已经被 Red Hat 自己的工具buildah和podman所替代。这些工具兼容于 Docker,但是不需要服务器/客户端这种架构来运行。使用本地工具,通常是一种推荐的方式,但是由于某些原因你仍然想安装原版 Docker。在这篇教程中,我们看到如何使用 Docker 官方制作的针对 CentOS 7 的源仓库,在 Rhel8 上安装Docker CE(100%兼容)。

这不是一个完美的解决方案,有时候我们需要手动操作,来保证 Docker 在 RHEL8 上正常运行。如果使用过程中发现了问题,或者找到了更好的解决方案,我们会更新这篇文章。

八、部署portainer面板

docker run -d -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock --restart=always --name prtainer portainer/portainer-ce

 

参考资料:

  1. https://cloud.tencent.com/developer/article/1626829

欢迎回来

希望本站对你有所帮助!

如有疑问请联系info@fanlog.org
2023 年 11 月
一二三四五六日
 12345
6789101112
13141516171819
20212223242526
27282930 
« 6 月    

AI辅助 (17)

© 2025 | Powered by Superbs Personal Blog theme
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT