生成 OCI bundle

最后发布时间 : 2026-08-10 11:51:12 浏览量 :

生成 OCI bundle 的核心就是:

准备 root filesystem(rootfs) + 生成 OCI Runtime Spec 的 config.json

bundle 是 OCI Runtime 使用的输入。

结构:

my-container/
├── config.json
└── rootfs/
    ├── bin/
    ├── usr/
    ├── lib/
    └── etc/

方法 1:使用 youki 自带命令生成(最简单)

youki/runc 都支持生成 OCI spec。

例如:

mkdir my-container
cd my-container

youki spec

生成:

my-container/
└── config.json

然后准备:

my-container/
├── config.json
└── rootfs/

方法 2:从 Docker/OCI 镜像生成 bundle(实际开发方式)

你的流程应该是:

Docker Image
      |
      |
OCI Registry
      |
      |
pull layers
      |
      |
unpack
      |
      |
rootfs
      |
      |
OCI config.json
      |
      |
youki run

第一步:拉取 OCI 镜像

例如:

quay.io/biocontainers/samtools

使用 Go:

推荐:

go-containerregistry

仓库:

go-containerregistry GitHub repository

代码:

img, err := remote.Image(
    name.ParseReference(
        "quay.io/biocontainers/samtools",
    ),
)

得到:

  • manifest
  • layers
  • config

第二步:解压 layers

OCI image:

image
 |
 + layer1.tar.gz
 |
 + layer2.tar.gz
 |
 + layer3.tar.gz

例如:

layer1:
 /
 ├── usr
 ├── bin

layer2:
 /
 ├── lib

按顺序:

layer1
 +
layer2
 +
layer3

=

rootfs

得到:

bundle/

└── rootfs/

    ├── usr/bin/samtools
    ├── usr/lib
    └── etc

第三步:生成 config.json

OCI Spec:

仓库:

opencontainers runtime-spec GitHub repository

Go 有库:

github.com/opencontainers/runtime-spec/specs-go

例如:

spec := specs.Spec{
    Version: specs.Version,

    Root: &specs.Root{
        Path: "rootfs",
    },

    Process: &specs.Process{
        Args: []string{
            "/bin/sh",
        },
    },
}

保存:

json.MarshalIndent(
    spec,
)

生成:

config.json

方法 3:使用 umoci(推荐测试)

umoci

它专门做:

OCI image ↔ bundle

例如:

拉镜像

umoci pull \
  --image docker://alpine:latest \
  alpine.img

创建 bundle

umoci unpack \
  --image alpine.img \
  bundle

结果:

bundle/

├── config.json
└── rootfs/

直接:

youki run alpine-test

方法 4:使用 skopeo + umoci

生产环境常见:

registry
   |
skopeo
   |
OCI layout
   |
umoci
   |
bundle
   |
runc/youki

BRAVE 如果实现,推荐架构

不要直接:

Docker image
       |
       |
youki

中间需要一个 Image Runtime:

                 BRAVE

                    |
                    |

            Image Manager

                    |
        ----------------------

        OCI Registry
        Docker Hub
        BioContainers

                    |
                    |

             OCI Image Store

                    |
                    |

             Bundle Builder

                    |
                    |

        +-------------------+
        | config.json       |
        | rootfs/           |
        +-------------------+

                    |
                    |

              youki run


Go 项目需要的主要库

功能Go库
拉OCI镜像go-containerregistry
OCI registry协议oras-go
OCI specruntime-spec
运行runtimeyouki/runc
解压layerarchive/tar

一个最小实现流程

你的 Go 程序:

brave run samtools

内部:

1. 检查本地image
2. pull OCI image
3. unpack layers
4. create bundle/
5. write config.json
6. exec youki run bundle
7. attach stdout/stderr

这基本就是一个简化版:

containerd + runc

而且完全可以做成用户无需 Docker 的生信运行环境。你现在研究的 BRAVE 架构,其实正好对应这一层。