拓冰建站拓冰建站
首页 / 资讯中心 / 正文

Dagger TypeScript SDK 详解:ContainerWithMountedFileOpts 选项类型与 withMountedFile 文件挂载机制

Dagger TypeScript SDK 详解ContainerWithMountedFileOpts 选项类型与 withMountedFile 文件挂载机制【免费下载链接】daggerAutomation engine to build, test and ship any codebase. Runs locally, in CI, or directly in the cloud项目地址: https://gitcode.com/GitHub_Trending/da/dagger本文以 Dagger 0.20 版 TypeScript 参考文档中的类型别名ContainerWithMountedFileOpts为主体完整讲解其expand、owner等可选属性的语义与取值格式并结合仓库中当前 SDK 类型定义client.gen.ts与引擎核心实现container.go说明该选项如何驱动Container.withMountedFile()的完整执行链路路径环境变量展开、属主继承、绝对路径解析与惰性挂载状态构建。读完后你可以准确使用该选项类型完成容器文件挂载并理解每个属性在引擎内部的真实生效方式。1. 类型别名概览ContainerWithMountedFileOpts 是什么在 0.20 版的 TypeScript API 参考文档 ContainerWithMountedFileOpts.md 中该类型被定义为ContainerWithMountedFileOptsobject它是一个纯选项对象类型all-optional object专门作为Container类withMountedFile()方法的第三个参数传入用于精细控制挂载文件的行为。原文档列出了以下两个可选属性1.1 expand?可选booleanReplace ${VAR} or $VAR in the value of path according to the current environment variables defined in the container (e.g. /$VAR/foo.txt).即当设置为true时引擎会根据容器内当前已定义的环境变量替换path参数值中的${VAR}或$VAR形式的变量引用。例如容器内定义了DATA/var/data那么withMountedFile(/$DATA/foo.txt, file, { expand: true })会实际挂载到/var/data/foo.txt。1.2 owner?可选stringA user or user:group to set for the mounted file.The user and group can either be an ID (1000:1000) or a name (foo:bar).If the group is omitted, it defaults to the same as the user.用于设置挂载文件的属主格式要点如下可以是用户 ID:组 ID如1000:1000也可以是用户名:组名如foo:bar若省略组部分如foo或1000组默认与用户相同该值最终决定挂载到容器内的文件以哪个身份归属。1.3 当前仓库 SDK 中的完整属性集多了 inheritOwner?需要注意0.20 版参考文档记录的是当时快照的属性集。当前仓库中的 TypeScript SDK 类型定义client.gen.ts#L1018-L1037显示ContainerWithMountedFileOpts实际包含3 个可选属性export type ContainerWithMountedFileOpts { /** * A user or user:group to set for the mounted file. * * The user and group can either be an ID (1000:1000) or a name (foo:bar). * * If the group is omitted, it defaults to the same as the user. */ owner?: string /** * Set the owner to the containers current user. */ inheritOwner?: boolean /** * Replace ${VAR} or $VAR in the value of path according to the current * environment variables defined in the container (e.g. /$VAR/foo.txt). */ expand?: boolean }新增的inheritOwner?boolean语义是将挂载文件的属主设置为容器当前的运行用户。它与owner互斥——这一约束在引擎侧有明确的报错校验见 core/schema/container.go#L3397-L3405func inheritedOwner(parent dagql.ObjectResult[*core.Container], owner string, inheritOwner bool) (string, error) { if !inheritOwner { return owner, nil } if owner ! { return , errors.New(cannot set both owner and inheritOwner) } return parent.Self().Config.User, nil }也就是说inheritOwner为true时直接取父容器的Config.User若同时显式设置了owner会直接报错 cannot set both owner and inheritOwner。2. 消费该类型的方法withMountedFile 签名与调用方式在 client.gen.ts#L6117-L6136 中withMountedFile的完整签名与文档注释如下/** * Retrieves this container plus a file mounted at the given path. * param path Location of the mounted file (e.g., /tmp/file.txt). * param source Identifier of the mounted file. * param opts.owner A user or user:group to set for the mounted file. * param opts.inheritOwner Set the owner to the containers current user. * param opts.expand Replace ${VAR} or $VAR in the value of path ... */ withMountedFile ( path: string, source: File, opts?: ContainerWithMountedFileOpts, ): Container { const ctx this._ctx.select(withMountedFile, { path, source, ...opts }) return new Container(ctx) }三个位置/可选参数与返回值的要点参数类型说明pathstring挂载位置例如/tmp/file.txtsourceFile要挂载的文件对象optsContainerWithMountedFileOpts可选包含owner/inheritOwner/expand返回值Container新的容器对象原容器不变不可变/惰性求值风格注意方法体中{ path, source, ...opts }的展开方式选项对象的各键会被平铺进 GraphQL 选择参数即expand: true、owner: 1000:1000最终作为同名 GraphQL 字段发送到引擎。这也意味着方法调用是惰性的lazy——方法执行时并不真正挂载只是构建一个新的查询节点new Container(ctx)。一个贴近实际用法的调用示例对照 0.20 文档中 cookbook 的 mount-file 片段// 将文件 f 挂载到容器 /src/name并设置属主、展开路径变量 const ctr await dagger .container() .from(alpine:3) .withEnvVariable(DEST, /opt/data) .withMountedFile(/$DEST/${name}, f, { expand: true, // 将 path 中的 $DEST 按容器环境变量展开 owner: 1000:1000, // 以 UID:GID 形式属主 })官方类型文档对该方法的定位也见 getting-started/types/directory.mdxContainer.withMountedFile()returns a container plus a file mounted at the given path与withFile复制文件进镜像层的区别说明在 partials/types/_container.mdx 的表格中withFile/withMountedFile分别对应复制文件进容器与在给定路径挂载文件。3. 引擎侧实现选项属性如何逐一生效TypeScript 侧发出的 GraphQL 调用最终落到引擎的containerSchema.withMountedFile。以下结合 core/schema/container.go 的实现说明各属性在引擎内部的真实行为。3.1 参数结构默认值一目了然container.go#L2705-L2711type containerWithMountedFileArgs struct { Path string Source core.FileID Owner string default: InheritOwner bool default:false Expand bool default:false }从结构体标签可以确认owner默认空字符串、inheritOwner与expand默认均为false——即不传opts时行为与裸挂载一致路径原样使用、不改动属主。3.2 执行流程五步走container.go#L2713-L2752 的withMountedFile方法按以下顺序处理加载源文件args.Source.Load(ctx, srv)将FileID解析为真实的core.File对象展开路径变量expand 生效点调用expandEnvVar(ctx, parent.Self(), args.Path, args.Expand)见 3.3 节克隆父容器cloneContainerForSchemaChild深拷贝父容器的文件系统访问器、挂载集合与元数据快照生成子容器保证原容器不被修改解析属主owner/inheritOwner 生效点inheritedOwner(parent, args.Owner, args.InheritOwner)见 1.3 节的互斥校验逻辑构建挂载目标与惰性状态target : absPath(parent.Self().Config.WorkingDir, path) ctr.Lazy core.ContainerWithMountedFileLazy{ LazyState: core.NewLazyState(), Parent: parent, Target: target, Source: file, Owner: owner, Readonly: false, }两个值得注意的细节相对路径会基于容器工作目录解析absPath(parent.Self().Config.WorkingDir, path)会把传入的相对路径锚定到容器的WorkingDir上因此传/tmp/file.txt这类绝对路径时行为直观而传file.txt时实际挂载在Workdir/file.txt挂载是惰性Lazy的真正写入只发生在挂载集合与ContainerWithMountedFileLazy状态上Readonly: false即该挂载可写实际的文件落盘推迟到容器真正执行如Exec/Sync时才发生。这与withMountedFile方法体中返回新查询节点的惰性设计相呼应。3.3 expand 的实现细节哪些环境变量不许参与展开container.go#L3445-L3485 的expandEnvVar揭示了expand: true的完整规则func expandEnvVar(ctx context.Context, parent *core.Container, input string, expand bool) (string, error) { if !expand { return input, nil } cfg, err : parent.ImageConfig(ctx) // ... expanded : os.Expand(input, func(k string) string { if slices.Contains(secretEnvs, k) { secretEnvFoundError fmt.Errorf(expand cannot be used with secret env variable %q, k) return } if slices.Contains(volatileEnvs, k) { secretEnvFoundError fmt.Errorf(expand cannot be used with volatile env variable %q, k) return } v, _ : core.LookupEnv(cfg.Env, k) return v }) // ... }从源码结构看可以确认以下行为展开的变量值来源是镜像配置中的环境变量parent.ImageConfig(ctx)返回的cfg.Env加上容器后续withEnvVariable定义的部分——即文档所说的 environment variables defined in the container展开基于 Go 的os.Expand语义${VAR}与$VAR两种写法都支持对应文档示例/$VAR/foo.txt安全护栏如果展开过程中引用了密文环境变量通过withMountedSecret挂载为环境变量的 Secret或volatile 环境变量会直接报错expand cannot be used with secret env variable/expand cannot be used with volatile env variable拒绝展开。其设计意图是避免用可能变化的敏感/易变值参与路径推导路径属于缓存键的一部分易变值会破坏缓存稳定性。3.4 owner 与 inheritOwner 的最终语义结合 1.3 节的inheritedOwner实现属主参数的优先级为ownerinheritOwner结果任意值false默认使用该owner值空true使用父容器的当前用户Config.User非空true报错cannot set both owner and inheritOwner挂载的Owner字段随ContainerWithMountedFileLazy状态传入执行阶段引擎在真正写入挂载文件时按该属主设置文件归属。4. 实践要点小结ContainerWithMountedFileOpts的所有字段都是可选的不传opts等价于owner、inheritOwnerfalse、expandfalse见 container.go#L2705-L2711 的默认值。owner支持UID:GID或user:group两种写法组缺省时与用户相同与inheritOwner二者只能取其一。expand: true仅在需要按容器环境变量定位挂载路径时开启引用 Secret/volatile 环境变量会被引擎明确拒绝。相对path会相对容器WorkingDir解析为绝对路径挂载本身是惰性、可写的Readonly: false真正落盘发生在容器执行阶段。文档版本提示0.20 参考文档 type-aliases/ContainerWithMountedFileOpts.md 只记录了expand与owner两个属性若你使用当前仓库的 SDK类型上还存在inheritOwner见 client.gen.ts#L1018-L1037建议以当前 SDK 的实际类型定义为准。完整的withMountedFile方法文档见 classes/Container.mdAPI 总览见 client.gen/README.md。【免费下载链接】daggerAutomation engine to build, test and ship any codebase. Runs locally, in CI, or directly in the cloud项目地址: https://gitcode.com/GitHub_Trending/da/dagger创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
分享:

看完干货,该让你的企业上线了

免费需求沟通 · 48 小时内出具建站方案 · 河南本地可上门