So I am trying to build a docker image with the Golang SDK, everything runs except the section in the Dockerfile where I use COPY to copy a file across into the image:
COPY testfile.txt /testfile.txt
My code is as follows:
func buildImage() {
// Run in directory where Dockerfile is found
os.Chdir("build-dir")
cli, err := client.NewEnvClient()
if err != nil {log.Fatal(err, " :unable to init client")}
// Image Build requiresa tar file
tar := new(archivex.TarFile)
tar.Create("dockerfile.tar")
tar.AddAll(".", true)
tar.Close()
// Use tar file as docker context
dockerBuildContext, err := os.Open("dockerfile.tar")
defer dockerBuildContext.Close()
options := types.ImageBuildOptions{
SuppressOutput: false,
Remove: true,
ForceRemove: true,
PullParent: true,
Tags: []string{"latest"},
Dockerfile: "Dockerfile",
}
buildResponse, err := cli.ImageBuild(context.Background(), dockerBuildContext, options)
defer buildResponse.Body.Close()
if err != nil {
log.Fatal(err, " :unable to build docker image")
}
// Copy out response of stream
_, err = io.Copy(os.Stdout, buildResponse.Body)
if err != nil {
log.Fatal(err, " :unable to read image build response")
}
}
The code fails with:
{
"errorDetail": {
"message":"COPY failed: stat /var/lib/docker/tmp/docker-builder264844317/testfile.txt: no such file or directory"
},
"error":"COPY failed: stat /var/lib/docker/tmp/docker-builder264844317/testfile.txt: no such file or directory"
}
So far I have tried copying the files into the tar before building and then I have also tried moving the textfile.txt
into the directory I run the command from but I still can not seem to get past this point
Extra information:
The file is in the same directory as the Dockerfile:
-- build-dir
|-- Dockerfile
|-- testfile.txt