0

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.

bli00
  • 2,215
  • 2
  • 19
  • 46
  • I'm not seeing where you write a `binary_build` directory into the tar stream. You need to send the build context, including your `binary_build` directory, to the docker engine to run the build. – BMitch Jun 14 '19 at 00:30

1 Answers1

0

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/
charlie
  • 9
  • 3
  • I did that and it didn't work. The only thing that did work was `COPY . /usr/local/bin/`. After that I exec'd into this docker container and found only the `Dockerfiles` there. – bli00 Jun 14 '19 at 08:21