podman 安装
如果你想在rhel系统中玩podman,必须是rhel8.2版本以上。podman版本是1.9.3。从centos8.2开始默认情况下,除了最小化安装之外,系统都会默认安装podman。
如果你使用rhel8.2以上的版本,那么就直接安装podman就可以了。
在rhel8以上的系统中,默认的appstream中已经集成了podman的软件。
yum -y install podman
podman 配置镜像加速
cp /etc/containers/registries.conf{,.bak}
cat > /etc/containers/registries.conf << 'END'
unqualified-search-registries = ["docker.io"]
[[registry]]
prefix = "docker.io"
location = "fiyc0dbc.mirror.aliyuncs.com"
END
mv /etc/containers/registries.conf.d/000-shortnames.conf /etc/containers/registries.conf.d/000-shortnames.conf.bak
mv /etc/containers/registries.conf.d/001-rhel-shortnames.conf /etc/containers/registries.conf.d/001-rhel-shortnames.conf.bak
podman 镜像管理
podman pull httpd
podman images
podman image rm docker.io/library/ubuntu
podman save > centos-latest.tar docker.io/library/centos:latest
ls -lh centos-latest.tar
podman load -i centos-latest.tar
podman search nginx
podman 容器管理
podman run -dt --name web1 httpd
podman exec -it web1 bash
podman container stop web1
podman ps -a
podman start web1
podman restart web1
podman stop web1
podman rm web1
podman rm -f container
podman run -dt --name web1 httpd
podman inspect web1
podman 网络管理
nmcli device status
nmcli con show
podman network create
podman network ls
nmcli con show
ip a show
podman run -dt --name web2 --network podman1 httpd
bridge link
podman network create --subnet 192.5.0.0/16 newnet
cat /etc/containers/networks/newnet.json
podman network create --subnet 2001:db8::/64 --ipv6 newnetv6
podman run -dt --name web4 --network newnetv6 httpd
podman inspect web4 | grep GlobalIPv6Address
podman network ls
podman network rm newnet
podman network rm newnetv6
podman network rm podman1
podman network ls
podman run -dt --name web2 -p 12345:80 httpd
podman 持久化存储
数据卷
podman volume ls
podman volume create volume1
find / -name volume1
podman volume create web
podman run -dt --name centos1 -v web:/web centos
podman exec -i centos1 df -Th
podman volume inspect web
podman inspect centos1 | grep web
podman run -dt --name web1 -v web:/usr/local/apache2/htdocs httpd
podman exec -i web1 df -Th
bind mounts
mkdir /web2
echo "dmxy" >> /web2/index.html
podman run -dt --name web2 -v /web2:/usr/local/apache2/htdocs httpd
podman inspect web2 | grep 10.88
podman exec -i centos1 curl 10.88.0.12
setenforce 0
podman exec -i centos1 curl 10.88.0.12
ls -ldZ /web2/
ls -ldZ /var/lib/containers/storage/volumes/web/
setenforce 1
podman run -dt --name web3 -p 22222:80 -v /web2:/usr/local/apache2/htdocs:Z httpd
curl localhost:22222
podman 容器自启动
podman run -dt --name web1 httpd
podman generate systemd --name web1
podman generate systemd --name web1 --files
mv container-web1.service /etc/systemd/system/
restorecon -RvF /etc/systemd/system/container-web1.service
systemctl restart container-web1.service
podman run -dt --name web2 -p 88:80 -v /web2:/usr/local/apache2/htdocs:Z httpd
podman ps -a
curl localhost:88
podman generate systemd --files --new --name web2
cat container-web2.service
mv container-web2.service /etc/systemd/system/
restorecon -RvF /etc/systemd/system/container-web2.service
systemctl enable container-web2.service --now
podman ps -a
非根用户使用 podman 容器
useradd greg
echo 123 | passwd --stdin greg
ssh greg@localhost
podman pull httpd
podman images
mkdir web1
echo web1 > web1/index.html
podman run -dt --name web1 -p 54321:80 -v /home/greg/web1:/usr/local/apache2/htdocs:Z httpd
podman ps
curl localhost:54321
mkdir -p ~/.config/systemd/user
podman generate systemd --new --files --name web1
mv container-web1.service ~/.config/systemd/user/
restorecon -RvF ~/.config/systemd/user/container-web1.service
loginctl enable-linger
systemctl --user daemon-reload
systemctl --user enable container-web1.service --now
reboot