gpt4 book ai didi

docker - Golang Docker SDK 镜像构建失败并出现 COPY

转载 作者:IT王子 更新时间:2023-10-29 02:30:51 27 4
gpt4 key购买 nike

因此,我正在尝试使用 Golang SDK 构建一个 docker 镜像,除 Dockerfile 中我使用 COPY 将文件复制到镜像中的部分外,所有内容均运行:

COPY testfile.txt /testfile.txt

我的代码如下:

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")
}
}

代码失败:

{
"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"
}

到目前为止,我已经尝试在构建之前将文件复制到 tar 中,然后我还尝试将 textfile.txt 移动到我运行命令的目录中,但我似乎仍然无法获得过了这个点

额外信息:

该文件与 Dockerfile 位于同一目录中:

-- build-dir
|-- Dockerfile
|-- testfile.txt

最佳答案

来自 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 是您发送给 docker 引擎的整个目录。在构建镜像时,Docker 引擎将尝试从构建上下文的根目录中查找文件。

在您的情况下,文件未添加到构建上下文中。

关于docker - Golang Docker SDK 镜像构建失败并出现 COPY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55300932/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com