本文介绍如何安装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容器
|