使用Golang api构建docker镜像时复制失败

时间:2018-05-17 14:22:59

标签: docker go dockerfile

我正在尝试使用官方GO API构建Docker镜像,但我在这一行遇到了问题:

COPY packages /tmp/packages

"message":"COPY failed: stat /var/lib/docker/tmp/docker-builder107969114/packages: no such file or directory"

其中packages是docker文件所在目录中的文件夹。

如果我只是构建一个Docker镜像构建到Dockerfile的路径,它会按预期构建。

我尝试将一个WORKDIR添加到Dockerfile在实际Dockerfile中的路径,但它似乎仍然无法找到该文件夹​​。

有谁知道这里发生了什么?

2 个答案:

答案 0 :(得分:0)

如果有其他人遇到此问题,请为您的主机配置进行任何安装,例如:

&container.HostConfig{
    Mounts:[]mount.Mount{
        {
            Source: .../somePath,
            Target: .../somePath,
        }
    }
}

您使用正确的构建上下文构建了图像:

cli.ImageBuild(context.Background(), tarFile, types.ImageBuildOptions{})

在你传递的tarFile中,... / somePath存在。

答案 1 :(得分:0)

我花了一天时间努力弄清楚如何使用API​​构建映像,然后潜入源代码,找到了一个整洁的解决方案,该解决方案包括使用docker提供的工具来创建档案,这就是我发现,它可能对某些人有帮助:

import "github.com/docker/docker/pkg/archive" reader, err := archive.TarWithOptions("path/to/the/project/where/the/dockefile/reside", &archive.TarOptions{})

因此,TarWithOptions返回我们需要Io.Reader传递给buildContext的内容。 为了进一步满足特殊需求,您可以将几个参数传递为TarOptions{}

TarOptions struct { IncludeFiles []string ExcludePatterns []string Compression Compression NoLchown bool UIDMaps []idtools.IDMap GIDMaps []idtools.IDMap ChownOpts *idtools.Identity IncludeSourceDir bool // WhiteoutFormat is the expected on disk format for whiteout files. // This format will be converted to the standard format on pack // and from the standard format on unpack. WhiteoutFormat WhiteoutFormat // When unpacking, specifies whether overwriting a directory with a // non-directory is allowed and vice versa. NoOverwriteDirNonDir bool // For each include when creating an archive, the included name will be // replaced with the matching name from this map. RebaseNames map[string]string InUserNS bool }

相关问题