如何安装和使用 Podman 在 Rocky Linux 8 上运行容器如何安装和使用 Podman 在 Rocky Linux 8 上运行容器如何安装和使用 Podman 在 Rocky Linux 8 上运行容器如何安装和使用 Podman 在 Rocky Linux 8 上运行容器
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2025年2月28日
类别
  • 未分类
标签

如何安装和使用 Podman 在 Rocky Linux 8 上运行容器

本教程适用于这些操作系统版本

  • 洛基Linux 8.4
  • 洛基Linux 8.4

在此页

  1. 先决条件
  2. 安装 Podman
  3. 搜索和下载容器镜像
  4. 运行容器
  5. 列出和停止容器
  6. 启动一个停止的容器
  7. 删除容器
  8. 杀死容器
  9. 删除图片
  10. 查看容器日志
  11. 检查容器
  12. 访问容器外壳
  13. 豆荚
  14. 结论

Podman 是一个免费的开源容器平台,用于在 Linux 环境中开发、管理和部署容器和 pod。 Redhat 在 2018 年开发了 Podman。它是一个与 Docker 工作方式不同的容器化引擎。 Podman 不依赖于守护进程来工作,这与使用 Docker CLI 和 Docker 守护进程的 Docker 不同。依赖守护进程会导致单点故障。

Podman 是根据 OCI(Open Container Initiative)标准设计的,允许 Podman 直接与内核、容器和镜像交互。它也比 Docker 更安全,因为它不需要 root 访问权限。 Podman 可以用作 Docker 的直接替代品,因为两者都符合 OCI。

本文将向您展示如何安装 Podman 并使用它来创建和管理图像和容器。

先决条件

  1. A Rocky Linux based server

  2. A non-sudo user with root privileges.

  3. Ensure that the server is updated.

    $ sudo dnf update
    

安装 Podman

Podman 与 Buildah 和 Skopeo 一起包含在 container-tools 模块中。它也可以在 Rocky Linux 8 的 AppStream 存储库中获得。我们将使用模块方法。

使用 dnf module 命令安装 Podman。

$ sudo dnf module install container-tools

检查 Podman 的版本,看是否安装正确。

$ podman --version
podman version 3.2.3

搜索并下载容器镜像

要搜索 Nginx 的图像,请使用以下命令。

$ podman search nginx

在输出中,您将看到图像来自的注册表的名称和图像的描述。

要下载图像,请使用以下命令之一。

$ podman pull docker.io/library/nginx

OR

$ podman pull nginx

您可以通过以下命令查看下载的图像。

$ podman images
REPOSITORY               TAG         IMAGE ID      CREATED     SIZE
docker.io/library/nginx  latest      f8f4ffc8092c  3 days ago  138 MB

运行容器

使用以下命令运行使用 Nginx 映像的容器。我们将容器命名为 webserver。

$ podman run -d --name webserver nginx 

我们可以使用相同的图像来启动另一个具有不同名称的容器。

$ podman run -d --name webserver2 nginx

我们可以使用同一个镜像启动无限数量的容器。

列出和停止容器

要列出所有正在运行的容器,请使用以下命令。

$ podman ps
CONTAINER ID  IMAGE                           COMMAND               CREATED         STATUS             PORTS       NAMES
19b6668bc627  docker.io/library/nginx:latest  nginx -g daemon o...  31 seconds ago  Up 31 seconds ago              webserver
35a286ba5a55  docker.io/library/nginx:latest  nginx -g daemon o...  2 seconds ago   Up 3 seconds ago               webserver2

停止正在运行的容器。

$ podman stop webserver
webserver

验证它是否已停止。

$ podman ps
CONTAINER ID  IMAGE                           COMMAND               CREATED             STATUS                 PORTS       NAMES
35a286ba5a55  docker.io/library/nginx:latest  nginx -g daemon o...  About a minute ago  Up About a minute ago              webserver2

要列出所有容器,包括已停止的容器,您需要使用 -a 标志。

$ podman ps -a
CONTAINER ID  IMAGE                           COMMAND               CREATED             STATUS                     PORTS       NAMES
19b6668bc627  docker.io/library/nginx:latest  nginx -g daemon o...  2 minutes ago       Exited (0) 35 seconds ago              webserver
35a286ba5a55  docker.io/library/nginx:latest  nginx -g daemon o...  About a minute ago  Up About a minute ago                  webserver2

启动一个停止的容器

使用以下命令启动已停止的容器。

$ podman start webserver
webserver

验证它是否已启动。

