本文介紹如何安裝podman,並創建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.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 文件創建一個目錄:
[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容器