Skip to content

Podman

官网

官方文档

参考链接

下载链接

bash
https://github.com/containers/podman/releases/download/v5.7.1/podman-installer-macos-arm64.pkg
https://github.com/containers/podman/releases/download/v5.7.1/podman-installer-windows-amd64.msi
https://github.com/containers/podman/releases/download/v5.7.1/podman-remote-static-linux_amd64.tar.gz

安装

Windows

Windows推荐直接使用msi安装包, Podman Compose安装依赖Python环境

bash
pip install podman-compose

安装目录如下

Directory or fileDescription
%LocalAppData%\Programs\Podman\Installation directory
%APPDATA%\containers\containers.conf.d\99-podman-machine-provider.confInstaller created configuration file
%APPDATA%\containers\containers.confClient main configuration file
%APPDATA%\containers\podman-connections.jsonClient connections configuration file
%USERPROFILE%\.local\share\containers\podman\machineMachines data directory
%USERPROFILE%\.config\containers\podman\machine\Machines configuration directory
%USERPROFILE%\.local\share\containers\storage\podman\Containers and images storage layers
%ProgramFiles%\Podman\Machine-scope installation directory
%ProgramData%\containers\containers.conf.d\99-podman-machine-provider.confMachine-scope installer created conf file
%ProgramData%\containers\containers.confMachine-scope client configuration file

MacOS

MacOS推荐用Homebrew安装

bash
brew install podman
brew install podman-compose

安装目录如下

Directory or fileDescription
%LocalAppData%\Programs\Podman\Installation directory

设置镜像源和网络代理

Linux

bash
# 安装
sudo dnf -y install podman
sudo dnf -y install podman-compose
# 卸载
sudo dnf -y remove podman-compose

验证是否安装成功

命令行执行的适合会有警告, 可以配置关闭警告

bash
podman version
podman compose version

配置文件

目测南京大学的镜像源相对稳定,无特殊要求,可以直接使用,无需设置代理

Windows

Windows 配置文件路径 - %APPDATA%\containers\containers.conf

MacOS

MacOS 配置文件路径 - ~/.config/containers/containers.conf

示例

containers.conf

toml
[engine]
env = ["https_proxy=http://127.0.0.1:7890", "http_proxy=http://127.0.0.1:7890", "no_proxy=localhost;127.*;192.168.*;10.*;172.*;<local>"]
compose_warning_logs = false

registries.conf

toml
[engine]
#
unqualified-search-registries = ["docker.io"]
[[registry]]
prefix = "docker.io"
location = "docker.io"
[[registry.mirror]]
location = "docker.nju.edu.cn"
[[registry.mirror]]
location = "docker.m.daocloud.io"

初始化

MacOS

bash
podman machine init --rootful

Windows

系统盘可能空间不够,推荐使用WSL标准方式,先初始化默认机器,备份后再导入的方式

准备

bash
# 停止并删除默认机器
podman machine stop
podman machine rm
# 清空所有数据
Remove-Item "$env:USERPROFILE\.local\share\containers\podman\*" -Recurse -Force -ErrorAction SilentlyContinue
# 清空数据目录
Remove-Item "D:\Tools\Podman\*" -Recurse -Force -ErrorAction SilentlyContinue

初始化默认机器

特别注意不要启动默认机器

bash
# 初始化默认机器
podman machine init --rootful
# 查看WSL实例
wsl -l -v
# 导出实例
wsl --export podman-machine-default D:\Temp\default.tar
# 注销实例
wsl --unregister podman-machine-default
# 创建数据目录
New-Item -ItemType Directory -Path "D:\Tools\Podman\machine" -Force
# 导入实例
wsl --import podman-machine-default "D:\Tools\Podman\machine" "D:\Temp\default.tar" --version 2
# 启动机器
sudo podman machine start
# 删除备份文件
Remove-Item "D:\Temp\default.tar"

