podman容器的使用

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镜像搜索
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

# 创建ipv4网络指定网段
podman network create --subnet 192.5.0.0/16 newnet
cat /etc/containers/networks/newnet.json

# 创建ipv6网络指定网段
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
# 挂载web2目录到容器目录
podman run -dt --name web2 -v /web2:/usr/local/apache2/htdocs httpd

# 查看容器网络
podman inspect web2 | grep 10.88

# 访问 web 页面
podman exec -i centos1 curl 10.88.0.12

image-20230704101042716

# 关闭 selinux 再次访问
setenforce 0
podman exec -i centos1 curl 10.88.0.12

image-20230704101330598

# 对比文件的selinux标记
ls -ldZ /web2/
ls -ldZ /var/lib/containers/storage/volumes/web/
setenforce 1

image-20230704101419931

# 挂载目录时,加上 Z 参数解决 selinux 问题
podman run -dt --name web3 -p 22222:80 -v /web2:/usr/local/apache2/htdocs:Z httpd
curl localhost:22222

image-20230704101531496

podman 容器自启动

# --name表示使用容器的名字来代替容器的ID
# --files表示生成systemd的服务文件
# web1表示使用哪个容器生成systemd的服务文件

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 容器

# podman如果要使用普通用户来管理容器,那么这个普通用户必须是ssh登陆或者通过终端登陆才行。否则会有问题。
useradd greg
echo 123 | passwd --stdin greg
ssh greg@localhost

# root用户的images,普通用户是看不到的。所以普通用户需要自己拉image
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

# 非根用户使用systemd接管podman容器
# 创建~/.config/systemd/user目录来存放普通用户的systemd文件
mkdir -p ~/.config/systemd/user

# 生成systemd服务文件
podman generate systemd --new --files --name web1

# 将服务文件移动到普通用户的systemd的目录文件
mv container-web1.service ~/.config/systemd/user/

# 恢复SELinux文件的安全上下文
restorecon -RvF ~/.config/systemd/user/container-web1.service

# 赋予普通用户的systemd管理权限
loginctl enable-linger
systemctl --user daemon-reload
systemctl --user enable container-web1.service --now

# 重启测试
reboot
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值