dpgbh20688 2018-01-26 22:00
浏览 87
已采纳

在GoLang Docker SDK中定义卷的安装点

I am currently trying to create a Docker container using the official GoLang Docker SDK and am trying to mount a volume from my localhost to the docker container.

vol := map[string]struct{}{"/pathInDocker":{}}

// This was prepared using example at:
//https://docs.docker.com/develop/sdk/examples/#run-a-container
res, err := cli.ContainerCreate(ctx, &container.Config{
    Image: testImageName,
    Volumes: vol,
    Cmd: []string{"ls", "/"},
}, nil, nil, "")

This works at creating the container and adding the "/pathInDocker" to the docker container, however, I cannot figure out how to add the mount point for the localhost.

I have tried the following possible values for the vol variable

vol := map[string]struct{}{"localPath:/pathInDocker":{}}
vol := map[string]struct{}{"\"localPath\":\"/pathInDocker\"":{}}

And for each of these the resulting docker container attempts to mount the map key as the folder in docker with not mount point for localhost.

I've dug through the docs and the only line I can seem to find talking about how to configure volumes at all is this one:

Volumes         map[string]struct{} // List of volumes (mounts) used for the container

So my question is how to configure this so it mounts a local folder to the volume?

  • 写回答

1条回答 默认 最新

  • dpt1712 2018-01-27 05:34
    关注

    If you want to use bind mounts, you need to provide your mount information in HostConfig.

    res, err := client.ContainerCreate(
        ctx,
        &container.Config{
            Image: "nginx",
            Cmd:   []string{"ls", "/"},
        },
        &container.HostConfig{
            Mounts: []mount.Mount{
                {
                    Type:   mount.TypeBind,
                    Source: "/localPath",
                    Target: "/pathInDocker",
                },
            },
        },
        nil,
        "",
    )
    

    And if you want to use volume, 1st you need to create a volume with mount path, then you need to use this volume name as Source.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog