使用 Podman 運行容器,無需 root 權限和守護程序,與 Docker 完全相容
專案簡介
Podman 是一個無守護程序(daemonless)的容器引擎,可以作為 Docker 的替代品。支援 rootless 容器、Pod 管理,與 Docker CLI 完全相容。
GitHub Stars: 30K+
主要功能
- 無守護程序 - 直接執行,無需 daemon
- Rootless - 無需 root 權限
- OCI 相容 - 標準容器格式
- Pod 支援 - 原生 Pod 管理
- Docker 相容 - 相同的 CLI 指令
安裝
Ubuntu/Debian
1
2
| sudo apt update
sudo apt install podman
|
Fedora/RHEL
1
| sudo dnf install podman
|
macOS
1
2
3
| brew install podman
podman machine init
podman machine start
|
基本使用
Docker 相容指令
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| # 執行容器
podman run -d -p 8080:80 nginx
# 列出容器
podman ps
# 停止容器
podman stop <container-id>
# 刪除容器
podman rm <container-id>
# 查看日誌
podman logs <container-id>
|
別名設定
1
2
| # 可以設定 alias
alias docker=podman
|
映像管理
拉取和推送
1
2
3
4
5
6
7
8
| # 從 Docker Hub 拉取
podman pull docker.io/nginx
# 從其他 Registry
podman pull quay.io/podman/hello
# 推送映像
podman push myimage:tag registry.example.com/myimage:tag
|
建立映像
1
2
3
4
5
| # 使用 Dockerfile
podman build -t myapp:latest .
# 使用 Containerfile(推薦)
podman build -f Containerfile -t myapp:latest .
|
映像管理
1
2
3
4
5
6
7
8
| # 列出映像
podman images
# 刪除映像
podman rmi <image-id>
# 清理未使用
podman image prune -a
|
Rootless 容器
設定
1
2
3
4
5
6
| # 檢查 subuid/subgid
cat /etc/subuid
cat /etc/subgid
# 如果沒有,新增
sudo usermod --add-subuids 100000-165535 --add-subgids 100000-165535 $USER
|
執行
1
2
3
4
5
| # 以普通使用者執行
podman run -d nginx
# 使用者命名空間
podman unshare cat /proc/self/uid_map
|
端口限制
1
2
3
4
5
6
| # 非 root 無法綁定 < 1024 端口
# 使用高端口
podman run -d -p 8080:80 nginx
# 或設定權限
sudo sysctl net.ipv4.ip_unprivileged_port_start=80
|
Pod 管理
建立 Pod
1
2
3
4
5
6
| # 建立 Pod
podman pod create --name mypod -p 8080:80
# 在 Pod 中運行容器
podman run -d --pod mypod nginx
podman run -d --pod mypod redis
|
管理 Pod
1
2
3
4
5
6
7
8
9
10
11
| # 列出 Pod
podman pod ls
# Pod 狀態
podman pod stats mypod
# 停止 Pod
podman pod stop mypod
# 刪除 Pod
podman pod rm mypod
|
Pod YAML
1
2
3
4
5
| # 從 Pod 產生 YAML
podman generate kube mypod > mypod.yaml
# 從 YAML 建立 Pod
podman play kube mypod.yaml
|
Podman Compose
安裝
1
| pip install podman-compose
|
使用
1
2
3
4
5
6
7
8
| # 啟動
podman-compose up -d
# 停止
podman-compose down
# 日誌
podman-compose logs
|
docker-compose.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
| version: '3.8'
services:
web:
image: nginx
ports:
- "8080:80"
volumes:
- ./html:/usr/share/nginx/html
db:
image: postgres
environment:
POSTGRES_PASSWORD: secret
|
Quadlet(Systemd 整合)
建立服務
1
2
3
4
5
6
7
8
9
10
11
| # ~/.config/containers/systemd/webapp.container
[Container]
Image=docker.io/nginx
PublishPort=8080:80
Volume=./html:/usr/share/nginx/html
[Service]
Restart=always
[Install]
WantedBy=default.target
|
管理
1
2
3
4
5
6
7
8
| # 重新載入
systemctl --user daemon-reload
# 啟動
systemctl --user start webapp
# 開機啟動
systemctl --user enable webapp
|
網路
建立網路
1
2
3
4
5
6
| # 建立
podman network create mynet
# 使用網路
podman run -d --network mynet --name web nginx
podman run -d --network mynet --name db postgres
|
網路類型
1
2
3
4
5
6
7
8
| # Bridge(預設)
podman network create --driver bridge mynet
# 主機網路
podman run --network host nginx
# 無網路
podman run --network none nginx
|
Volume 管理
建立 Volume
1
2
3
4
5
6
7
8
9
10
11
| # 建立
podman volume create mydata
# 使用
podman run -v mydata:/data nginx
# 列出
podman volume ls
# 刪除
podman volume rm mydata
|
安全功能
無權限容器
1
2
3
4
5
| # 移除所有 capabilities
podman run --cap-drop=all nginx
# 只保留必要的
podman run --cap-drop=all --cap-add=net_bind_service nginx
|
SELinux
1
2
| # 啟用 SELinux 標籤
podman run -v /data:/data:Z nginx
|
唯讀根檔案系統
1
| podman run --read-only nginx
|
與 Docker 差異
| 功能 | Docker | Podman |
|---|
| 守護程序 | 需要 | 不需要 |
| Rootless | 選配 | 預設 |
| Swarm | 支援 | 不支援 |
| Compose | 原生 | podman-compose |
| Pod | 不支援 | 原生支援 |
相關連結
延伸閱讀