How to install and use Docker on AlmaLinux 8

Docker is a powerful platform for developers and system administrators that simplifies the process of deploying applications inside software containers. Containers allow you to package your application and all its parts (code, runtime, system tools, system libraries - anything usually located in /usr/bin or /usr/lib) so that it can be run consistently on any Linux machine to run. This includes the operating system kernel and other shared resources such as memory and disk space. Docker provides a portable environment for development and production environments. You can create containers from a set of files that can be used anywhere else without worrying about dependencies being different on each new server.

Docker CE is useful for Linux users as it helps them create their own environment without affecting other users on the system. It also automates deployment, eliminating configuration errors, and makes it easy to manage projects across development teams developing software applications together.

In this guide, we will see how to install Docker CE to create and manage a development environment on an AlmaLinux 8 system.

prerequisites

In order to use this article, you need the following:

  • A running AlmaLinux 8 system.
  • At least 15GB of free disk space. Each Docker container that is launched has one or more vCPUs at its disposal.
  • A non-root user with sudo privileges.

Step 1: Update the system

There are some security updates that help protect your system from malware and other attacks on your computer. There are also kernel updates that add new features or improve performance for hardware devices such as video cards and USB controllers.

These can be installed via the dnf update command on AlmaLinux 8.

sudo dnf -y update

Step 2: Add the Docker CE repository

For Red Hat-based Linux systems, there is an open Docker CE repository that contains rpm packages for installation. Before we can install Docker CE on AlmaLinux 8, we need to add this repository.

To add Docker CE repository to Rocky Linux 8 system, execute the commands listed below.

sudo dnf install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

To confirm that the repository was added correctly you can run this command.

sudo dnf repolist

第 3 步:安装 Docker CE

现在我们已经添加了存储库,我们可以使用它来安装 Docker CE。

sudo dnf -y update
sudo dnf -y install docker-ce docker-ce-cli containerd.io

此命令将为 AlmaLinux 8 安装最新的 docker 包。它安装 Docker CE,其中包括 Docker、容器和命令行工具。

安装完成后,您可以使用此命令启动 Docker CE。

sudo systemctl start docker

运行该命令可以查看服务是否启动成功。

sudo systemctl status docker

您应该会看到以下输出,这意味着一切正常。

检查 Docker 守护进程的状态

如果您希望 Docker CE 在 AlmaLinux 8 启动时自动启动,请运行此命令。

sudo systemctl enable docker

第 4 步:将非 root 用户添加到 Docker 组

Docker CE 使用虚拟化,需要以特权用户身份运行。 Docker 只能由 root 用户访问,这一点很重要。 要在 AlmaLinux 8 上进行设置,我们需要将新的非 root 用户添加到 Docker 组。 否则,您可能无法访问虚拟化设施并遇到权限被拒绝错误。 要将新的非 root 用户添加到 Docker 组,我们需要执行以下命令。

sudo usermod -aG docker $USER

其中: $USER 是您的非 root 用户用户名。 在这 example,让我们添加一个名为 vitux 的用户。

运行此命令后,注销并重新登录系统。 这将确保正确应用更改。

此时,您可以通过运行此命令来验证非 root 用户是否是 Docker 组的成员。

id vitux

步骤 5:测试 Docker CE 安装

现在我们已经安装了 Docker CE,是时候测试一切是否按预期工作了。

为此,我们需要一个用于测试的容器映像。 幸运的是,已经有一个图像可用于测试目的。 让我们通过运行以下命令来运行 hello-world 容器来测试安装。

sudo docker pull hello-world
sudo docker run hello-world

此命令将从 Docker 中心拉取最新的 hello-world 映像并将其作为容器运行。 它写 来自 Docker 的你好! 在您的终端上显示消息并退出,如下所示。

拉取并运行 Docker 镜像

此输出确认安装成功。

如果不是,那就是Docker包有问题,或者用户没有加入Docker组。

第 6 步:出于开发目的运行 Docker 容器

现在 Docker CE 已启动并运行,让我们将其用作 AlmaLinux 8 的开发环境。 当您在上一步中启动 hello-world 容器时,您正在运行一个虚拟机 (VM),该虚拟机运行并在执行活动后离开. 它运行,发出 来自 Docker 的你好! 输出,并在完成后立即退出。

Docker 容器可能比这个默认值更有帮助 example. Docker 容器与 VM 相同,但有一个例外:它们的资源密集程度较低。

采取 example,使用 Docker 中心提供的最新 Ubuntu 映像运行容器。

docker pull ubuntu
docker run -it ubuntu

此命令将拉取 Ubuntu 的最新映像,并将在交互式会话中运行(即,它保持连接到您的 AlmaLinux 8 终端),如下所示。

运行图像

您的命令提示符应更改为带有 id 的井号 (#)。 在这种情况下,它是 f5221423e0b9. 这表明容器已启动并正在运行,您可以在其中运行命令。

您可以运行没有前缀的任何命令 sudo 在容器内部,当您以 root 用户身份运行此容器时。 您在容器中所做的任何更改只会影响容器。 它不会影响您当前登录的操作系统(AlmaLinux 8)。

让我们运行 apt update 命令来更新包管理系统。

apt update

您应该会看到以下输出,这意味着一切正常。

更新图片

要退出容器,您可以在提示符下键入 exit 并点击 Enter.

结论

在本教程中,我们向您展示了如何在 AlmaLinux 8 系统上安装 Docker CE。 我们希望它帮助您安装 Docker,现在您已准备好使用它的各种工具。

有关 Docker 的更多信息,您可以查看 官方文件.

Guess you like

Origin blog.csdn.net/muzihuaner/article/details/130518697