我正在尝试使用Golang Docker SDK构建docker映像,但是它失败并显示{"message": "COPY failed: no source files were specified"}," error": "COPY failed: no source files were specified"}
这是我的代码
package main
import (
"archive/tar"
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"os"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)
func buildImage(client *client.Client, tags []string, dockerfile string) error {
ctx := context.Background()
// Create a buffer
buf := new(bytes.Buffer)
tw := tar.NewWriter(buf)
defer tw.Close()
// Create a filereader
dockerFileReader, err := os.Open(dockerfile)
if err != nil {
return err
}
// Read the actual Dockerfile
readDockerFile, err := ioutil.ReadAll(dockerFileReader)
if err != nil {
return err
}
// Make a TAR header for the file
tarHeader := &tar.Header{
Name: dockerfile,
Size: int64(len(readDockerFile)),
}
// Writes the header described for the TAR file
err = tw.WriteHeader(tarHeader)
if err != nil {
return err
}
// Writes the dockerfile data to the TAR file
_, err = tw.Write(readDockerFile)
if err != nil {
return err
}
dockerFileTarReader := bytes.NewReader(buf.Bytes())
buildOptions := types.ImageBuildOptions{
Context: dockerFileTarReader,
Dockerfile: dockerfile,
Remove: true,
Tags: tags,
}
// Build the actual image
imageBuildResponse, err := client.ImageBuild(
ctx,
dockerFileTarReader,
buildOptions,
)
if err != nil {
return err
}
// Read the STDOUT from the build process
defer imageBuildResponse.Body.Close()
_, err = io.Copy(os.Stdout, imageBuildResponse.Body)
if err != nil {
return err
}
return nil
}
这是我的文件夹结构,尝试使用src的Dockerfile构建docker映像。
我想考虑docker构建中src
内的所有文件。
-- root
|-- src
|--Dockerfile
|--files
|-- go-code
|--build.go
Dockerfile
FROM node:12
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD [ "node", "index.js" ]
详细的错误消息:
{"errorDetail":{"message":"COPY failed: no source files were specified"},"error":"COPY failed: no source files were specified"}
答案 0 :(得分:0)
这取决于您从何处调用docker构建。 例如。
-- root
|-- src
|--Dockerfile
|--files
|-- go-code
|--build.go
如果要复制/root/src/files/
中的文件,则需要cd到/root/src
文件夹中。
在这种情况下,docker命令将为:
docker built -t <tagname> .
在您的情况下,第一个“”。 COPY命令中的,将尝试从build.go运行所在的目录中进行复制。
这可能是您的脚本失败的原因。