设为首页收藏本站language 语言切换
查看: 1042|回复: 0
收起左侧

如何快速建立一个podman环境

[复制链接]
发表于 2023-4-14 09:06:36 | 显示全部楼层 |阅读模式
本文介绍如何安装podman,并创建podman容器
环境

Centos8

安装podman

Podman 是一个容器环境,首先在主机上安装 Podman。执行下面命令来安装podman:

[root@localhost ~]# yum -y install podman

然后修改一下用户命名空间的大小:

[root@localhost ~]# echo "user.max_user_namespaces=28633" >> /etc/sysctl.d/userns.conf[root@localhost ~]# sysctl -p /etc/sysctl.d/userns.confuser.max_user_namespaces = 28633

下面创建一个podman容器来看一下吧,下面使用的是RHEL的UBI镜像:

[root@localhost ~]# podman run ubi8/ubi cat /etc/os-releaseResolved "ubi8/ubi" as an alias (/etc/containers/registries.conf.d/001-rhel-shortnames.conf)Trying to pull registry.access.redhat.com/ubi8/ubi:latest...Getting image source signaturesChecking if image destination supports signaturesCopying blob ce3c6836540f done  Copying blob 63f9f4c31162 done  Copying config cc06568478 done  Writing manifest to image destinationStoring signaturesNAME="Red Hat Enterprise Linux"VERSION="8.5 (Ootpa)"ID="rhel"ID_LIKE="fedora"VERSION_ID="8.5"PLATFORM_ID="platform:el8"PRETTY_NAME="Red Hat Enterprise Linux 8.5 (Ootpa)"ANSI_COLOR="0;31"CPE_NAME="cpe:/o:redhat:enterprise_linux:8::baseos"HOME_URL="https://www.redhat.com/"DOCUMENTATION_URL="https://access.redhat.com/documentation/red_hat_enterprise_linux/8/"BUG_REPORT_URL="https://bugzilla.redhat.com/"REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 8"REDHAT_BUGZILLA_PRODUCT_VERSION=8.5REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux"REDHAT_SUPPORT_PRODUCT_VERSION="8.5"


                               
登录/注册后可看大图

创建Dockerfile

现在可以创建一个 Dockerfile 来指定如何构建新的镜像。首先需要为 Dockerfile 文件创建一个目录:

[root@localhost ~]# mkdir ~/myc[root@localhost ~]# cd ~/myc

创建一个文件Dockerfile文件,来建立一个新镜像:

[root@localhost myc]# vim DockerfileFROM ubi8/ubi:latestRUN dnf install -y nano


                               
登录/注册后可看大图

开始使用podman build来创建容器:

[root@localhost myc]# podman build -f Dockerfile -t ubi-with-nano[root@localhost myc]# podman build -f Dockerfile -t ubi-with-nanoSTEP 1/2: FROM ubi8/ubi:latestSTEP 2/2: RUN dnf install -y nanoUpdating Subscription Management repositories.Unable to read consumer identity...


                               
登录/注册后可看大图

使用podman images来确认是否创建新镜像:

[root@localhost myc]# podman images


                               
登录/注册后可看大图

现在你可以运行容器了,检查nano编辑器是否可用:

[root@localhost myc]# podman run localhost/ubi-with-nano /usr/bin/which nano/usr/bin/nano

通过查看nano执行文件的位置,来检查是否安装。

Nano 现在已安装在你的自定义容器中。还可以交互式的运行容器:

[root@localhost myc]# podman run -it localhost/ubi-with-nano /bin/bash[root@d1f0e46f2b6d /]# lsbin   dev  home  lib64      media  opt   root sbin  sys  usrboot  etc  lib  lost+found  mnt    proc  run srv   tmp  var[root@d1f0e46f2b6d /]#


                               
登录/注册后可看大图

在容器中运行exit来退出容器。https://www.bestcentos.com

可以使用podman ps来查看运行的容器,如果需要查看以停止的容器,可以添加-a选项:

[root@localhost myc]# podman ps [root@localhost myc]# podman ps -a


                               
登录/注册后可看大图

存储

经常让新用户感到困惑的一件事是它们的短暂性。例如进入容器里创建的文件,退出之后,再次进入发现文件没有了。下面我们将容器中需要存储永久性文件的文件夹挂载到系统的某个文件夹中。下面在本机创建一个存储位置:

[root@localhost ~]# mkdir /pod_data

然后使用您的存储目录作为一些相关的挂载点来启动容器。此示例将本地目录/pod_data绑定到容器中名为/storage 的位置,必须在目录位置附加 :Z,以便 SELinux 可以在主机和 Podman 之间转换上下文。

[root@localhost ~]# podman run -it --volume /pod_data:/storage:Z localhost/ubi-with-nano[root@d590bc344b76 /]# echo "hello podman" >> /storage/msg.txt[root@d590bc344b76 /]# exitexit[root@localhost ~]# cat /pod_data/msg.txt hello podman


                               
登录/注册后可看大图

可以看到目录绑定之后,在容器中写入数据,退出容器,在本机的/pod_data可以看到写入的内容。

总结

本文介绍如何安装podman,并创建podman容器


您需要登录后才可以回帖 登录 | 论坛注册

本版积分规则

QQ|Archiver|手机版|小黑屋|sitemap|鸿鹄论坛 ( 京ICP备14027439号 )  

GMT+8, 2025-2-2 20:50 , Processed in 0.075406 second(s), 11 queries , Redis On.  

  Powered by Discuz!

  © 2001-2025 HH010.COM

快速回复 返回顶部 返回列表