我有以下代码从包含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”选项,但仍会出现相同的错误。我总是得到无效的论据。
我尝试了所有方法question
我在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