$ podman ps
CONTAINER ID  IMAGE                           COMMAND               CREATED        STATUS             PORTS       NAMES
19b6668bc627  docker.io/library/nginx:latest  nginx -g daemon o...  3 minutes ago  Up 16 seconds ago              webserver
35a286ba5a55  docker.io/library/nginx:latest  nginx -g daemon o...  2 minutes ago  Up 2 minutes ago               webserver2

删除容器

您需要在删除容器之前停止容器。

$ podman stop webserver2

删除容器。

$ podman rm webserver2

您可以使用 --force 标志删除正在运行的容器。

$ podman rm webserver2 --force
35a286ba5a553d5f88e3d9795780f893cfb58bf4a126c4912d1ec56b9d0e5a27

杀死容器

停止和杀死容器是两件不同的事情,但最终会达到同样的目的。不同之处在于,Stopping 是优雅地关闭容器,而 Killing 是强制结束容器,导致数据丢失。

使用以下命令终止容器。

$ podman kill -s 9 webserver2

上面的命令使用 SIGNAL 9 (SIGKILL) 选项来终止容器。

要终止所有容器,请使用 --all 或 -a 标志并仅终止最新的容器,请使用 --latest 或 -l 标志。

删除图像

您可以使用 rmi 命令删除图像。

$ podman rmi registry.redhat.io/rhel8/rsyslog

您可以使用逗号分隔多个图像来删除它们。

$ podman rmi registry.redhat.io/rhel8/rsyslog registry.redhat.io/ubi8/ubi

要删除系统上的所有图像,请使用 -a 标志。

$ podman rmi -a

查看容器日志

要查看容器日志,请使用以下命令。

$ podman logs webserver
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
......

您可以使用 --tail 选项将日志限制在最后 5 行。

$ podman logs --tail=5 webserver
2021/10/05 10:13:52 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6)
2021/10/05 10:13:52 [notice] 1#1: OS: Linux 4.18.0-305.19.1.el8_4.x86_64
2021/10/05 10:13:52 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 262144:262144
2021/10/05 10:13:52 [notice] 1#1: start worker processes
2021/10/05 10:13:52 [notice] 1#1: start worker process 23

默认情况下,您不会在日志中获得任何时间戳。使用 -t 标志将时间戳添加到您的日志中。

$ podman logs -t webserver
2021-10-05T09:25:02.026967459Z /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
2021-10-05T09:25:02.026967459Z /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
2021-10-05T09:25:02.033956297Z /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
2021-10-05T09:25:02.043751152Z 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
2021-10-05T09:25:02.064561317Z 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
.....

检查容器

检查容器将打印有关容器的信息。

$ podman inspect webserver
[
    {
        "Id": "19b6668bc6278a66b3ffc98ae1515af25f5bebcd20bf26de803cae41c4485f59",
        "Created": "2021-10-05T09:25:01.784949744Z",
        "Path": "/docker-entrypoint.sh",
        "Args": [
            "nginx",
            "-g",
            "daemon off;"
        ],
        "State": {
            "OciVersion": "1.0.2-dev",
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 4423,
....

默认命令将打印出 JSON 格式的长输出。要过滤输出,您需要使用 --format 选项。要查明容器何时启动,请运行以下命令。

$ podman inspect webserver --format '{{.State.StartedAt}}'
2021-10-05 10:13:52.794806322 +0000 UTC

访问容器外壳

您可以使用 exec 选项访问任何容器的 Shell 提示符。

$ podman exec -it webserver2 /bin/bash

豆荚

Podman 具有 Docker 所缺乏的独特功能。 Podman 可以从一起运行的容器中创建 Pod。这使您可以集中管理多个容器。

要创建 Pod,请使用以下命令。

$ podman pod create --name mypod

将容器添加到新创建的容器中。

$ podman run --pod mypod --name myimage1 image:latest
$ podman run --pod mypod --name myimage2 diff-image:latest

您现在可以使用简单的一行命令来管理容器。

$ podman kill mypod      # Kill all containers
$ podman restart mypod   # Restart all containers
$ podman stop mypod      # Stop all containers
$ podman pod ps			# List all pods
$ podman pod top mypod   # Display running processes in a pod
$ podman pod inspect mypod # Inspect a Pod
$ podman pod rm mypod    # Remove the pod

结论

我们关于安装和使用 Podman 运行容器的教程到此结束。您可以使用 Podman 做很多我们没有介绍的事情。如果您有任何问题,请在下面的评论中发表。

©2015-2025 艾丽卡 support@alaica.com