二、Podman - 安装和基础命令

suaxi
2026-01-14 / 0 评论 / 3 阅读 / 正在检测是否收录...

1. 安装

以 Debian 12 为例

apt install podman

root@debian:~# podman version
Client:       Podman Engine
Version:      4.3.1
API Version:  4.3.1
Go Version:   go1.19.8
Built:        Thu Jan  1 08:00:00 1970
OS/Arch:      linux/amd64


2. 基础命令

运行示例容器
# 以基础的 httpd 服务为例
podman run --name nginx-test -d -p 8080:80/tcp docker.io/nginx

注:该命令下容器以分离模式运行(-d),Podman 会在运行结束后打印容器ID


列出正在运行的容器
podman ps

# 列出所有容器
podman ps -a


检查运行中的容器
podman inspect <container>

注:由于容器以 rootless 模式运行,因此不会分配 IP 地址,inspect 信息中该项的值为 none


测试 httpd 服务
root@debian:~# curl http://localhost:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>


查看容器日志
podman logs -f <container_id>
root@debian:~# podman logs nginx-test
/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: 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
2026/01/14 14:05:20 [notice] 1#1: using the "epoll" event method
2026/01/14 14:05:20 [notice] 1#1: nginx/1.29.4
2026/01/14 14:05:20 [notice] 1#1: built by gcc 14.2.0 (Debian 14.2.0-19) 
2026/01/14 14:05:20 [notice] 1#1: OS: Linux 6.1.0-41-amd64
2026/01/14 14:05:20 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2026/01/14 14:05:20 [notice] 1#1: start worker processes
2026/01/14 14:05:20 [notice] 1#1: start worker process 24
2026/01/14 14:05:20 [notice] 1#1: start worker process 25
2026/01/14 14:05:20 [notice] 1#1: start worker process 26
2026/01/14 14:05:20 [notice] 1#1: start worker process 27
10.88.0.1 - - [14/Jan/2026:14:05:38 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.88.1" "-"


查看容器信息和进程
root@debian:~# podman top nginx-test 
USER        PID         PPID        %CPU        ELAPSED          TTY         TIME        COMMAND
root        1           0           0.000       2m26.588290297s  ?           0s          nginx: master process nginx -g daemon off; 
nginx       24          1           0.000       2m26.588345391s  ?           0s          nginx: worker process 
nginx       25          1           0.000       2m26.588369296s  ?           0s          nginx: worker process 
nginx       26          1           0.000       2m26.588388167s  ?           0s          nginx: worker process 
nginx       27          1           0.000       2m26.588406535s  ?           0s          nginx: worker process 


检查容器

对容器进行检查点操作会停止容器运行,并将容器内所有进程的状态写入磁盘,在有需要的时候可以恢复,并从检查点创建时的状态继续运行(此功能需要系统上安装 CRIU 3.11 或更高版本,且不支持 rootless

sudo podman container checkpoint <container_id>


恢复容器

创建过检查点的容器才支持快照

sudo podman container restore <container_id>


迁移容器

创建检查点 ---> 将对应文件传输至目标主机 ---> 目标主机恢复容器

# 原主机
sudo podman container checkpoint <container_id> -e /tmp/checkpoint.tar.gz
scp /tmp/checkpoint.tar.gz <destination_host>:/tmp

# 目标主机
sudo podman container restore -i /tmp/checkpoint.tar.gz


停止容器
podman stop <container_id>


移除容器
podman rm <container_id>
0

评论 (0)

取消