From dc3c55cf8e7366514018b4c065a40dc18917bd56 Mon Sep 17 00:00:00 2001 From: Chris Bednarski Date: Tue, 11 Aug 2015 22:22:52 -0700 Subject: [PATCH] Implemented downloader for the docker communicator so we can pull files out of a container --- builder/docker/communicator.go | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/builder/docker/communicator.go b/builder/docker/communicator.go index 4fcd9b658..31ccc2579 100644 --- a/builder/docker/communicator.go +++ b/builder/docker/communicator.go @@ -194,8 +194,36 @@ func (c *Communicator) UploadDir(dst string, src string, exclude []string) error return nil } +// Download pulls a file out of a container using `docker cp`. We have a source +// path and want to write to an io.Writer, not a file. We use - to make docker +// cp to write to stdout, and then copy the stream to our destination io.Writer. func (c *Communicator) Download(src string, dst io.Writer) error { - panic("not implemented") + + log.Printf("Downloading file from container: %s:%s", c.ContainerId, src) + localCmd := exec.Command("docker", "cp", fmt.Sprintf("%s:%s", c.ContainerId, src), "-") + + pipe, err := localCmd.StdoutPipe() + if err != nil { + return fmt.Errorf("Failed to open pipe: %s", err) + } + + err = localCmd.Start() + if err != nil { + return fmt.Errorf("Failed to start download: %s", err) + } + + numBytes, err := io.Copy(dst, pipe) + if err != nil { + return fmt.Errorf("Failed to pipe download: %s", err) + } else { + log.Printf("Copied %d bytes for %s", numBytes, src) + } + + if err = localCmd.Wait(); err != nil { + return fmt.Errorf("Failed to download '%s' from container: %s", src, err) + } + + return nil } // canExec tells us whether `docker exec` is supported