使用 Podman 運(yùn)行一個(gè) “hello world” MLCube
MLCube 是一個(gè)新的基于基礎(chǔ)設(shè)施規(guī)范的開源容器,被引入到基于 Python 的機(jī)器學(xué)習(xí)工作流程中,以實(shí)現(xiàn)可重復(fù)性。它可以利用諸如 Podman、Singularity 和 Docker 等工具。也支持在遠(yuǎn)程平臺(tái)上的執(zhí)行。開發(fā) MLCube 的 MLCommons 最佳實(shí)踐工作組的主席之一是來(lái)自 Red Hat 的 Diane Feddema。這篇介紹性文章解釋了如何在 Fedora Linux 上使用 Podman 運(yùn)行 “hello world” MLCube 例子。
Yazan Monshed 寫了一篇關(guān)于 Fedora 上的 Podman 的非常有用的介紹,對(duì)這里使用的一些步驟給出了更多細(xì)節(jié)。
首先安裝必要的依賴項(xiàng)。
sudo dnf -y update
sudo dnf -y install podman git virtualenv \
policycoreutils-python-utils
然后,按照文檔的要求,設(shè)置一個(gè)虛擬環(huán)境并獲得示例代碼。為了確保可重復(fù)性,使用一個(gè)特定的提交,因?yàn)樵擁?xiàng)目正在積極改進(jìn)。
virtualenv -p python3 ./env_mlcube
source ./env_mlcube/bin/activate
git clone https://github.com/mlcommons/mlcube_examples.git
cd ./mlcube_examples/hello_world
git checkout 5fe69bd
pip install mlcube mlcube-docker
mlcube describe
現(xiàn)在,通過(guò)編輯 $HOME/mlcube.yaml 文件,將運(yùn)行器命令從 docker 改為 podman,即:
docker: docker
改為:
docker: podman
如果你使用的是 x86_64 架構(gòu)的電腦,你可以用以下方式獲取容器:
mlcube configure --mlcube=. --platform=docker
你會(huì)看到一些選項(xiàng):
? Please select an image:
? registry.fedoraproject.org/mlcommons/hello_world:0.0.1
registry.access.redhat.com/mlcommons/hello_world:0.0.1
docker.io/mlcommons/hello_world:0.0.1
quay.io/mlcommons/hello_world:0.0.1
選擇 docker.io/mlcommons/hello_world:0.0.1 來(lái)獲取容器。
如果你的電腦不是 x86_64 架構(gòu)的,你需要構(gòu)建容器。改變文件 $HOME/mlcube.yaml,將這一行:
build_strategy: pull
變?yōu)椋?/p>
build_strategy: auto
然后用以下方法構(gòu)建容器:
mlcube configure --mlcube=. --platform=docker
要運(yùn)行測(cè)試,你可能需要在目錄中適當(dāng)?shù)卦O(shè)置 SELinux 權(quán)限。你可以通過(guò)輸入以下內(nèi)容來(lái)檢查 SELinux 是否已經(jīng)啟用:
sudo sestatus
應(yīng)該會(huì)有類似這樣的輸出:
SELinux status: enabled
Josphat Mutai、Christopher Smart 和 Daniel Walsh 解釋說(shuō),在為容器使用的文件設(shè)置適當(dāng)?shù)?SELinux 策略時(shí),你需要謹(jǐn)慎。在這里,你將允許容器讀取和寫入 workspace 目錄。
sudo semanage fcontext -a -t container_file_t "$PWD/workspace(/.*)?"
sudo restorecon -Rv $PWD/workspace
現(xiàn)在檢查目錄策略:
ls -Z
輸出結(jié)果類似于:
unconfined_u:object_r:user_home_t:s0 Dockerfile
unconfined_u:object_r:user_home_t:s0 README.md
unconfined_u:object_r:user_home_t:s0 mlcube.yaml
unconfined_u:object_r:user_home_t:s0 requirements.txt
unconfined_u:object_r:container_file_t:s0 workspace
現(xiàn)在運(yùn)行這個(gè)例子:
mlcube run --mlcube=. --task=hello --platform=docker
mlcube run --mlcube=. --task=bye --platform=docker
最后,檢查輸出:
cat workspace/chats/chat_with_alice.txt
有類似于以下的文字:
Hi, Alice! Nice to meet you.
Bye, Alice! It was great talking to you.
你可以按照 這里 的描述創(chuàng)建你自己的 MLCube。歡迎對(duì) MLCube 示例庫(kù) 做出貢獻(xiàn)。Udica 是一個(gè)新項(xiàng)目,它承諾為容器提供更精細(xì)的 SELinux 策略控制,便于系統(tǒng)管理員應(yīng)用。這些項(xiàng)目的積極開發(fā)正在進(jìn)行中。對(duì)它們進(jìn)行測(cè)試并提供反饋,將有助于使帶有 SELinux 的系統(tǒng)上的安全數(shù)據(jù)管理更容易、更有效。