从Go代码构建Docker图像 [英] Build Docker Image From Go Code

查看:1371
本文介绍了从Go代码构建Docker图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用Docker API和Docker Go库构建Docker映像:
https:// github.com/docker/engine-api/



代码示例:

  package main 

import(
fmt

github.com/docker/engine-api/client
github.com/docker/engine-api/types
golang.org/x/net/context


func main(){
defaultHeaders:= map [string] string {User-Agent:engine-api-cli-1.0}
cli,err:= client.NewClient(unix:/// var / run / docker sock,v1.22,nil,defaultHeaders)
如果err!= nil {
panic(err)
}

fmt.Print(cli。 ClientVersion())

opt:= types.ImageBuildOptions {
CPUSetCPUs:2,
CPUSetMems:12,
CPUShares:20,
CPUQuota:10,
CPUPeriod:30,
内存:256,
内存交换:512,
ShmSize:10,
CgroupParent:cgroup_parent,
Dockerfile:dockerSrc / docker-debug-container / Dockerfile,
}
_ ,err = cli.ImageBuild(context.Background(),nil,opt)
如果err == nil || err.Error()!=来自守护程序的错误响应:服务器错误{
fmt.Printf(expected a Server Error,got%v,err)
}


  1. 文件夹存在于构建路径中

  2. 尝试相对和绝对路径

  3. 路径中没有软链接

  4. 尝试使用二进制和Dockerfile的相同文件夹

  5. docker build works

  6. 和一堆其他东西

错误总是相同的:

 守护程序的错误响应:找不到指定的Dockerfile:dockerSrc / docker-debug-container / Dockerfile 

 守护程序的错误响应:找不到指定的Dockerfile:Dockerfile 

其他选项是使用看起来像作品的RemoteContext,但这只适用于完全自包含的dockerfiles ...而不是具有本地文件存在的那些



更新:
尝试传递tar作为缓冲区,但是相同的东西:

  dockerBuildContext,err:= os.Open(< path to> / docker-调试容器/ docker-debug-container.tar)
defer dockerBuildContext.Close()

opt:= types.ImageBuildOptions {
上下文:dockerBuildContext,
CPUSetCPU:2,
CPUSetMems:12,
CPUShares:20,
CPUQuota:10,
CPUPeriod:30,
内存:256,
MemorySwap:512,
ShmSize:10,
CgroupParent:cgroup_parent,
// Dockerfile:Dockerfile,
}

_ ,err = cli.ImageBuild(context.Background(),nil,opt)


解决方案

@Mangirdas:盯着屏幕足够长的时间可以帮助 - 至少在我的情况下。现在我已经被困在同一个问题上了。
你是正确的使用tar文件(你的第二个例子)。如果您在这里查看API文档 https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/build-image-from-a-dockerfile ,您可以看到它期望一个tar。
真正帮助我的是在我的情况下查看客户端,perl和ruby的其他实现。当被要求从目录构建图像时,两者都会在飞行中创建一个tar。
无论如何,你只需要将dockerBuildContext放在别的地方(参见cli.ImageBuild())

  dockerBuildContext, err:= os.Open(/ Path / to / your / docker / tarfile.tar)
defer dockerBuildContext.Close()

buildOptions:= types.ImageBuildOptions {
Dockerfile:Dockerfile,//可选,是默认的
}

buildResponse,err:= cli.ImageBuild(context.Background(),dockerBuildContext,buildOptions)
如果err!= nil {
log.Fatal(err)
}
defer buildResponse.Body.Close()

我不在那里正确命名图像,但至少我可以创建他们...
希望这有帮助。
干杯


Trying to Build Docker image using Docker API and Docker Go libraries: https://github.com/docker/engine-api/

Code example:

package main

import (
    "fmt"

    "github.com/docker/engine-api/client"
    "github.com/docker/engine-api/types"
    "golang.org/x/net/context"
)

func main() {
    defaultHeaders := map[string]string{"User-Agent": "engine-api-cli-1.0"}
    cli, err := client.NewClient("unix:///var/run/docker.sock", "v1.22", nil, defaultHeaders)
    if err != nil {
        panic(err)
    }

    fmt.Print(cli.ClientVersion())

    opt := types.ImageBuildOptions{
        CPUSetCPUs:   "2",
        CPUSetMems:   "12",
        CPUShares:    20,
        CPUQuota:     10,
        CPUPeriod:    30,
        Memory:       256,
        MemorySwap:   512,
        ShmSize:      10,
        CgroupParent: "cgroup_parent",
        Dockerfile:   "dockerSrc/docker-debug-container/Dockerfile",
    }
    _, err = cli.ImageBuild(context.Background(), nil, opt)
    if err == nil || err.Error() != "Error response from daemon: Server error" {
        fmt.Printf("expected a Server Error, got %v", err)
    }

  1. Folder exist in build path
  2. Tried relative and absolute path
  3. No softlinks in the path
  4. tried same folder for binary and Dockerfile
  5. docker build works
  6. and bunch of other stuff

Error is always same:

Error response from daemon: Cannot locate specified Dockerfile: dockerSrc/docker-debug-container/Dockerfile 

or

Error response from daemon: Cannot locate specified Dockerfile: Dockerfile

Other option was to use RemoteContext which looks like works, but this is fine only for fully self contained dockerfiles... and not the ones with "local file presence"

Update: Tried passing tar as buffer, but same stuff:

        dockerBuildContext, err := os.Open("<path to>/docker-debug-    container/docker-debug-container.tar")
        defer dockerBuildContext.Close()

    opt := types.ImageBuildOptions{
        Context:      dockerBuildContext,
        CPUSetCPUs:   "2",
        CPUSetMems:   "12",
        CPUShares:    20,
        CPUQuota:     10,
        CPUPeriod:    30,
        Memory:       256,
        MemorySwap:   512,
        ShmSize:      10,
        CgroupParent: "cgroup_parent",
        //  Dockerfile:   "Dockerfile",
    }

    _, err = cli.ImageBuild(context.Background(), nil, opt)

解决方案

@Mangirdas: staring at a screen long enough DOES help - at least in my case. I have been stuck with the same issue for some time now. You were right to use the tar file (your second example). If you look at the API doc here https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/build-image-from-a-dockerfile you can see that it expects a tar. What really helped me was looking at other implementations of the client, perl and ruby in my case. Both create a tar on the fly when being asked to build an image from a directory. Anyway, you only need to put your dockerBuildContext somewhere else (see the cli.ImageBuild())

dockerBuildContext, err := os.Open("/Path/to/your/docker/tarfile.tar")
defer dockerBuildContext.Close()

buildOptions := types.ImageBuildOptions{
    Dockerfile:   "Dockerfile", // optional, is the default
}

buildResponse, err := cli.ImageBuild(context.Background(), dockerBuildContext, buildOptions)
if err != nil {
    log.Fatal(err)
}
defer buildResponse.Body.Close()

I am not there with naming the images properly yet, but at least I can create them... Hope this helps. Cheers

这篇关于从Go代码构建Docker图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