5.3. 在容器内运行命令

使用 podman run 命令以交互方式运行容器。

先决条件

  • container-tools 元数据包已安装。

流程

  1. 根据 registry.redhat.io/ubi9/ubi 镜像,运行名为 myubi 的容器:

    $ podman run --name=myubi -it registry.access.redhat.com/ubi9/ubi /bin/bash
    [root@6ccffd0f6421 /]#
    • -i 选项创建一个交互式会话。如果不使用 -t 选项,shell 将保持打开状态,但您无法对 shell 输入任何东西。
    • -t 选项打开一个终端会话。如果不使用 -i 选项,shell 会打开,然后退出。
  2. 安装 procps-ng 软件包,其包含一组系统工具(如 pstopuptime ,等等):

    [root@6ccffd0f6421 /]# dnf install procps-ng
  3. 使用 ps -ef 命令列出当前的进程:

    # ps -ef
    UID          PID    PPID  C STIME TTY          TIME CMD
    root           1       0  0 12:55 pts/0    00:00:00 /bin/bash
    root          31       1  0 13:07 pts/0    00:00:00 ps -ef
  4. 输入 exit 退出容器并返回到主机:

    # exit
  5. 可选:列出所有容器:

    $ podman ps
    CONTAINER ID  IMAGE                               COMMAND    CREATED         STATUS                     PORTS   NAMES
    1984555a2c27  registry.redhat.io/ubi9/ubi:latest  /bin/bash  21 minutes ago  Exited (0) 21 minutes ago          myubi

    您可以看到容器处于 Exited 状态。

其他资源

  • podman-run 手册页