机器启动后,因为机器实际上是再WSL中运行的虚拟机,所以配置镜像源需要进入虚拟机中配置

目录挂载

bash
# 进入虚拟机
podman machine ssh
# 打开 wsl.conf 配置文件
sudo vi /etc/wsl.conf

增加下面配置

toml
[automount]
options = "metadata,umask=22,fmask=11"

配置镜像源

目测南京大学的镜像源相对稳定,无特殊要求,可以直接使用,无需设置代理

bash
# 确保机器已启动
podman machine start
# 进入虚拟机终端
podman machine ssh
# 查看配置文件
sudo cat /etc/containers/registries.conf
# 备份配置文件
sudo mv /etc/containers/registries.conf /etc/containers/registries.conf.bak
# 编辑配置文件
sudo vi /etc/containers/registries.conf

把下面内容添加到配置文件中

toml
#
unqualified-search-registries = ["docker.io"]
[[registry]]
prefix = "docker.io"
location = "docker.io"
[[registry.mirror]]
location = "docker.nju.edu.cn"
[[registry.mirror]]
location = "docker.m.daocloud.io"

保存后重启虚拟机

Linux

常用操作

切换模式

bash
#
# 切换模式
#
podman machine stop
podman machine set --rootful
podman machine start
#
# 设置命令行链接默认模式
#
podman system connection list
podman system connection default podman-machine-default-root
#
# 命令行内部切换内模式
#
podman machine ssh
sudo -i

配置代理和镜像

Podman Machine

bash
# 进入虚拟机
podman machine ssh
# 查看配置文件
sudo cat /etc/containers/containers.conf
# 查看配置文件
sudo mv /etc/containers/containers.conf /etc/containers/containers.conf.bak
# 编辑配置文件
sudo vi /etc/containers/containers.conf

添加下面内容

Windows

toml
[containers]

[engine]
cgroup_manager = "cgroupfs"
http_proxy = "http://host.containers.internal:7890"
https_proxy = "http://host.containers.internal:7890"
no_proxy = "localhost,127.0.0.1,192.168.0.0/16,10.0.0.0/8,172.16.0.0/12"

# Using iptables until we fix nftables on WSL:
# https://github.com/containers/podman/issues/25201
[network]
firewall_driver = "iptables"

Linux && MacOS

toml
[engine]
http_proxy = "http://host.containers.internal:7890"
https_proxy = "http://host.containers.internal:7890"
no_proxy = "localhost,127.0.0.1,192.168.0.0/16,10.0.0.0/8,172.16.0.0/12"

推出虚拟机并重启

bash
podman machine stop
podman machine start

数据存储

bash
### 创建卷
podman volume create data
### 查看卷
podman volume inspect dat
### 备份数据
podman cp pgsql:/var/lib/data ./data-backup
### 还原数据
podman cp ./data-backup pgsql:/var/lib/data

PGSQL数据备份和数据还原

bash
# 备份数据库
podman exec -t pgsql pg_dump dbname > backup.sql
# 还原数据库
podman exec -i pgsql psql dbname < backup.sql

MYSQL数据备份和数据还原

bash
# 备份数据库
podman exec mysql mysqldump -u root -p dbname > backup.sql
# 还原数据库
podman exec -i mysql mysql -u root -p dbname < backup.sql

常见中间件

RabbitMQ

bash
podman unshare chmod 600 D:\Data\podman\rabbitmq\data\.erlang.cookie

MySQL

MySQL初始化可能会因为my.cnf文件的权限导致无法正常初始化,可以在初始化后进入容器,修改配置文件权限,再重新启动容器

bash
# 进入容器
podman exec -it mysql bash
# 修改配置文件权限
chmod 644 /etc/my.cnf
bash
# 关闭虚拟机
wsl --shutdown
# 重启虚拟机
podman machine start
# 创建软连接指向本地目录
sudo ln -s /mnt/d/wsl /data