要在golang docker sdk中创建一个共享目录并将其设置为只读,可以使用container.HostConfig.Binds字段来指定要共享的目录,并使用container.HostConfig.ReadonlyPaths字段将其设置为只读。

以下是一个示例代码:

package main

import (
	"context"
	"fmt"

	"github.com/docker/docker/api/types"
	"github.com/docker/docker/client"
)

func main() {
	cli, err := client.NewClientWithOpts(client.FromEnv)
	if err != nil {
		panic(err)
	}

	ctx := context.Background()

	createContainerOptions := types.ContainerCreateConfig{
		Config: &types.ContainerConfig{
			Image: "ubuntu",
		},
		HostConfig: &types.HostConfig{
			Binds: []string{
				"/path/on/host:/path/in/container",
			},
			ReadonlyPaths: []string{
				"/path/in/container",
			},
		},
	}

	resp, err := cli.ContainerCreate(ctx, &createContainerOptions, nil, nil, "")
	if err != nil {
		panic(err)
	}

	fmt.Println(resp.ID)
}

在上面的示例中,/path/on/host是主机上的目录路径,/path/in/container是容器中的目录路径。通过将这两个路径绑定在一起,可以在容器中访问主机上的目录。通过将/path/in/container添加到ReadonlyPaths中,可以将该目录设置为只读。

请确保在运行此示例代码之前已经安装了Docker和golang docker sdk。

golang docker sdk中ContainerCreate方法创建共享目录并设置共享目录只读如何实现

原文地址: https://www.cveoy.top/t/topic/h40m 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录