5.10. 在运行中的容器中执行命令

使用 podman exec 命令,来在正在运行的容器中执行命令并调查该容器。使用 podman exec 命令而不是 podman run 命令的原因是,您可以在不中断容器活动的情况下调查正在运行的容器。

先决条件

  • container-tools 元数据包已安装。
  • 容器正在运行。

流程

  1. myrsyslog 容器中执行 rpm -qa 命令来列出所有已安装的软件包:

    $ podman exec -it myrsyslog rpm -qa
    tzdata-2020d-1.el8.noarch
    python3-pip-wheel-9.0.3-18.el8.noarch
    redhat-release-8.3-1.0.el8.x86_64
    filesystem-3.8-3.el8.x86_64
    ...
  2. myrsyslog 容器中执行 /bin/bash 命令:

    $ podman exec -it myrsyslog /bin/bash
  3. 安装 procps-ng 软件包,其包含一组系统工具(如 pstopuptime ,等等):

    # dnf install procps-ng
  4. 检查容器:

    • 要列出系统上的每个进程:

      # ps -ef
      UID          PID    PPID  C STIME TTY          TIME CMD
      root           1       0  0 10:23 ?        00:00:01 /usr/sbin/rsyslogd -n
      root           8       0  0 11:07 pts/0    00:00:00 /bin/bash
      root          47       8  0 11:13 pts/0    00:00:00 ps -ef
    • 要显示文件系统磁盘空间使用情况:

      # df -h
      Filesystem      Size  Used Avail Use% Mounted on
      fuse-overlayfs   27G  7.1G   20G  27% /
      tmpfs            64M     0   64M   0% /dev
      tmpfs           269M  936K  268M   1% /etc/hosts
      shm              63M     0   63M   0% /dev/shm
      ...
    • 要显示系统信息:

      # uname -r
      4.18.0-240.10.1.el8_3.x86_64
    • 要以 MB 为单位显示空闲和使用的内存量:

      # free --mega
      total        used        free      shared  buff/cache   available
      Mem:       2818         615        1183          12         1020        1957
      Swap:      3124           0        3124

其他资源

  • podman-exec man page