如何快速建立一个podman环境

本文介绍如何安装podman,并创建podman容器环境Centos8安装podmanPodman 是一个容器环境,首先在主机上安装 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.conf
user.max_user_namespaces = 28633
下面创建一个podman容器来看一下吧,下面使用的是RHEL的UBI镜像:[root@localhost ~]# podman run ubi8/ubi cat /etc/os-release
Resolved "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 signatures
Checking if image destination supports signatures
Copying blob ce3c6836540f done
Copying blob 63f9f4c31162 done
Copying config cc06568478 done
Writing manifest to image destination
Storing signatures
NAME="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.5
REDHAT_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 Dockerfile

FROM ubi8/ubi:latest
RUN 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-nano
STEP 1/2: FROM ubi8/ubi:latest
STEP 2/2: RUN dnf install -y nano
Updating 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 /]# ls
bin dev home lib64 media opt root sbin sys usr
boot etc lib lost+found mnt proc run srv tmp var
[root@d1f0e46f2b6d /]#

图片
在容器中运行exit来退出容器。可以使用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 /]# exit
exit
[root@localhost ~]# cat /pod_data/msg.txt
hello podman

图片
可以看到目录绑定之后,在容器中写入数据,退出容器,在本机的/pod_data可以看到写入的内容。总结本文介绍如何安装podman,并创建podman容器

你可能感兴趣的:(如何快速建立一个podman环境)