dongqiu8375 2019-03-22 13:41
浏览 403
已采纳

Golang Docker SDK映像构建因COPY而失败

So I am trying to build a docker image with the Golang SDK, everything runs except the section in the Dockerfile where I use COPY to copy a file across into the image:

COPY testfile.txt /testfile.txt

My code is as follows:

func buildImage() {
    // Run in directory where Dockerfile is found
    os.Chdir("build-dir")

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

    // Image Build requiresa tar file
    tar := new(archivex.TarFile)
    tar.Create("dockerfile.tar")
    tar.AddAll(".", true)
    tar.Close()

    // Use tar file as docker context
    dockerBuildContext, err := os.Open("dockerfile.tar")
    defer dockerBuildContext.Close()
    options := types.ImageBuildOptions{
        SuppressOutput: false,
        Remove:         true,
        ForceRemove:    true,
        PullParent:     true,
        Tags:           []string{"latest"},
        Dockerfile:     "Dockerfile",
    }
    buildResponse, err := cli.ImageBuild(context.Background(), dockerBuildContext, options)
    defer buildResponse.Body.Close()
    if err != nil {
        log.Fatal(err, " :unable to build docker image")
    }

    // Copy out response of stream
    _, err = io.Copy(os.Stdout, buildResponse.Body)


    if err != nil {
        log.Fatal(err, " :unable to read image build response")
    }
}

The code fails with:

{
    "errorDetail": {
        "message":"COPY failed: stat /var/lib/docker/tmp/docker-builder264844317/testfile.txt: no such file or directory"
    },
    "error":"COPY failed: stat /var/lib/docker/tmp/docker-builder264844317/testfile.txt: no such file or directory"
}

So far I have tried copying the files into the tar before building and then I have also tried moving the textfile.txt into the directory I run the command from but I still can not seem to get past this point

Extra information:

The file is in the same directory as the Dockerfile:

-- build-dir
   |-- Dockerfile
   |-- testfile.txt
  • 写回答

2条回答 默认 最新

  • doumao9363 2019-03-22 14:36
    关注

    From Source

    The docker build command builds Docker images from a Dockerfile and a “context”. A build’s context is the set of files located in the specified PATH or URL. The build process can refer to any of the files in the context. For example, your build can use a COPY instruction to reference a file in the context.

    Docker build-context is the entire directory you send to the docker engine. While building your image, Docker engine will try to find the files from the root of your build-context.

    In your case, the file was not added to the build-context.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联
  • ¥15 VB.NET操作免驱摄像头
  • ¥15 笔记本上移动热点开关状态查询
  • ¥85 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目
  • ¥15 利用pthon计算薄膜结构的光导纳