Docker go client: How to include files to copy within build context tarball
I am trying to build an image with the docker go client.
Everything works except that I am unable to copy my needed files to build the image.
The problem is that I somehow don’t get how to include these files in the build context.
This is my BuildImage() go function:
func BuildImage(filePath string, fileName string, imgTags []string, printSteps bool) {
fmt.Println("Building docker image: ", imgTags)
ctx, client := createClient()
buildResponse, err := client.ImageBuild(
ctx,
dockerFileTarReader("../path_with_files_to_copy/Dockerfile", fileName),
types.ImageBuildOptions{
Context: getContext("../path_with_files_to_copy"),
Dockerfile: fileName,
Tags: imgTags})
errors.Check(err)
responseHandler(buildResponse.Body, printSteps)
defer buildResponse.Body.Close()
}
This is my getContext() function:
func getContext(filePath string) io.Reader {
ctx, _ := archive.TarWithOptions(filePath, &archive.TarOptions{})
return ctx
}
This is my Dockerfile:
FROM golang:latest
WORKDIR /app
COPY file.go .
If I try to build with this setup I encounter the following error:
{"errorDetail":{"message":"COPY failed: stat /var/lib/docker/tmp/docker-builderXXXXXXX/file.go: no such file or directory"},"error":"COPY failed: stat /var/lib/docker/tmp/docker-builderXXXXXXX/file.go: no such file or directory"}
So how can I accomplish that the file.go that is located in "../path_with_files_to_copy" to be present at build time in the docker-builderXXXXXXX dir so docker can copy it into the container image?
Source: Docker Questions