這要怎麼寫才不報錯 docker client api

986118933發表於2017-04-12
package main

import (
    "context"
    "fmt"
    "os"

    "github.com/docker/docker/api/types"
    "github.com/docker/docker/client"
)
func BuildImageFromDockerfile(dockerfilePath string, tag string) (reb types.ImageBuildResponse,err error) {
    defaultHeaders := map[string]string{"User-Agent": "engine-api-cli-1.0"}
    client,err:=client.NewClient("unix:///var/run/docker.sock","1.24",nil,defaultHeaders)
    //fmt.Println(client.ImageList(context.Background(),types.ImageListOptions{All:true}))
    if err!=nil {
        return
    }
    opt := types.ImageBuildOptions{
        Dockerfile:  dockerfilePath,
        Tags:        []string{tag},
        NoCache:     true,
        PullParent:true,
        SuppressOutput:true,
        ForceRemove: true,
    }
    dockerBuildContext, err := os.Open("/root/yuxing/tarfile.tar")
    defer dockerBuildContext.Close()
    reb,err=client.ImageBuild(context.Background(), dockerBuildContext, opt)
    return
}
func main() {
    ret,err := BuildImageFromDockerfile("/root/yuxing/Dockerfile","aaaa")
    fmt.Println(ret,err)
}

錯誤如下:

error during connect: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.24/build?buildargs=null&cachefrom=null&cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=%2Froot%2Fyuxi
ng%2FDockerfile&forcerm=1&labels=null&memory=0&memswap=0&networkmode=&nocache=1&pull=1&q=1&rm=0&shmsize=0&t=aaaa&ulimits=null: invalid argument

Dockerfile 內容:

FROM centos
MAINTAINER yuxing
RUN ln -s /server/nginx/sbin/nginx /bin
RUN ln -s /server/php/sbin/php-fpm /bin
RUN mkdir /www
ADD http://10.0.5.6/php7.1.3-bin.tar.gz /www
WORKDIR /www
RUN tar -xf /www/php7.1.3-bin.tar.gz
RUN mv -f /www/php7.1.3-bin/lib64/* /usr/lib64
RUN mv /www/php7.1.3-bin/server /
RUN mv /www/php7.1.3-bin/npstart /bin
RUN rm -f /www/php7.1.3-bin.tar.gz
RUN rm -rf /www/php7.1.3-bin
RUN chmod +x /bin/npstart
CMD ["npstart"]

執行獲取映象列表都是沒問題的

更多原創文章乾貨分享,請關注公眾號
  • 這要怎麼寫才不報錯 docker client api
  • 加微信實戰群請加微信(註明:實戰群):gocnio

相關文章