简体   繁体   中英

docker build in Go client fail on COPY

I'm trying to build an image using docker's Go client. Here is the Go program I'm running:

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

    buf := new(bytes.Buffer)
    tw := tar.NewWriter(buf)
    defer tw.Close()

    dockerFile := "Dockerfile"
    dockerFileReader, err := os.Open("./Dockerfile")
    if err != nil {
        log.Fatal(err, " :unable to open Dockerfile")
    }
    readDockerFile, err := ioutil.ReadAll(dockerFileReader)
    if err != nil {
        log.Fatal(err, " :unable to read dockerfile")
    }

    tarHeader := &tar.Header{
        Name: dockerFile,
        Size: int64(len(readDockerFile)),
    }
    err = tw.WriteHeader(tarHeader)
    if err != nil {
        log.Fatal(err, " :unable to write tar header")
    }
    _, err = tw.Write(readDockerFile)
    if err != nil {
        log.Fatal(err, " :unable to write tar body")
    }
    dockerFileTarReader := bytes.NewReader(buf.Bytes())

    imageBuildResponse, err := cli.ImageBuild(
        ctx,
        dockerFileTarReader,
        types.ImageBuildOptions{
            Dockerfile: dockerFile,
            Tags:       []string{"devbuild"},
            Remove:     true})
    if err != nil {
        log.Fatal(err, " :unable to build docker image")
    }
    defer imageBuildResponse.Body.Close()
    _, err = io.Copy(os.Stdout, imageBuildResponse.Body)
    if err != nil {
        log.Fatal(err, " :unable to read image build response")
    }
}

It puts the Dockerfile (located in the current directory) in a tar file and builds an image using cli.ImageBuild . This solution was taken from this post , and my Dockerfile looks like this:

FROM alpine:latest

WORKDIR /gopath/src/build
COPY ./binary_build/ /usr/local/bin/

I'm constantly getting the error on the last step:

{"stream":"Step 3/3 : COPY /binary_build/ /usr/local/bin/"}
{"stream":"\n"}
{"errorDetail":{"message":"COPY failed: stat /var/lib/docker/tmp/docker-builder389608393/binary_build: no such file or directory"},"error":"COPY failed: stat /var/lib/docker/tmp/docker-builder389608393/binary_build: no such file or directory"}

There seems to be similar issue in the past reported here , but it seems to have been patched. I ran go get github.com/docker/docker@latest and my docker version in go.mod is github.com/docker/docker v1.13.1 . The issue still persists.

If you just build a docker image, that code means nothing, because the docker building process won't run the program. You should put your Dockerfile and executable file "binary_build" in a sole folder,may be the Dockerfile should like:

FROM alpine:latest
WORKDIR /gopath/src/build
COPY binary_build /usr/local/bin/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM