0

我有以下代码从包含 Dockerfile 的 .tar 文件构建图像。

os.MkdirAll("configs/container",0755)
tar := new(archivex.TarFile)
tar.Create("configs/container/conf.tar")
tar.AddAll("configs/configdir", false)
tar.Close()

dockerBuildContext, err := os.Open("config/dns/conf.tar")
defer dockerBuildContext.Close()


buildOptions := 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", // optional, is the default
        Tags:   []string{"coolimage"},
    }

cli, err := client.NewEnvClient()

buildResponse, err := cli.ImageBuild(context.Background(), dockerBuildContext, buildOptions)
if err != nil {
        log.Printf("%s", err.Error())
}
if err != nil { 
    log.Fatal(err)
}
response, err := ioutil.ReadAll(buildResponse.Body) 
log.Printf(string(response))

defer buildResponse.Body.Close()

但是,这将返回以下错误:

2017/10/22 13:57:42 error during connect: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.34/build?buildargs=null&cachefrom=null&cgroupparent=cgroup_parent&cpuperiod=30&cpuquota=10&cpusetcpus=2&cpusetmems=12&cpushares=20&dockerfile=Dockerfile&labels=null&memory=256&memswap=512&networkmode=&rm=0&shmsize=10&t=dnsimage&target=&ulimits=null: invalid argument

我试图删除所有 BuildOptions,只留下“Dockerfile”选项,但仍然出现相同的错误。我总是得到无效的论点。

我尝试了关于这个问题的所有方法

我在 Mac 上运行 Docker,但我也尝试在运行 CentOS 的 EC2 实例中得到相同的结果。

Client:
Version:         1.12.6
API version:     1.24
Package version: docker-1.12.6-55.gitc4618fb.el7.centos.x86_64
Go version:      go1.8.3
Git commit:      c4618fb/1.12.6
Built:           Thu Sep 21 22:33:52 2017
OS/Arch:         linux/amd64

Server:
Version:         1.12.6
API version:     1.24
Package version: docker-1.12.6-55.gitc4618fb.el7.centos.x86_64
Go version:      go1.8.3
Git commit:      c4618fb/1.12.6
Built:           Thu Sep 21 22:33:52 2017
OS/Arch:         linux/amd64

docker.log 或 journalctl 上没有出现错误我也尝试使用 github.com/docker/docker/client 和 github.com/moby/moby/client

4

0 回答 0