上面介绍了 Podman 创建容器和查看容器状态,除此以外,你还会用到容器启动、停止和重启操作。这三个操作使用也非常频繁,有必要好好掌握。
用于启动已创建但未运行(状态为 Created)或已停止(状态为 Exited)的容器。
基本语法:
podman start [选项] 容器ID/容器名称
常用选项:
-a/--attach:启动容器的同时,附加到容器的标准输入 / 输出(类似 podman run -it 的交互模式)。
-i/--interactive:启动后保持标准输入打开(即使未附加)。
点击查看 podman start 命令手册。
(1)启动指定容器(通过 ID 或名称):
# 按名称启动 C:\Users\hxstr> podman start my-nginx my-nginx # 查看 redis 容器的ID C:\Users\hxstr> podman ps -a | findstr redis b6b36dffa6eb docker.io/library/redis:latest redis-server --ap... 25 hours ago Exited (0) 48 seconds ago 0.0.0.0:6379->6379/tcp redis # 按 ID 启动 C:\Users\hxstr> podman start b6b36dffa6eb b6b36dffa6eb
(2) 启动并进入容器交互模式:
# 等价于启动后立即 `podman attach` C:\Users\hxstr> podman start -a -i my-nginx /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: IPv6 listen already enabled /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh /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 2025/11/12 03:22:03 [notice] 1#1: using the "epoll" event method 2025/11/12 03:22:03 [notice] 1#1: nginx/1.29.3 2025/11/12 03:22:03 [notice] 1#1: built by gcc 14.2.0 (Debian 14.2.0-19) 2025/11/12 03:22:03 [notice] 1#1: OS: Linux 6.6.87.2-microsoft-standard-WSL2 2025/11/12 03:22:03 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576 2025/11/12 03:22:03 [notice] 1#1: start worker processes 2025/11/12 03:22:03 [notice] 1#1: start worker process 17 2025/11/12 03:22:03 [notice] 1#1: start worker process 18 2025/11/12 03:22:03 [notice] 1#1: start worker process 19 2025/11/12 03:22:03 [notice] 1#1: start worker process 20 2025/11/12 03:22:03 [notice] 1#1: start worker process 21 2025/11/12 03:22:03 [notice] 1#1: start worker process 22 2025/11/12 03:22:03 [notice] 1#1: start worker process 23 2025/11/12 03:22:03 [notice] 1#1: start worker process 24
注意,上面命令会启动名为 my-nginx 的容器,并实时显示其输出,同时保持交互能力(如果容器内程序支持的话,比如可以通过终端向容器发送信号等)。对于 Nginx 这类后台服务,-i 可能不会有明显的交互效果,但 -a 会让你看到 Nginx 启动的日志输出。
用于优雅停止运行中(状态为 Running)的容器,会向容器发送 SIGTERM 信号,允许容器保存状态后退出(默认等待 10 秒,超时后发送 SIGKILL 强制终止)。
基本语法:
podman stop [选项] 容器ID/容器名称
常用选项:
-t/--time:指定等待容器停止的秒数(默认 10 秒),超时后强制终止。
-a/--all:停止所有运行中的容器
点击查看 podman stop 命令手册。
(1)优雅停止指定容器:
# 默认最长等待 10 秒后停止 C:\Users\hxstr> podman stop my-nginx my-nginx
(2)自定义等待时间(如 30 秒):
C:\Users\hxstr> podman stop -t 30 my-nginx my-nginx
用于重启运行中或已停止的容器,相当于先执行 stop 再执行 start(若容器已停止,则直接启动)。
基本语法:
podman restart [选项] 容器ID/容器名称
常用选项:
-t/--time:停止容器时的等待秒数(同 podman stop 的 -t)。
--latest/-l:重启最近创建的容器。
点击查看 podman restart 命令手册。
(1) 重启指定容器(默认等待 10 秒停止后启动):
C:\Users\hxstr> podman ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 66c213f1729f docker.io/library/nginx:latest nginx -g daemon o... 2 days ago Up 7 seconds 0.0.0.0:9000->80/tcp my-nginx C:\Users\hxstr> podman restart my-nginx my-nginx
(2)重启时自定义停止等待时间(如 5 秒):
C:\Users\hxstr> podman ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 66c213f1729f docker.io/library/nginx:latest nginx -g daemon o... 2 days ago Up 33 seconds 0.0.0.0:9000->80/tcp my-nginx C:\Users\hxstr> podman restart -t 5 my-nginx my-nginx
注意:下面操作仅能在 Linux 系统下有效,因为$() 是 Linux/Unix shell(如 Bash、Zsh)中的命令替换语法,核心作用是执行括号内的命令,并用其输出结果替代整个 $() 表达式。例如:
[user@localhost ~]$ echo $(podman ps -q) 66c213f1729f 9f26781c8d4b e62eccbe38af b6b36dffa6eb
通过 podman ps -q 结合管道,可批量处理容器,例如:
(1)批量停止所有运行中容器:
[user@localhost ~]$ podman stop $(podman ps -q) 9f26781c8d4b e62eccbe38af b6b36dffa6eb 66c213f1729f
(2) 批量重启所有运行中容器:
[user@localhost ~]$ podman restart $(podman ps -q) WARN[0014] StopSignal SIGTERM failed to stop container eureka-server in 10 seconds, resorting to SIGKILL WARN[0025] StopSignal SIGTERM failed to stop container user-service1 in 10 seconds, resorting to SIGKILL WARN[0036] StopSignal SIGTERM failed to stop container user-service2 in 10 seconds, resorting to SIGKILL 66c213f1729f 1562fbb28727 74e0d9680be2 6d7f4b8e618a 84ea46e2c422 ...
(3)批量启动所有已停止容器:
[user@localhost ~]$ podman start $(podman ps -a -q --filter "status=exited") 42bb37ebdf0e 66c213f1729f 1562fbb28727 74e0d9680be2 6d7f4b8e618a ...
下面表格列举了容器在某个状态支持那些操作,以及操作后的状态变化:
容器状态 | 可执行操作 | 操作后状态 |
Running | stop/restart | Exited / Running |
Exited | start/restart | Running / Running |
Created | start | Running |
Paused | 需先 podman unpause 再操作 | 恢复为 Running 后执行 |
注意,如果需要强制终止无响应的容器,还可以使用 podman kill 命令(直接发送 SIGKILL 信号),语法与 stop 类似:
# 强制终止容器 C:\Users\hxstr> podman kill my-nginx my-nginx
点击查看 podman kill 命令手册。