Podman 容器管理完全指南 🐳
📋 目录
📖 简介 Podman 是一个开源的容器运行时工具,用于管理容器、镜像和容器编排。它与 Docker 兼容但无需守护进程,提供了更安全的容器管理方式。Podman 支持 rootless 容器运行,是 Docker 的优秀替代品。
🎯 Podman 主要特性
无守护进程架构 : 不需要长期运行的守护进程
Rootless 容器 : 允许普通用户运行容器,提高安全性
Docker 兼容 : 支持 Docker CLI 命令和镜像格式
Pod 支持 : 原生支持 Kubernetes 风格的 Pod
系统集成 : 与 systemd 集成良好
🔄 Podman vs Docker
特性
Podman
Docker
架构
无守护进程
客户端-服务器架构
权限
支持 rootless
通常需要 root 权限
安全性
更高,无需守护进程
守护进程可能存在安全风险
兼容性
兼容 Docker 命令
原生 Docker 生态
👀 容器查看命令 1. 查看运行中的容器 1 2 3 4 5 6 7 8 9 10 11 podman ps podman ps --format '{{.ID}}: {{.Names}} - {{.Status}}' podman ps --format "table {{.ID}}\t{{.Names}}\t{{.Status}}\t{{.Ports}}" podman ps --format "table {{.Names}}\t{{.Image}}\t{{.Status}}"
2. 查看所有容器状态 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 podman ps -a podman ps -a --filter "status=exited" podman ps -a --filter "status=exited" --format "{{.ID}} {{.Names}}" podman ps -a --filter "status=created" podman ps -a --filter "status=running" podman ps -a --filter "status=paused" podman ps -a --filter "status=exited" podman ps -a --filter "status=removing"
3. 高级查看选项 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 podman ps -l podman ps -n 5 podman ps -s podman ps --sort =name podman ps --sort =status podman ps --sort =created podman ps --filter "name=web" podman ps --filter "label=env=prod" podman ps --filter "ancestor=nginx"
⚡ 容器生命周期管理 4. 启动和停止容器 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 podman start mytest podman stop mytest podman stop -t 0 mytest podman kill mytest podman restart mytest podman restart -t 30 mytest
5. 创建和运行容器 1 2 3 4 5 6 7 8 9 10 11 12 13 14 podman create --name mycontainer nginx:alpine podman run -d --name mytest nginx:alpine podman run -it --name mytest ubuntu:20.04 bash podman run --rm -it alpine:latest sh podman run -d --restart=always --name myapp myapp:latest
6. 删除容器 1 2 3 4 5 6 7 8 9 10 11 12 13 14 podman rm mytest podman rm -f mytest podman rm -v mytest podman container prune podman rm -f mytest || podman stop mytest && podman rm mytest
📊 容器日志和监控 8. 查看容器日志 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 podman logs mytest podman logs -f mytest podman logs --tail 100 mytest podman logs -t mytest podman logs --since 2023-01-01T00:00:00 mytest podman logs --since 1h mytest podman logs --since 30m mytest podman logs -f --tail 50 --since 10m mytest
9. 容器资源监控 1 2 3 4 5 6 7 8 9 10 11 12 13 14 podman stats mytest podman stats --no-stream mytest podman stats --all podman stats --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}" watch -n 5 podman stats --no-stream
10. 高级监控命令 1 2 3 4 5 6 7 8 9 10 11 podman top mytest podman top mytest -eo pid,ppid,user,comm podman inspect mytest --format '{{.HostConfig.Memory}}' podman inspect mytest --format '{{.HostConfig.NanoCpus}}'
💻 容器交互操作 11. 进入容器执行命令 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 podman exec -it mytest bash podman exec -it mytest sh podman exec mytest ls -la /app podman exec mytest /bin/bash -c "ls -la && pwd" podman exec -it --user www-data mytest bash podman exec -it -e DEBUG=true mytest bash
12. 文件操作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 podman cp local_file.txt mytest:/path/in/container/ podman cp mytest:/path/in/container/file.txt ./ podman cp ./local_dir/ mytest:/path/in/container/ podman cp mytest:/path/in/container/ ./local_dir/ podman cp -a local_file.txt mytest:/path/in/container/
🔄 批量操作 13. 批量停止容器 1 2 3 4 5 6 7 8 podman stop $(podman ps -q) podman stop $(podman ps -q --filter "name=web" ) podman ps -q | xargs podman stop -t 30
14. 批量删除容器 1 2 3 4 5 6 7 8 9 10 11 podman container prune podman rm -f $(podman ps -aq) podman rm -f $(podman ps -aq --filter "name=test" ) podman rm -f $(podman ps -aq --filter "status=exited" )
15. 批量重启容器 1 2 3 4 5 podman restart $(podman ps -q) podman restart $(podman ps -q --filter "ancestor=nginx" )
16. 批量操作脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 BACKUP_DIR="./backup/$(date +%Y%m%d_%H%M%S) " mkdir -p "$BACKUP_DIR " for container in $(podman ps -aq); do name=$(podman inspect --format='{{.Name}}' $container | sed 's|/||' ) echo "Backing up $name ..." mkdir -p "$BACKUP_DIR /$name " podman inspect $container > "$BACKUP_DIR /$name /inspect.json" podman cp $container :/etc "$BACKUP_DIR /$name /" 2>/dev/null || true podman cp $container :/app "$BACKUP_DIR /$name /" 2>/dev/null || true done
🔍 容器信息查询 17. 查看容器详细信息 1 2 3 4 5 6 7 8 9 10 11 12 13 14 podman inspect mytest podman inspect --format='{{.NetworkSettings.IPAddress}}' mytest podman inspect --format='{{json .Config}}' mytest | jq . podman inspect --format='{{.State}}' mytest podman inspect --format='{{.Mounts}}' mytest
18. 网络信息查询 1 2 3 4 5 6 7 8 9 10 11 podman port mytest podman port mytest 80 podman inspect --format='{{.NetworkSettings}}' mytest podman inspect --format='{{.NetworkSettings.IPAddress}}' mytest
19. 资源信息查询 1 2 3 4 5 6 7 8 9 podman inspect --format='{{.HostConfig.Memory}}' mytest podman inspect --format='{{.HostConfig.CpuShares}}' mytest podman inspect --format='{{.GraphDriver}}' mytest podman inspect --format='{{.Config.Env}}' mytest
🚀 高级管理命令 20. 容器重命名 1 2 3 4 5 6 7 8 9 podman rename mytest new-container-name for container in $(podman ps -aq); do old_name=$(podman inspect --format='{{.Name}}' $container | sed 's|/||' ) new_name="prod_${old_name} " podman rename $old_name $new_name done
21. 容器提交和导出 1 2 3 4 5 6 7 8 9 10 11 12 13 14 podman commit mytest new_image_name:tag podman commit --author "Your Name <email@example.com>" mytest new_image podman commit --change 'CMD ["nginx", "-g", "daemon off;"]' mytest nginx_custom podman export mytest > mytest.tar podman import mytest.tar my_imported_image:tag
22. 暂停和恢复容器 1 2 3 4 5 6 7 8 podman pause mytest podman unpause mytest podman inspect --format='{{.State.Paused}}' mytest
23. 容器更新操作 1 2 3 4 5 6 7 8 9 podman update --memory 512m mytest podman update --cpus 1.5 mytest podman update --restart unless-stopped mytest podman update --memory 1g --cpus 2 --restart always mytest
📝 实用脚本示例 24. 容器健康检查脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #!/bin/bash CONTAINERS=$(podman ps --format "{{.Names}}" ) for container in $CONTAINERS ; do status=$(podman inspect --format='{{.State.Status}}' $container ) health=$(podman inspect --format='{{.State.Health.Status}}' $container 2>/dev/null || echo "N/A" ) echo "Container: $container " echo "Status: $status " echo "Health: $health " if [ "$status " != "running" ]; then echo "❌ Container $container is not running. Attempting to restart..." podman restart $container fi if [ "$health " = "unhealthy" ]; then echo "⚠️ Container $container is unhealthy. Check logs: podman logs $container " fi echo "----------------------------------------" done
25. 自动备份脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #!/bin/bash BACKUP_DIR="/backup/containers/$(date +%Y%m%d_%H%M%S) " mkdir -p "$BACKUP_DIR " echo "Starting container backup at $(date) " for container in $(podman ps -q); do name=$(podman inspect --format='{{.Name}}' $container | sed 's|/||' ) echo "Backing up $name ..." container_dir="$BACKUP_DIR /$name " mkdir -p "$container_dir " podman inspect $container > "$container_dir /inspect.json" volumes=$(podman inspect --format='{{range .Mounts}}{{.Source}}:{{.Destination}} {{end}}' $container ) for volume in $volumes ; do src=$(echo $volume | cut -d: -f1) if [ -d "$src " ]; then rsync -a "$src /" "$container_dir /volumes/$(basename $src) /" fi done podman logs $container > "$container_dir /container.log" 2>/dev/null done echo "Backup completed: $BACKUP_DIR "
26. 容器资源监控仪表板 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #!/bin/bash watch -n 2 ' echo "==================== CONTAINER MONITOR ====================" echo "CPU and Memory Usage:" podman stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.MemPerc}}" echo -e "\nRunning Containers:" podman ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" echo -e "\nResource Limits:" for container in $(podman ps -q); do name=$(podman inspect --format="{{.Name}}" $container | sed "s|/||") memory=$(podman inspect --format="{{.HostConfig.Memory}}" $container) cpu=$(podman inspect --format="{{.HostConfig.NanoCpus}}" $container) echo "$name: Memory=${memory} bytes, CPU=${cpu} nanoseconds" done '
🛠️ 故障排除 27. 容器状态诊断 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 podman inspect --format='{{json .State}}' mytest | jq . podman inspect --format='{{.State.ExitCode}}' mytest podman inspect --format='{{.State.Error}}' mytest podman inspect --format='{{.State.StartedAt}}' mytest podman inspect --format='{{.State.FinishedAt}}' mytest podman inspect --format='{{.State.OOMKilled}}' mytest
28. 日志分析技巧 1 2 3 4 5 6 7 8 9 10 11 podman logs mytest | grep -i error podman logs --since "10m" mytest | grep -i exception podman logs -f mytest | grep --line-buffered -i error podman logs mytest | awk '{print $1}' | sort | uniq -c | sort -nr
29. 网络故障排查 1 2 3 4 5 6 7 8 9 10 11 podman exec mytest ping -c 4 8.8.8.8 podman exec mytest cat /etc/resolv.conf podman exec mytest netstat -tulpn podman exec container1 ping container2
🔒 安全最佳实践 30. 非特权容器运行 1 2 3 4 5 6 7 8 podman run --user 1000:1000 nginx:alpine podman run --user $(id -u):$(id -g) nginx:alpine podman run --security-opt=no-new-privileges nginx:alpine
31. 资源限制和隔离 1 2 3 4 5 6 7 8 9 10 11 12 13 14 podman run --memory=512m --memory-swap=1g nginx:alpine podman run --cpus=1.5 --cpu-shares=512 nginx:alpine podman run --device-read-bps=/dev/sda:1mb --device-write-iops=/dev/sda:10 nginx:alpine podman run --read-only nginx:alpine podman run -v /data:/data:ro nginx:alpine
32. 安全增强配置 1 2 3 4 5 6 7 8 9 10 11 podman run --security-opt apparmor=my-profile nginx:alpine podman run --security-opt label=type :my_container_t nginx:alpine podman run --security-opt seccomp=unconfined nginx:alpine podman run --cap-drop=ALL --cap-add=NET_BIND_SERVICE nginx:alpine
🌐 网络管理 33. 网络查看和诊断 1 2 3 4 5 6 7 8 9 10 11 12 13 14 podman network ls podman network inspect bridge podman network create mynetwork podman network connect mynetwork mycontainer podman network disconnect mynetwork mycontainer
34. 高级网络配置 1 2 3 4 5 6 7 8 podman network create --subnet 192.168.100.0/24 --gateway 192.168.100.1 mynet podman network create -d macvlan --subnet=192.168.1.0/24 --gateway=192.168.1.1 -o parent=eth0 mymacvlan podman run --network mynet --ip 192.168.100.10 nginx:alpine
📦 镜像管理 35. 镜像操作命令 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 podman images podman pull nginx:alpine podman push myimage:latest registry.example.com/myimage:latest podman rmi nginx:alpine podman build -t myapp:latest . podman tag nginx:alpine myregistry/nginx:latest
36. 镜像清理和维护 1 2 3 4 5 6 7 8 9 10 11 podman image prune podman image prune -a podman system df podman system prune -a
🎯 总结 通过这份完整的 Podman 容器管理指南,您应该能够:
熟练掌握 Podman 的基本容器操作命令
有效监控 容器状态和资源使用情况
高效管理 多个容器的批量操作
深入排查 容器故障和性能问题
安全运行 容器并实施最佳实践
灵活配置 容器网络和存储
Podman 作为 Docker 的现代替代品,提供了更安全、更灵活的容器管理体验。建议定期练习这些命令,并将其集成到您的日常运维工作中。
💡 提示 : 记得定期更新 Podman 版本以获取最新功能和安全修复:1 2 sudo dnf update podman sudo apt update podman