在 Rocky Linux 8 上安装 Docker

欢迎阅读有关如何在 Rocky Linux 8 上安装 docker 的指南。 Docker 是一个平台,使开发人员和系统管理员能够 构建、运行和共享 容器的应用程序。

有两个版本的 docker 可用, Docker CEDocker EE

  • Docker(社区版)是 Docker 的开源、社区支持版本,可免费使用。
  • Docker EE(企业版)是 Docker 的商业/高级版本,由 Docker Inc 支持。

我们将学习如何在 Rocky Linux 8 上安装和使用 Docker CE。

在 Rocky Linux 8 上安装 Docker

您可以通过多种方法在 Rocky Linux 8 上安装 docker;

  • 从 Docker 存储库在 Rocky Linux 8 上安装 docker
  • 使用 RPM 二进制包在 Rocky Linux 8 上手动安装 docker
  • 使用 Docker 安装脚本安装 docker (超出本教程的范围)。

从 Docker 存储库在 Rocky Linux 8 上安装 Docker

卸载旧的 Docker 版本

如果您的系统上安装了任何旧的 docker 版本,请确保在继续之前删除它们及其依赖项。

dnf remove docker*

在 Rocky Linux 8 上安装 docker Repository

为了确保未来 Docker 包的无缝升级,您可以从他们的官方存储库中安装它。 为此,您需要通过执行以下命令在 Rocky Linux 8 上设置 Docker 存储库;

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

上面的命令在 Rocky Linux 8 上安装并启用 docker repo。

在 Rocky Linux 8 上安装 Docker 引擎 (docker)

存储库就位后,继续安装 docker-ce 和其他工具,包括 containerd.io,基于 Rocky Linux 8 和 docker CLI 实用程序的开放且可靠的容器运行时, docker-ce-cli.

dnf install docker-ce

样本输出;

Dependencies resolved. ============================================================================================================================================================================  Package                                        Architecture             Version                                                   Repository                          Size ============================================================================================================================================================================ Installing:  docker-ce                                      x86_64                   3:20.10.7-3.el8                                           docker-ce-stable                    27 M Installing dependencies:  checkpolicy                                    x86_64                   2.9-1.el8                                                 baseos                             345 k  container-selinux                              noarch                   2:2.162.0-1.module+el8.4.0+556+40122d08                   appstream                           51 k  containerd.io                                  x86_64                   1.4.6-3.1.el8                                             docker-ce-stable                    34 M  docker-ce-cli                                  x86_64                   1:20.10.7-3.el8                                           docker-ce-stable                    33 M  docker-ce-rootless-extras                      x86_64                   20.10.7-3.el8                                             docker-ce-stable                   9.2 M  docker-scan-plugin                             x86_64                   0.8.0-3.el8                                               docker-ce-stable                   4.2 M  fuse-common                                    x86_64                   3.2.1-12.el8                                              baseos                              20 k  fuse-overlayfs                                 x86_64                   1.4.0-3.module+el8.4.0+556+40122d08                       appstream                           71 k  fuse3                                          x86_64                   3.2.1-12.el8                                              baseos                              49 k  fuse3-libs                                     x86_64                   3.2.1-12.el8                                              baseos                              93 k  libcgroup                                      x86_64                   0.41-19.el8                                               baseos                              69 k  libslirp                                       x86_64                   4.3.1-1.module+el8.4.0+556+40122d08                       appstream                           68 k  policycoreutils-python-utils                   noarch                   2.9-14.el8                                                baseos                             251 k  python3-audit                                  x86_64                   3.0-0.17.20191104git1c2f876.el8.1                         baseos                              85 k  python3-libsemanage                            x86_64                   2.9-6.el8                                                 baseos                             126 k  python3-policycoreutils                        noarch                   2.9-14.el8                                                baseos                             2.2 M  python3-setools                                x86_64                   4.3.0-2.el8                                               baseos                             625 k  slirp4netns                                    x86_64                   1.1.8-1.module+el8.4.0+556+40122d08                       appstream                           50 k Enabling module streams:  container-tools                                                         rhel8                                                                                               Transaction Summary ============================================================================================================================================================================ Install  19 Packages  Total download size: 111 M Installed size: 449 M Is this ok [y/N]: y 

