Skip to content

Podman 基础

这是 Podman 容器引擎的基础知识文档,详细介绍其功能、特性和使用方法。

什么是 Podman?

Podman(Pod Manager)是一个开源的容器引擎,用于管理、运行和构建 OCI(Open Container Initiative)容器和 pod。它由 Red Hat 主导开发,作为 Docker 的替代品,提供了相似的命令行界面但具有不同的架构设计。

主要特点

  • 无守护进程架构:不依赖于集中式守护进程,提高了安全性和稳定性
  • Rootless 支持:允许普通用户在不使用 root 权限的情况下运行容器
  • Pod 支持:原生支持 Kubernetes Pod 概念,便于与 Kubernetes 集成
  • OCI 兼容:完全兼容 OCI 容器标准
  • Docker 命令兼容:提供与 Docker 相似的命令行界面,便于迁移
  • 镜像管理:支持镜像的拉取、推送、构建和管理

Podman 与 Docker 的区别

特性PodmanDocker
架构无守护进程基于守护进程
Rootless原生支持需要额外配置
Pod 支持原生支持需使用 docker-compose 或 Kubernetes
安全性更高(无集中式守护进程)较低(守护进程以 root 运行)
命令兼容性高度兼容 docker 命令标准 docker 命令

Podman 核心概念

1. 容器(Container)

  • 轻量级、独立的可执行软件包,包含运行所需的所有内容
  • 基于容器镜像创建

2. 镜像(Image)

  • 容器的只读模板,包含应用程序及其依赖
  • 可以从镜像仓库拉取或本地构建

3. Pod

  • 包含一个或多个容器的逻辑组
  • 共享网络命名空间和存储卷
  • 与 Kubernetes Pod 概念一致

4. 镜像仓库(Registry)

  • 存储和分发容器镜像的服务
  • 如 Docker Hub、Quay.io、私有仓库等

5. Rootless 容器

  • 由普通用户运行的容器,不使用 root 权限
  • 提高了系统安全性

常用 Podman 命令

1. 容器管理

bash
# 运行容器
podman run -d --name my-container nginx

# 查看运行中的容器
podman ps

# 查看所有容器
podman ps -a

# 停止容器
podman stop my-container

# 启动容器
podman start my-container

# 删除容器
podman rm my-container

# 强制删除运行中的容器
podman rm -f my-container

# 进入容器
podman exec -it my-container /bin/bash

# 查看容器日志
podman logs my-container

# 查看容器资源使用情况
podman stats my-container

2. 镜像管理

bash
# 拉取镜像
podman pull nginx

# 查看本地镜像
podman images

# 删除镜像
podman rmi nginx

# 构建镜像
podman build -t my-image .

# 推送镜像到仓库
podman push my-image registry.example.com/my-image:latest

# 搜索镜像
podman search nginx

3. Pod 管理

bash
# 创建 Pod
podman pod create --name my-pod

# 查看 Pod
podman pod ps

# 在 Pod 中运行容器
podman run -d --pod my-pod --name my-container nginx

# 停止 Pod
podman pod stop my-pod

# 启动 Pod
podman pod start my-pod

# 删除 Pod
podman pod rm my-pod

# 查看 Pod 中的容器
podman ps --pod my-pod

4. Rootless 容器

bash
# 检查 rootless 模式是否可用
podman info | grep -i rootless

# 以 rootless 模式运行容器(普通用户)
podman run -d --name my-rootless-container nginx

# 查看当前用户的 UID 映射
podman unshare cat /etc/subuid
podman unshare cat /etc/subgid

5. 网络管理

bash
# 查看网络
podman network ls

# 创建网络
podman network create my-network

# 运行容器时指定网络
podman run -d --name my-container --network my-network nginx

# 查看网络详情
podman network inspect my-network

# 删除网络
podman network rm my-network

6. 存储管理

bash
# 查看存储卷
podman volume ls

# 创建存储卷
podman volume create my-volume

# 运行容器时挂载存储卷
podman run -d --name my-container -v my-volume:/data nginx

# 查看存储卷详情
podman volume inspect my-volume

# 删除存储卷
podman volume rm my-volume

7. Docker 兼容性

bash
# 使用 podman-docker 兼容层(部分系统)
# 安装后可直接使用 docker 命令
# docker run -d --name my-container nginx

Podman 与 Kubernetes 集成

1. 生成 Kubernetes YAML

bash
# 从运行中的容器生成 Kubernetes YAML
podman generate kube my-container > pod.yaml

# 从 Pod 生成 Kubernetes YAML
podman generate kube my-pod > pod.yaml

2. 应用 Kubernetes YAML

bash
# 使用 Podman 应用 Kubernetes YAML
podman play kube pod.yaml

# 使用 Kubernetes 应用 YAML
kubectl apply -f pod.yaml

3. 镜像转换

bash
# 将容器镜像推送到 Kubernetes 可访问的仓库
podman push my-image registry.example.com/my-image:latest

Podman 构建镜像

1. 使用 Dockerfile

bash
# 使用 Dockerfile 构建镜像
podman build -t my-image .

# 指定 Dockerfile 路径
podman build -t my-image -f ./Dockerfile.custom .

# 使用构建缓存
podman build --no-cache -t my-image .

2. 使用 Buildah(推荐)

bash
# Buildah 是 Podman 生态系统中的镜像构建工具
# 安装 Buildah 后使用以下命令

buildah from scratch
buildah run $container -- dnf install -y nginx
buildah config --cmd "nginx -g 'daemon off;'" $container
buildah commit $container my-nginx-image

最佳实践

  • 优先使用 Rootless 容器:提高系统安全性
  • 使用 Pod 管理相关容器:便于与 Kubernetes 集成
  • 定期更新镜像:确保使用最新的安全补丁
  • 合理使用存储卷:持久化重要数据
  • 限制容器资源:使用 --memory--cpu 等参数限制容器资源使用
  • 使用私有镜像仓库:对于企业环境,使用私有镜像仓库管理镜像

通过以上内容,你应该对 Podman 的基本概念、特性和常用命令有了全面的了解。作为 Docker 的替代品,Podman 提供了更安全、更灵活的容器管理体验,特别适合与 Kubernetes 集成的环境。