使用Golang API构建Docker映像时复制失败

使用Golang API构建Docker映像时复制失败

问题描述:

I'm trying to build a Docker Image using the official GO API, but I run into a problem on this line:

COPY packages /tmp/packages

of

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

Where packages is a folder in the directory where the docker file resides.

If I just build a docker image build to the path of the Dockerfile it builds as expected.

I tried adding a WORKDIR to the path where the Dockerfile is in the actual Dockerfile but it still seems like it can't find the folder.

Does anyone know what's going on here?

我正在尝试使用官方的GO API构建Docker映像,但在此行遇到了问题 : p>

  COPY包/ tmp / packages 
  code>  pre> 
 
 

of p>

  “ message”:“复制失败:stat / var / lib / docker / tmp / docker-builder107969114 / packages:无此类文件或目录” 
  code>  pre> 
 
 

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

如果我只是按预期方式将Docker镜像构建到Dockerfile的路径。 p>

我尝试将WORKDIR添加到其中的路径 Dockerfile位于实际的Dockerfile中,但似乎仍然找不到该文件夹​​。 p>

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

If anyone else encounters into this problem, if you have any mounts for your host config like:

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

That you built the image with the correct build context:

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

That within the tarFile that you're passing, .../somePath exists.

I struggled during one day to figure out how to build an image with the API, then I dived in the source code and found a neat solution which consist to used the tools provided by docker for the creation of archive, here is what I've found, it may help some people:

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

Thus the TarWithOptions return what we need an Io.Reader to pass to the buildContext. To go further and fit a special need, you can pass several argument as 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 }