doutong1890 2019-06-14 22:08
浏览 129
已采纳

使用Docker Go客户端并行构建Docker映像

I'm using Docker's Go client to build my projects. This post highlights how to do that with the Go client. I'm calling ImageBuild on three of my Dockerfiles (1.Dockerfile, 2.Dockerfile, and 3.Dockerfile) as a test. Here is my code:

func GetContext(filePath string) io.Reader {
    // Use homedir.Expand to resolve paths like '~/repos/myrepo'
    filePath, _ = homedir.Expand(filePath)
    ctx, err := archive.TarWithOptions(filePath, &archive.TarOptions{})
    if err != nil {
        panic(err)
    }

    return ctx
}

func testImageBuild() {
    ctx := context.Background()
    cli, err := client.NewEnvClient()
    if err != nil {
        log.Fatal(err, " :unable to init client")
    }

    var wg sync.WaitGroup
    for i := 0; i < 3; i++ {
        wg.Add(1)
        go func(i int) {
            defer wg.Done()
            dockerFile := fmt.Sprintf("%d.Dockerfile", i)
            imageBuildResponse, err := cli.ImageBuild(
                ctx,
                GetContext("."),
                types.ImageBuildOptions{
                    Dockerfile: dockerFile,
                    Tags:       []string{fmt.Sprintf("devbuild_%d", i)},
                })
            if err != nil {
                log.Fatal(err, " :unable to build docker image"+string(1))
            }
            defer imageBuildResponse.Body.Close()
            _, err = io.Copy(os.Stdout, imageBuildResponse.Body)
            if err != nil {
                log.Fatal(err, " :unable to read image build response "+string(1))
            }
        }(i)
    }
    wg.Wait()
}

func main() {
    testImageBuild()
}

GetContext is used to tar the directory path as a context for Docker. testImageBuild spins off three different goroutines to build the three different images.

My question is: When I run this, the output to stdout is always the same and seems deterministic, which makes me think that the images aren't actually been built in parallel. I'm not familiar with how docker build its images, and it seems entirely possible that this approach is simply sending requests to docker server in parallel rather than actually building in parallel. Is this true? If so, how can I build my projects in parallel?

  • 写回答

1条回答 默认 最新

  • dongpu9852 2019-06-17 05:27
    关注

    If I understand your question correctly, you have a docker-machine on which you want to build the images concurrently using your GO program.

    I tried to do the same thing with Dockerfiles which have the same image being built, and per my understanding, all of them were build concurrently.

    Here is the go package that I used to replicate the scenario - https://github.com/nihanthd/stackoverflow/tree/master/docker

    Now in your case if you were using 3 different docker files, then certainly they would have different build times, that means the output would be seem to be deterministic

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

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