【问题标题】:Copy failed when building a docker image using Golang api使用 Golang api 构建 docker 映像时复制失败
【发布时间】:2018-05-17 14:22:58
【问题描述】:

我正在尝试使用官方 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 中的路径,但它似乎仍然找不到该文件夹​​。

有人知道这里发生了什么吗?

【问题讨论】:

  • 你能添加你的 docker build 命令吗?
  • 重要的是指向构建上下文,您可以在构建命令中更改它,docker build -t my-image . 点指向您正在运行命令的位置

标签: docker go dockerfile


【解决方案1】:

如果其他人遇到此问题,如果您的主机配置有任何挂载,例如:

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

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

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

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

【讨论】:

    【解决方案2】:

    我花了一天的时间来弄清楚如何使用 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 }

    【讨论】:

      猜你喜欢
      • 2018-06-02
      • 2022-09-28
      • 2019-12-02
      • 1970-01-01
      • 2021-08-16
      • 1970-01-01
      • 1970-01-01
      • 2022-12-23
      • 2021-12-29
      相关资源
      最近更新 更多