如何快速建立一个podman环境
本文介绍如何安装podman,并创建podman容器
环境
Centos8安装podman
Podman 是一个容器环境,首先在主机上安装 Podman。执行下面命令来安装podman:@localhost ~]# yum -y install podman然后修改一下用户命名空间的大小:# echo "user.max_user_namespaces=28633" >> /etc/sysctl.d/userns.conf# sysctl -p /etc/sysctl.d/userns.confuser.max_user_namespaces = 28633下面创建一个podman容器来看一下吧,下面使用的是RHEL的UBI镜像:# 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 doneCopying blob 63f9f4c31162 doneCopying config cc06568478 doneWriting 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"https://www.linuxprobe.com/wp-content/uploads/2021/12/image1-4.png创建Dockerfile
现在可以创建一个 Dockerfile 来指定如何构建新的镜像。首先需要为 Dockerfile 文件创建一个目录:# mkdir ~/myc# cd ~/myc创建一个文件Dockerfile文件,来建立一个新镜像:# vim DockerfileFROM ubi8/ubi:latestRUN dnf install -y nanohttps://www.linuxprobe.com/wp-content/uploads/2021/12/image2-3.png
开始使用podman build来创建容器:# podman build -f Dockerfile -t ubi-with-nano# 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...https://www.linuxprobe.com/wp-content/uploads/2021/12/image3-3.png
使用podman images来确认是否创建新镜像:# podman imageshttps://www.linuxprobe.com/wp-content/uploads/2021/12/image4-3.png
现在你可以运行容器了,检查nano编辑器是否可用:# podman run localhost/ubi-with-nano /usr/bin/which nano/usr/bin/nano通过查看nano执行文件的位置,来检查是否安装。Nano 现在已安装在你的自定义容器中。还可以交互式的运行容器:# podman run -it localhost/ubi-with-nano /bin/bash# lsbin devhomelib64 mediaopt root sbinsysusrbootetcliblost+foundmnt procrun srv tmpvar# https://www.linuxprobe.com/wp-content/uploads/2021/12/image5-3.png
在容器中运行exit来退出容器。https://www.bestcentos.com可以使用podman ps来查看运行的容器,如果需要查看以停止的容器,可以添加-a选项:# podman ps # podman ps -ahttps://www.linuxprobe.com/wp-content/uploads/2021/12/image6-2.png存储
经常让新用户感到困惑的一件事是它们的短暂性。例如进入容器里创建的文件,退出之后,再次进入发现文件没有了。下面我们将容器中需要存储永久性文件的文件夹挂载到系统的某个文件夹中。下面在本机创建一个存储位置:# mkdir /pod_data然后使用您的存储目录作为一些相关的挂载点来启动容器。此示例将本地目录/pod_data绑定到容器中名为/storage 的位置,必须在目录位置附加 :Z,以便 SELinux 可以在主机和 Podman 之间转换上下文。# podman run -it --volume /pod_data:/storage:Z localhost/ubi-with-nano# echo "hello podman" >> /storage/msg.txt# exitexit# cat /pod_data/msg.txt hello podmanhttps://www.linuxprobe.com/wp-content/uploads/2021/12/image7-2.png
可以看到目录绑定之后,在容器中写入数据,退出容器,在本机的/pod_data可以看到写入的内容。总结
本文介绍如何安装podman,并创建podman容器
页:
[1]