从 RPM 包安装 docker

您也可以使用 RPM 包在 Rocky Linux 8 上安装 docker。

从 Docker 包页面下载上面安装的各个包的最新版本。

相应地替换包的版本/发布号。

dnf install https://download.docker.com/linux/centos/8/x86_64/stable/Packages/docker-ce-20.10.7-3.el8.x86_64.rpm -y
dnf install -y https://download.docker.com/linux/centos/8/x86_64/stable/Packages/docker-ce-cli-20.10.7-3.el8.x86_64.rpm
dnf install -y https://download.docker.com/linux/centos/8/x86_64/stable/Packages/containerd.io-1.4.6-3.1.el8.x86_64.rpm

启动 Docker 服务

启动并启用 Docker 和 Containerd 服务以在系统启动时运行;

systemctl enable --now docker containerd

以非 root 用户身份运行 Docker

如果以标准用户身份运行 Docker,可能会出现这样的错误;

[[email protected] ~]$ docker ps -a
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get https://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/json: dial unix /var/run/docker.sock: connect: permission denied

因此,如果您需要以非 root 用户身份运行 docker,只需将您想要运行 docker 的用户添加到 docker 组或简单地授予用户 sudo 权利。 例如添加一个用户, koromicha, 到 docker 团体;

usermod -aG docker koromicha
groups koromicha
koromicha : koromicha docker

接下来,注销并以您添加到 docker 组的非 root 用户身份再次登录,以便可以重新评估您的组成员身份.

然后,您可以以非 root 用户身份运行 docker。

验证 docker 安装

检查docker版本;

docker --version
Docker version 20.10.7, build f0df350

要验证 docker 是否运行良好,您可以尝试运行 hello-world 容器图像。

[[email protected] ~]$ docker run hello-world
Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world b8dfde127a29: Pull complete  Digest: sha256:df5f5184104426b65967e016ff2ac0bfcd44ad7899ca3bbcf8e44e4461491a9e 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 ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES 4083300e3660        hello-world         "/hello"            3 minutes ago       Exited (0) 5 minutes ago                       laughing_visvesvaraya

在 Rocky Linux 8 上运行 Docker 镜像

在验证上面的 docker 安装时,我们运行了 hello-world docker 容器镜像。 构建和运行 docker 容器化应用程序是设置 docker 环境后的下一步。

所以这里有两件事:

  • 您可以构建和运行自己的 docker 容器镜像,或者
  • 从 docker hub 拉取已经创建和共享的镜像,docker hub 是一个用于 Docker 镜像的镜像仓库。

在这个演示中,我们将学习如何使用来自 docker hub 的已经创建的 docker 镜像。 使用 docker 命令,您可以轻松地从 docker hub 拉取 docker 镜像,就像我们使用 你好,世界 上图。

如果您想构建自己的镜像并在 docker hub 上共享,那么您需要注册创建自己的命名空间来存储您的镜像,然后可以公开访问。

使用 docker 命令

docker command 有很多命令行选项用于运行各种任务。

命令行语法是;

docker [OPTIONS] COMMAND

可以通过执行查看各种命令选项和命令;

docker --help
... Options:       --config string      Location of client config files (default "/home/koromicha/.docker")   -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use")   -D, --debug              Enable debug mode ... Management Commands:   builder     Manage builds   config      Manage Docker configs   container   Manage containers ... Commands:   attach      Attach local standard input, output, and error streams to a running container   build       Build an image from a Dockerfile   commit      Create a new image from a container's changes   cp          Copy files/folders between a container and the local filesystem   create      Create a new container ... 

查看特定 docker 命令的帮助;

docker COMMAND --help

比如查找docker的用法 run 命令;

docker run --help

从 Docker Hub 运行 Docker 镜像

在 Docker Hub 上搜索 Docker 镜像

您可以使用以下命令在 Docker Hub 上搜索特定的公开可用的 Docker 映像 docker search 命令。 docker search 命令的语法是;

docker search [OPTIONS] TERM

docker search --help 查看更多选项。

例如,搜索所有包含 hello-world 的图片;

