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条)

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R