Add support for 'docker cp' to write to stdout

Closes #10805

Signed-off-by: Doug Davis <dug@us.ibm.com>
This commit is contained in:
Doug Davis 2015-03-05 08:39:32 -08:00
parent 5035fa1e21
commit f3d96e81e9
5 changed files with 50 additions and 11 deletions

View File

@ -2415,7 +2415,7 @@ func (cli *DockerCli) CmdRun(args ...string) error {
} }
func (cli *DockerCli) CmdCp(args ...string) error { func (cli *DockerCli) CmdCp(args ...string) error {
cmd := cli.Subcmd("cp", "CONTAINER:PATH HOSTPATH", "Copy files/folders from the PATH to the HOSTPATH", true) cmd := cli.Subcmd("cp", "CONTAINER:PATH HOSTPATH|-", "Copy files/folders from the PATH to the HOSTPATH. Use '-' to write the data\nas a tar file to STDOUT.", true)
cmd.Require(flag.Exact, 2) cmd.Require(flag.Exact, 2)
utils.ParseFlags(cmd, args, true) utils.ParseFlags(cmd, args, true)
@ -2442,7 +2442,14 @@ func (cli *DockerCli) CmdCp(args ...string) error {
} }
if statusCode == 200 { if statusCode == 200 {
if err := archive.Untar(stream, copyData.Get("HostPath"), &archive.TarOptions{NoLchown: true}); err != nil { dest := copyData.Get("HostPath")
if dest == "-" {
_, err = io.Copy(cli.out, stream)
} else {
err = archive.Untar(stream, dest, &archive.TarOptions{NoLchown: true})
}
if err != nil {
return err return err
} }
} }

View File

@ -2,16 +2,17 @@
% Docker Community % Docker Community
% JUNE 2014 % JUNE 2014
# NAME # NAME
docker-cp - Copy files/folders from the PATH to the HOSTPATH docker-cp - Copy files/folders from the PATH to the HOSTPATH, or STDOUT
# SYNOPSIS # SYNOPSIS
**docker cp** **docker cp**
[**--help**] [**--help**]
CONTAINER:PATH HOSTPATH CONTAINER:PATH HOSTPATH|-
# DESCRIPTION # DESCRIPTION
Copy files/folders from a container's filesystem to the host Copy files/folders from a container's filesystem to the
path. Paths are relative to the root of the filesystem. Files path. Use '-' to write the data as a tar file to STDOUT.
Paths are relative to the root of the filesystem. Files
can be copied from a running or stopped container. can be copied from a running or stopped container.
# OPTIONS # OPTIONS

View File

@ -117,7 +117,7 @@ unix://[/path/to/socket] to use.
Create a new image from a container's changes Create a new image from a container's changes
**docker-cp(1)** **docker-cp(1)**
Copy files/folders from a container's filesystem to the host at path Copy files/folders from a container's filesystem to the host
**docker-create(1)** **docker-create(1)**
Create a new container Create a new container

View File

@ -757,12 +757,15 @@ Supported `Dockerfile` instructions: `CMD`, `ENTRYPOINT`, `ENV`, `EXPOSE`,
## cp ## cp
Copy files/folders from a container's filesystem to the host Copy files/folders from a container's filesystem to the
path. Paths are relative to the root of the filesystem. path. Use '-' to write the data as a tar file to STDOUT.
Paths are relative to the root of the filesystem.
Usage: docker cp CONTAINER:PATH HOSTPATH Usage: docker cp CONTAINER:PATH HOSTPATH|-
Copy files/folders from the PATH to the HOSTPATH. Use '-' to write the data
as a tar file to STDOUT.
Copy files/folders from the PATH to the HOSTPATH
## create ## create

View File

@ -8,6 +8,7 @@ import (
"os/exec" "os/exec"
"path" "path"
"path/filepath" "path/filepath"
"strings"
"testing" "testing"
) )
@ -528,3 +529,30 @@ func TestCpToDot(t *testing.T) {
} }
logDone("cp - to dot path") logDone("cp - to dot path")
} }
func TestCpToStdout(t *testing.T) {
out, exitCode, err := dockerCmd(t, "run", "-d", "busybox", "/bin/sh", "-c", "echo lololol > /test")
if err != nil || exitCode != 0 {
t.Fatalf("failed to create a container:%s\n%s", out, err)
}
cID := stripTrailingCharacters(out)
defer deleteContainer(cID)
out, _, err = dockerCmd(t, "wait", cID)
if err != nil || stripTrailingCharacters(out) != "0" {
t.Fatalf("failed to set up container:%s\n%s", out, err)
}
out, _, err = runCommandPipelineWithOutput(
exec.Command(dockerBinary, "cp", cID+":/test", "-"),
exec.Command("tar", "-vtf", "-"))
if err != nil {
t.Fatalf("Failed to run commands: %s", err)
}
if !strings.Contains(out, "test") || !strings.Contains(out, "-rw") {
t.Fatalf("Missing file from tar TOC:\n%s", out)
}
logDone("cp - to stdout")
}