Podman 中通过 podman exec 命令可在运行中的容器内执行指令,该命令支持多种参数来适配交互模式、权限控制、环境配置等不同场景,以下是其详细用法介绍,包含基础语法、核心参数及实操示例:
podman exec [选项] 容器名/容器ID 待执行命令 [命令参数]
容器名/容器ID用于指定目标运行容器,可通过podman ps命令查看;待执行命令为在容器内要运行的指令,后续可附加该命令所需的参数。
选项说明:
-i, --interactive 保持标准输入打开,即便未附加终端,适用于需要输入的场景
-t, --tty 分配伪终端,常与 -i 搭配实现交互式终端登录
-d, --detach 后台运行命令,不阻塞当前终端,执行后返回会话 ID
-u, --user 指定执行命令的用户 / 用户组,格式支持用户名、UID、用户:组等
-w, --workdir 指定命令执行的容器内工作目录
-e, --env 设置环境变量,若只写变量名则自动读取主机对应变量值
--privileged 赋予命令扩展权限,关闭容器与主机的部分隔离安全特性
更多选项请参考 podman exec 命令手册。
以下是一些常用示例:
C:\Users\hxstri> podman exec -it my-nginx /bin/bash root@66c213f1729f:/# ls bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var boot docker-entrypoint.d etc lib media opt root sbin sys usr root@66c213f1729f:/#
选项说明:
-i:保持标准输入打开
-t:分配伪终端(终端交互)
注意,如果容器内没有 bash,可尝试 sh(如 Alpine 镜像,下面使用 nginx:alpine 作为案例):
# 拉取 nginx:alpine 镜像 C:\Users\hxstri> podman pull nginx:alpine Resolving "nginx" using unqualified-search registries (/home/user/.config/containers/registries.conf) Trying to pull docker.io/library/nginx:alpine... Getting image source signatures Copying blob sha256:8f6a6833e95d43ac524f1f9c5e7c1316c1f3b8e7ae5ba3db4e54b0c5b910e80a Copying blob sha256:d9a55dab5954588333096b28b351999099bea5eb3c68c10e99f175b12c97198d Copying blob sha256:df413d6ebdc834bccf63178455d406c4d25e2c2d38d2c1ab79ee5494b18e5624 Copying blob sha256:194fa24e147df0010e146240d3b4bd25d04180c523dc717e4645b269991483e3 Copying blob sha256:2d35ebdb57d9971fea0cac1582aa78935adf8058b2cc32db163c98822e5dfa1b Copying blob sha256:3eaba6cd10a374d9ed629c26d76a5258e20ddfa09fcef511c98aa620dcf3fae4 Copying blob sha256:ff8a36d5502a57c3fc8eeff48e578ab433a03b1dd528992ba0d966ddf853309a Copying blob sha256:bdabb0d442710d667f4fd871b5fd215cc2a430a95b192bc508bf945b8e60999b Copying config sha256:d4918ca78576a537caa7b0c043051c8efc1796de33fee8724ee0fff4a1cabed9 Writing manifest to image destination d4918ca78576a537caa7b0c043051c8efc1796de33fee8724ee0fff4a1cabed9 # 使用 nginx:alpine 镜像运行容器,容器名为 my-nginx-alpine C:\Users\hxstri> podman run -d --name my-nginx-alpine -p 8082:80 nginx:alpine 031be4cc24f51a77069682ac82bfb64701d557c9eac582d0a972eb069cd6e23a # 尝试在 my-nginx-alpine 容器内部执行 bash,失败了,bash 不存在 C:\Users\hxstri> podman exec -it my-nginx-alpine /bin/bash Error: crun: executable file `/bin/bash` not found: No such file or directory: OCI runtime attempted to invoke a command that was not found # 改用,在 my-nginx-alpine 容器中之心 sh C:\Users\hxstri> podman exec -it my-nginx-alpine /bin/sh / # ls bin etc mnt run tmp dev home opt sbin usr docker-entrypoint.d lib proc srv var docker-entrypoint.sh media root sys / #
注意:Alpine 是一款基于 musl libc 和 busybox 的轻量级 Linux 发行版,核心优势是体积极小、资源消耗低、安全精简,是容器化场景的首选基础镜像之一。
例如,查看容器内的 /etc/os-release 文件:
C:\Users\hxstri> podman exec my-nginx cat /etc/os-release PRETTY_NAME="Debian GNU/Linux 13 (trixie)" NAME="Debian GNU/Linux" VERSION_ID="13" VERSION="13 (trixie)" VERSION_CODENAME=trixie DEBIAN_VERSION_FULL=13.1 ID=debian HOME_URL="https://www.debian.org/" SUPPORT_URL="https://www.debian.org/support" BUG_REPORT_URL="https://bugs.debian.org/"
默认情况下,exec 会以容器的默认用户执行命令,如果需要 root 权限可加 -u root 选项:
# 以 root 身份更新包管理 C:\Users\hxstri> podman exec -u root my-nginx apt update WARNING: apt does not have a stable CLI interface. Use with caution in scripts. Get:1 http://deb.debian.org/debian trixie InRelease [140 kB] Get:2 http://deb.debian.org/debian trixie-updates InRelease [47.3 kB] Get:3 http://deb.debian.org/debian-security trixie-security InRelease [43.4 kB] Get:4 http://deb.debian.org/debian trixie/main amd64 Packages [9669 kB] ...
在容器内运行一个长时间任务并将输出写入文件:
podman exec my-nginx sh -c "while true; do echo 'hello'; sleep 1; done > /tmp/log.txt &"
之后可通过 cat 查看结果:
C:\Users\hxstri> podman exec my-nginx cat /tmp/log.txt hello hello hello hello hello ...
指通过 podman exec 命令在已运行的容器中执行特定命令时,临时注入环境变量,让这些变量在容器内的命令执行过程中生效。
简单来说,就是让容器内的命令 “感知” 到你传递的变量值,从而影响命令的行为(比如作为参数、配置项等)
环境变量是 “键值对” 形式的参数(如 KEY=VALUE),传递到容器内后,可被容器中执行的命令读取和使用,实现命令的动态配置,而无需修改容器本身的配置或重启容器。
例如:
# 输出 hello C:\Users\hxstri> podman exec -e "MSG=hello" my-nginx sh -c "echo $MSG" hello
假设容器内有 /app/script.sh脚本,如下图:

执行它:
C:\Users\hxstri> podman exec my-ubuntu sh /app/script.sh hello podman Tue Nov 11 23:53:53 HST 2025
要运行 podman exec 命令,有以下注意事项:
容器必须处于 运行中(podman ps 可见),否则无法执行 exec
若需退出交互式终端,输入 exit 即可
更多命令相关信息可通过 podman exec --help 查看完整参数说明。