docker search hello-world
NAME                                       DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED hello-world                                Hello World! (an example of minimal Dockeriz…   1479      [OK]        kitematic/hello-world-nginx                A light-weight nginx container that demonstr…   150                   tutum/hello-world                          Image to test docker deployments. Has Apache…   83                   [OK] dockercloud/hello-world                    Hello World!                                    19                   [OK] crccheck/hello-world                       Hello World web server in under 2.5 MB          15                   [OK] vad1mo/hello-world-rest                    A simple REST Service that echoes back all t…   5                    [OK] arm32v7/hello-world                        Hello World! (an example of minimal Dockeriz…   3                     ppc64le/hello-world                        Hello World! (an example of minimal Dockeriz…   2                     datawire/hello-world                       Hello World! Simple Hello World implementati…   1                    [OK] markmnei/hello-world-java-docker           Hello-World-Java-docker                         1                    [OK] thomaspoignant/hello-world-rest-json       This project is a REST hello-world API to bu…   1                     ansibleplaybookbundle/hello-world-db-apb   An APB which deploys a sample Hello World! a…   1                    [OK] ansibleplaybookbundle/hello-world-apb      An APB which deploys a sample Hello World! a…   1                    [OK] 

一些列标题是不言自明的。 其他列包括;

  • 星星 :显示图像的点赞数。
  • 官方的 : 指定映像是否从受信任的来源构建。
  • 自动化 :显示映像是否已从外部存储库中的源代码自动构建并自动推送到 Docker 存储库。

通常,没有前缀的镜像被认为是官方的 docker hub 镜像,稳定且正在维护。 它还包含 [OK]官方的 柱子。

拉取 Docker 镜像到本地注册表

一旦确定了需要运行的映像,您可以使用 docker pull 命令或简单地使用 docker run 命令。

例如,搜索并拉取一个 Ubuntu 系统 docker 镜像;

docker search ubuntu
NAME                                                      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED ubuntu                                                    Ubuntu is a Debian-based Linux operating sys…   12507     [OK]        dorowu/ubuntu-desktop-lxde-vnc                            Docker image to provide HTML5 VNC interface …   552                  [OK] websphere-liberty                                         WebSphere Liberty multi-architecture images …   276       [OK]        rastasheep/ubuntu-sshd                                    Dockerized SSH service, built on top of offi…   254                  [OK] consol/ubuntu-xfce-vnc                                    Ubuntu container with "headless" VNC session…   241                  [OK] ubuntu-upstart                                            Upstart is an event-based replacement for th…   111       [OK]        ansible/ubuntu14.04-ansible                               Ubuntu 14.04 LTS with ansible                   98                   [OK] neurodebian                                               NeuroDebian provides neuroscience research s…   84        [OK]        1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5      ubuntu-16-nginx-php-phpmyadmin-mysql-5          50                   [OK] open-liberty                                              Open Liberty multi-architecture images based…   47        [OK]        ubuntu-debootstrap                                        debootstrap --variant=minbase --components=m…   44        [OK]        i386/ubuntu                                               Ubuntu is a Debian-based Linux operating sys…   25                    solita/ubuntu-systemd                                     Ubuntu + systemd                                24                   [OK] ... 

让我们拉取官方的 Ubuntu 镜像。

docker pull ubuntu

这将默认拉取当前可用的最新版本的 Ubuntu,通常是最新的 LTS 版本。

Using default tag: latest latest: Pulling from library/ubuntu a31c7b29f4ad: Pull complete  Digest: sha256:b3e2e47d016c08b3396b5ebe06ab0b711c34e7f37b98c9d37abe794b71cea0a2 Status: Downloaded newer image for ubuntu:latest docker.io/library/ubuntu:latest 

不过,您可以为特定类型的 Ubuntu 指定其他标签;

docker search ubuntu:20.04

列出可用的 Docker 镜像

您可以使用列出本地可用的图像 docker images 命令。

docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE ubuntu        latest    c29284518f49   5 days ago     72.8MB hello-world   latest    d1165f221234   4 months ago   13.3kB

运行 Docker 容器镜像

从 Docker 集线器拉取容器并在本地可用后,您可以使用以下命令运行它 docker run 或者 docker create 命令。

docker run,您可以创建一个容器,启动它并使用 shell 访问它以在其中运行任何命令。 docker run 命令行语法是;

Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG…]

例如,为了运行我们的 Ubuntu 镜像,我们以交互方式从上面的 Docker hub 中提取(选项 -i) 下降到外壳 (选项 -t, 分配伪 TTY);

docker run -it ubuntu

这下降到外壳;

[email protected]:/#

你现在在一个 Ubuntu 容器中,有一个容器 ID, 9b93a21dac21, 作为 root 用户。 你在这个 shell 下运行或执行的任何事情都发生在容器本身的文件系统中。

可以直接执行命令,无需掉线到容器shell;

docker run ubuntu cat /etc/os-release
NAME="Ubuntu" VERSION="20.04.2 LTS (Focal Fossa)" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 20.04.2 LTS" VERSION_ID="20.04" HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" VERSION_CODENAME=focal UBUNTU_CODENAME=focal 

如果以交互方式运行容器并启动 shell,则可以在其中运行命令。

当您退出外壳时,连接到容器的外壳将分离并且容器停止。 如果在运行 docker 容器时使用了 -i 和 -t 选项,则可以在不停止容器的情况下退出 shell,使用按键 Ctrl + p 然后 Ctrl + q.

您也可以在后台运行容器并打印容器 ID

docker run -dit --name ubuntudemo ubuntu

将其附加到控制台;

docker attach ubuntudemo

列出 Docker 容器

您可以使用列出实时(当前正在运行)的容器 docker ps 命令。

docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED         STATUS         PORTS     NAMES a1a815bb0de6   ubuntu    "bash"    5 seconds ago   Up 2 seconds             ubuntu-demo

列出所有容器,那些正在运行的和那些已经退出的;

docker ps -a
CONTAINER ID   IMAGE         COMMAND                 CREATED              STATUS                          PORTS     NAMES a1a815bb0de6   ubuntu        "bash"                  26 seconds ago       Up 22 seconds                             ubuntu-demo 075d2a694b0c   ubuntu        "bash"                  About a minute ago   Exited (0) About a minute ago             ubuntudemo be8a66af520a   ubuntu        "cat /etc/os-release"   2 minutes ago        Exited (0) 2 minutes ago                  eloquent_dirac 9b93a21dac21   ubuntu        "bash"                  3 minutes ago        Exited (0) 2 minutes ago                  happy_elbakyan 61407c90dae8   hello-world   "/hello"                13 minutes ago       Exited (0) 13 minutes ago                 intelligent_gagarin 

启动和停止 Docker 容器

您可以使用启动退出/停止的容器 docker start 命令。

例如,要启动名为的容器, happy_elbakyan,ID 为 9b93a21dac21,运行命令;

docker start happy_elbakyan

或者

docker start 9b93a21dac21

同样,您可以使用名称或容器 ID 停止正在运行的 docker docker stop 命令。

例如;

docker stop ubuntudemo

删除 Docker 容器

完成容器后,您可以使用 docker rm 命令。

删除 ID 为的已停止容器 9b7867c1daad,例如,在我们的 docker ps -a 以上输出;

docker rm 9b7867c1daad

除非使用 using 强制删除,否则无法删除正在运行的容器 -f 或者 --force docker rm 命令的选项。

docker rm 1c280cf21322 -f

删除 Docker 镜像

Docker 镜像也可以使用删除 docker rmi 命令。 例如,您可以列出图像并获取要删除的特定图像的图像 ID;

docker images -a

您可以使用显示数字 IDS -q 选项。

docker images -a -q
74435f89ab78 bf756fb1ae65

删除图像;

docker rmi 74435f89ab78

或者使用名称 repo name;

docker rmi ubuntu

使用选项 -f 或者 --force 强制删除。

我们关于如何在 Rocky Linux 8 上安装 docker 的指南到此结束。

进一步阅读

Docker 文档

安装 ModSecurity 3 Apache 在 Docker 容器中

在 Docker 容器上部署单节点 Elastic Stack 集群

将 MariaDB 作为 Docker 容器安装和运行