总结摘要
从安装到镜像加速的完整 Podman 配置指南
Podman 完整安装与配置指南
一、安装 Podman
各平台安装方法:
Linux 系统:
1
2
3
4
5
6
7
8
| # Ubuntu/Debian
sudo apt update && sudo apt install -y podman
# RHEL/CentOS
sudo yum install -y podman
# Fedora
sudo dnf install -y podman
|
macOS 系统:
1
2
3
4
5
6
| # 使用 Homebrew 安装
brew install podman
# 初始化虚拟机
podman machine init
podman machine start
|
Windows 系统:
1
2
3
4
5
6
| # 使用 Winget 安装
winget install RedHat.Podman
# 初始化 WSL 环境
podman machine init
podman machine start
|
官方安装文档:
https://podman.io/docs/installation
二、配置镜像加速
完整配置步骤:
进入 Podman 虚拟机环境:
编辑镜像源配置文件:
1
| sudo vim /etc/containers/registries.conf
|
使用以下优化配置(支持多镜像源自动切换):
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
| # 默认搜索源
unqualified-search-registries = ["docker.io"]
# 主仓库配置
[[registry]]
prefix = "docker.io"
location = "docker.mirrors.ustc.edu.cn" # 推荐首选源
insecure = true # 允许非 HTTPS 连接
# 镜像源列表(按顺序尝试)
[[registry.mirror]]
location = "docker.mirrors.ustc.edu.cn" # 中科大镜像源
insecure = true
[[registry.mirror]]
location = "hub-mirror.c.163.com" # 网易镜像源
insecure = true
[[registry.mirror]]
location = "mirror.baidubce.com" # 百度云镜像源
insecure = true
[[registry.mirror]]
location = "docker.nju.edu.cn" # 南京大学源
insecure = true
[[registry.mirror]]
location = "registry-1.docker.io" # Docker 官方源(备用)
insecure = false
|
保存配置并退出编辑器
验证配置生效:
1
2
3
4
5
| # 检查配置是否加载
podman info --format '{{ .Registries }}'
# 测试镜像拉取速度
time podman pull nginx:alpine
|
三、高级配置选项
1. 配置存储驱动(解决磁盘空间问题)
1
2
3
4
5
6
7
8
| # 编辑存储配置文件
sudo vim /etc/containers/storage.conf
# 修改以下参数
[storage]
driver = "overlay"
graphroot = "/var/lib/containers/storage"
runroot = "/run/containers/storage"
|
2. 设置 Rootless 模式(推荐)
1
2
3
4
5
6
| # 启用用户命名空间
sudo usermod --add-subuids 100000-165535 $USER
sudo usermod --add-subgids 100000-165535 $USER
# 验证无根模式
podman run --rm hello-world
|
3. 配置网络(自定义网桥)
1
2
3
4
5
| # 创建自定义网络
podman network create mynet --subnet 10.10.0.0/24
# 使用自定义网络运行容器
podman run -d --network mynet --name web nginx
|
四、常用操作示例
1. Docker 命令对比
| Docker 命令 | Podman 等效命令 |
|---|
docker run | podman run |
docker ps | podman ps |
docker build | podman build |
docker-compose up | podman-compose up |
2. 实际使用示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| # 运行交互式容器
podman run -it --rm alpine sh
# 构建自定义镜像
echo "FROM alpine" > Dockerfile
echo 'CMD echo "Hello Podman!"' >> Dockerfile
podman build -t hello-podman .
# 运行自定义镜像
podman run --rm hello-podman
# 查看容器资源使用
podman stats
# 生成 systemd 服务文件
podman generate systemd --name web > /etc/systemd/system/web.service
|
五、故障排除
常见问题解决方案:
镜像拉取失败:
1
2
3
4
5
| # 检查当前使用的镜像源
podman info | grep -A 10 registries
# 临时使用特定镜像源
podman pull --registry=docker.mirrors.ustc.edu.cn/library/nginx
|
权限问题:
1
2
3
4
5
| # 修复用户命名空间配置
podman system migrate
# 检查用户映射
grep $USER /etc/subuid
|
网络连接问题:
1
2
3
4
5
6
7
8
| # 检查网络配置
podman network ls
podman inspect <container_id> | grep IPAddress
# 重置网络
podman machine stop
podman machine rm -f
podman machine init
|
存储空间不足:
1
2
3
4
5
| # 清理无用资源
podman system prune -a -f
# 查看存储使用
podman system df
|
性能优化提示:定期更新镜像源列表,国内用户推荐使用中科大(docker.mirrors.ustc.edu.cn)或南京大学(docker.nju.edu.cn)镜像源
日志查看:
1
2
3
4
5
| # 查看容器日志
podman logs <container_id>
# 查看服务日志(macOS/Windows)
podman machine logs
|
参考资源:
Podman 官方文档