Apptainer 容器快速上手

Build a Container 构建容器

The build command is the “Swiss army knife” of container creation. You can use it to download and assemble existing containers from external resources like Docker Hub and other OCI registries. You can use it to convert containers between the formats supported by Apptainer. And you can use it in conjunction with a Apptainer definition file to create a container from scratch and customized it to fit your needs.

构建命令是创建容器的“瑞士军刀”。您可以使用它从外部资源(如Docker Hub和其他OCI注册表)下载和组装现有容器。您可以使用它在Apptainer支持的格式之间转换容器。您可以将它与Apptainer定义文件结合使用,从零开始创建容器并对其进行定制以满足您的需求。

Overview 概述

The build command accepts a target as input and produces a container as output.

build命令接受目标作为输入并生成一个容器作为输出。

The type of target given determines the method that build will use to create the container. It can be one of the following:

build给定目标的类型决定了用于创建容器的方法。它可以是以下之一:

  • URI beginning with docker:// to build from Docker Hub

    以 docker:// 开头的 URI 以从 Docker Hub 生成

  • URI beginning with oras:// to build from an OCI registry that supports OCI Artifacts

    从支持 OCI 构件的 OCI 注册表 构建的以 oras:// 开头的 URI

  • URI beginning with library:// to build from the Container Library

    以 library:// 开头的 URI 以从 Container Library 生成

  • path to an existing container on your local machine

    本地计算机上现有容器的路径

  • path to a directory to build from a sandbox

    要从 沙盒 生成的目录的路径

  • path to a Apptainer definition file

    Apptainer 定义文件 的路径

容器来源

Downloading an existing container from Docker Hub

从 Docker Hub 下载现有容器 (推介)

$ apptainer build alpine.sif docker://alpine

Downloading an existing container from a Library API Registry

从库 API 注册表下载现有容器

$ apptainer build lolcow.sif library://lolcow

The first argument (lolcow.sif) specifies the path and name for your container. The second argument (library://lolcow) gives the Container Library URI from which to download. By default, the container will be converted to a compressed, read-only SIF. If you want your container in a writable format, use the --sandbox option.

第一个参数 ( lolcow.sif) 指定容器的路径和名称。第二个参数 ( library://lolcow) 给出要从中下载的容器库 URI。默认情况下,容器将转换为压缩的只读 SIF。如果您希望容器采用可写格式,请使用该--sandbox选项。

Creating writable --sandbox directories

创建可写--sandbox目录

$ apptainer build --sandbox alpine/ docker://alpine

The resulting directory operates just like a container in a SIF file. To make persistent changes within the sandbox container, use the --writable flag when you invoke your container.

生成的目录就像 SIF 文件中的容器一样运行。要在沙箱容器内进行持久更改,请 --writable在调用容器时使用该标志。

$ apptainer shell --writable alpine/

Converting containers from one format to another

将容器从一种格式转换为另一种格式

If you already have a container saved locally, you can use it as a target to build a new container. This allows you convert containers from one format to another. For example, if you had a sandbox container called development/ and you wanted to convert it to a SIF container called production.sif, you could do so as follows:

如果您已经在本地保存了容器,则可以将其用作构建新容器的目标。这允许您将容器从一种格式转换为另一种格式。例如,如果您有一个名为 的沙盒容器development/,并且希望将其转换为名为 的 SIF 容器production.sif,则可以按如下方式执行操作:

$ apptainer build production.sif development/

Use care when converting a sandbox directory to the default SIF format. If changes were made to the writable container before conversion, there is no record of those changes in the Apptainer definition file, which compromises the reproducibility of your container. It is therefore preferable to build production containers directly from an Apptainer definition file instead.

将沙盒目录转换为默认 SIF 格式时要小心。如果在转换之前对可写容器进行了更改,则 Apptainer 定义文件中不会记录这些更改,这会损害容器的可重现性。因此,最好直接从 Apptainer 定义文件构建生产容器。

Building containers from Apptainer definition files

从 Apptainer 定义文件构建容器

Apptainer definition files are the most powerful type of target when building a container. For detailed information on writing Apptainer definition files, please see the Container Definitions documentation. Suppose you already have the following container definition file called, lolcow.def, and you want to use it to build a SIF container:

Apptainer 定义文件是构建容器时最强大的目标类型。有关编写 Apptainer 定义文件的详细信息,请参阅容器定义文档。假设您已经有以下名为 lolcow.def 的容器定义文件,并且您想使用它来构建 SIF 容器:

$ vim lolcow.def
Bootstrap: docker
From: ubuntu:20.04

%post
    apt-get -y update
    apt-get -y install cowsay lolcat

%environment
    export LC_ALL=C
    export PATH=/usr/games:$PATH

%runscript
    date | cowsay | lolcat

You can do so with the following command.

您可以使用以下命令来执行此操作。

$ apptainer build lolcow.sif lolcow.def

Note

Beware that it is possible to build an image on a host and have the image not work on a different host. This could be because of the default compressor supported by the host. For example, when building an image on a host in which the default compressor is xz and then trying to run that image on a node where the only compressor available is gzip. 有可能在一台主机上构建映像,但该映像无法在其他主机上运行。这可能是由于主机支持的默认压缩器所致。例如,在默认压缩器为 xz 的主机上构建映像,然后在唯一可用压缩器为 gzip 的节点上运行该映像时。

Building encrypted containers

构建加密容器

With Apptainer it is possible to build and run encrypted containers. Encrypted containers are decrypted at runtime entirely in memory, meaning that no intermediate decrypted data is ever written to disk. See encrypted containers for more details. 使用 Apptainer,可以构建和运行加密容器。加密容器在运行时完全在内存中解密,这意味着不会将中间解密数据写入磁盘。有关详细信息,请参阅加密容器

更多容器构建的参数,请参考Build a Container

举例

Docker Hub 中构建容器镜像

$ apptainer build samtools.sif docker://staphb/samtools
$ apptainer build wgcna.sif docker://rcoan/wgcna
$ apptainer build WGCNA.sif docker://jpcartailler/iterativewgcna

通过 沙箱 构建 Apptainer 容器

$ apptainer build --sandbox wgcna/ docker://r-base

$ apptainer shell --writable wgcna/

$ R
install.packages("BiocManager")

BiocManager::install("WGCNA")
BiocManager::install("tidyverse")
BiocManager::install("ggplot2")
BiocManager::install("ggrepel")
BiocManager::install("cowplot")
BiocManager::install("ggthemes")
install.packages("dplyr")


# 遇到其他缺少的包,安装就可以了
# 要以管理员权限运行,遇到系统依赖包时需要管理员权限安装
# 沙盒还有个好处就是,可以在人家已经构建好的容器上进行修改
$ apptainer build wgcna.sif wgcna/

容器运行

$ apptainer exec WGCNA.sif Rscript wgcna.R

exec 命令允许通过容器执行容器中的相关命令或软件。

相比于一般运行时的Rscript wgcna.R, 只在前面加上apptainer exec WGCNA.sif即可。

更多详细问题,请参考 Apptainer 官方文档

本文由 mdnice 多平台发布

  • 18
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值