mirror of
				https://github.com/moby/moby.git
				synced 2022-11-09 12:21:53 -05:00 
			
		
		
		
	Modified docker export to allow an --output flag
				
					
				
			Copied code from CmdSave into CmdExport. This should work, not an expert in the API calls being made. But it does make more sense to have a consistent export/save flag. Signed-off-by: Joseph Kern <joseph.a.kern@gmail.com> checkpoint before edits on the export functions Signed-off-by: Joseph Kern <joseph.a.kern@gmail.com> Added an --output flag to docker export and created tests. Signed-off-by: Joseph Kern <joseph.a.kern@gmail.com> White space cleanup. Signed-off-by: Joseph Kern <joseph.a.kern@gmail.com> Docker-DCO-1.1-Signed-off-by: Joseph Kern <jkern@semafour.net> (github: jfrazelle) checkpoint before edits on the export functions Signed-off-by: Joseph Kern <joseph.a.kern@gmail.com> White space cleanup. Signed-off-by: Joseph Kern <joseph.a.kern@gmail.com> Added text to reflect a new output option for the export command. Signed-off-by: Joseph Kern <joseph.a.kern@gmail.com> Whitespace clean up Signed-off-by: Joseph Kern <joseph.a.kern@gmail.com> Added man page documentation for the new --output flag in export Signed-off-by: Joseph Kern <joseph.a.kern@gmail.com>
This commit is contained in:
		
							parent
							
								
									f4f10cf43b
								
							
						
					
					
						commit
						5ff122f797
					
				
					 4 changed files with 100 additions and 10 deletions
				
			
		| 
						 | 
				
			
			@ -1872,14 +1872,40 @@ func (cli *DockerCli) CmdEvents(args ...string) error {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
func (cli *DockerCli) CmdExport(args ...string) error {
 | 
			
		||||
	cmd := cli.Subcmd("export", "CONTAINER", "Export the contents of a filesystem as a tar archive to STDOUT", true)
 | 
			
		||||
	cmd := cli.Subcmd("export", "CONTAINER", "Export a filesystem as a tar archive (streamed to STDOUT by default)", true)
 | 
			
		||||
	outfile := cmd.String([]string{"o", "-output"}, "", "Write to a file, instead of STDOUT")
 | 
			
		||||
	cmd.Require(flag.Exact, 1)
 | 
			
		||||
 | 
			
		||||
	utils.ParseFlags(cmd, args, true)
 | 
			
		||||
 | 
			
		||||
	if err := cli.stream("GET", "/containers/"+cmd.Arg(0)+"/export", nil, cli.out, nil); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	var (
 | 
			
		||||
		output io.Writer = cli.out
 | 
			
		||||
		err    error
 | 
			
		||||
	)
 | 
			
		||||
	if *outfile != "" {
 | 
			
		||||
		output, err = os.Create(*outfile)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	} else if cli.isTerminalOut {
 | 
			
		||||
		return errors.New("Cowardly refusing to save to a terminal. Use the -o flag or redirect.")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(cmd.Args()) == 1 {
 | 
			
		||||
		image := cmd.Arg(0)
 | 
			
		||||
		if err := cli.stream("GET", "/containers/"+image+"/export", nil, output, nil); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	} else {
 | 
			
		||||
		v := url.Values{}
 | 
			
		||||
		for _, arg := range cmd.Args() {
 | 
			
		||||
			v.Add("names", arg)
 | 
			
		||||
		}
 | 
			
		||||
		if err := cli.stream("GET", "/containers/get?"+v.Encode(), nil, output, nil); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -14,17 +14,24 @@ Export the contents of a container's filesystem using the full or shortened
 | 
			
		|||
container ID or container name. The output is exported to STDOUT and can be
 | 
			
		||||
redirected to a tar file.
 | 
			
		||||
 | 
			
		||||
Stream to a file instead of STDOUT by using **-o**.
 | 
			
		||||
 | 
			
		||||
# OPTIONS
 | 
			
		||||
**--help**
 | 
			
		||||
  Print usage statement
 | 
			
		||||
**-o**, **--output**=""
 | 
			
		||||
   Write to a file, instead of STDOUT
 | 
			
		||||
 | 
			
		||||
# EXAMPLES
 | 
			
		||||
Export the contents of the container called angry_bell to a tar file
 | 
			
		||||
called test.tar:
 | 
			
		||||
called angry_bell.tar:
 | 
			
		||||
 | 
			
		||||
    # docker export angry_bell > test.tar
 | 
			
		||||
    # ls *.tar
 | 
			
		||||
    test.tar
 | 
			
		||||
    # docker export angry_bell > angry_bell.tar
 | 
			
		||||
    # docker export --output=angry_bell-latest.tar angry_bell
 | 
			
		||||
    # ls -sh angry_bell.tar
 | 
			
		||||
    321M angry_bell.tar
 | 
			
		||||
    # ls -sh angry_bell-latest.tar
 | 
			
		||||
    321M angry_bell-latest.tar
 | 
			
		||||
 | 
			
		||||
# See also
 | 
			
		||||
**docker-import(1)** to create an empty filesystem image
 | 
			
		||||
| 
						 | 
				
			
			@ -34,3 +41,4 @@ and import the contents of the tarball into it, then optionally tag it.
 | 
			
		|||
April 2014, Originally compiled by William Henry (whenry at redhat dot com)
 | 
			
		||||
based on docker.com source material and internal work.
 | 
			
		||||
June 2014, updated by Sven Dowideit <SvenDowideit@home.org.au>
 | 
			
		||||
Janurary 2015, updated by Joseph Kern (josephakern at gmail dot com)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1066,14 +1066,22 @@ This will create a new Bash session in the container `ubuntu_bash`.
 | 
			
		|||
 | 
			
		||||
## export
 | 
			
		||||
 | 
			
		||||
    Usage: docker export CONTAINER
 | 
			
		||||
    Usage: docker export [OPTIONS] CONTAINER
 | 
			
		||||
 | 
			
		||||
    Export the contents of a filesystem as a tar archive to STDOUT
 | 
			
		||||
    Export the contents of a filesystem to a tar archive (streamed to STDOUT by default)
 | 
			
		||||
 | 
			
		||||
For example:
 | 
			
		||||
      -o, --output=""    Write to a file, instead of STDOUT
 | 
			
		||||
 | 
			
		||||
      Produces a tarred repository to the standard output stream.
 | 
			
		||||
 | 
			
		||||
   For example:
 | 
			
		||||
 | 
			
		||||
    $ sudo docker export red_panda > latest.tar
 | 
			
		||||
 | 
			
		||||
   Or
 | 
			
		||||
 | 
			
		||||
    $ sudo docker export --output="latest.tar" red_panda
 | 
			
		||||
 | 
			
		||||
> **Note:**
 | 
			
		||||
> `docker export` does not export the contents of volumes associated with the
 | 
			
		||||
> container. If a volume is mounted on top of an existing directory in the
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,8 @@
 | 
			
		|||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"os"
 | 
			
		||||
	"os/exec"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"testing"
 | 
			
		||||
| 
						 | 
				
			
			@ -47,3 +49,49 @@ func TestExportContainerAndImportImage(t *testing.T) {
 | 
			
		|||
	logDone("export - export a container")
 | 
			
		||||
	logDone("import - import an image")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Used to test output flag in the export command
 | 
			
		||||
func TestExportContainerWithOutputAndImportImage(t *testing.T) {
 | 
			
		||||
	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
 | 
			
		||||
	out, _, err := runCommandWithOutput(runCmd)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatal("failed to create a container", out, err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	cleanedContainerID := stripTrailingCharacters(out)
 | 
			
		||||
 | 
			
		||||
	inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
 | 
			
		||||
	out, _, err = runCommandWithOutput(inspectCmd)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatalf("output should've been a container id: %s %s ", cleanedContainerID, err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	exportCmdTemplate := `%v export --output=/tmp/testexp.tar %v`
 | 
			
		||||
	exportCmdFinal := fmt.Sprintf(exportCmdTemplate, dockerBinary, cleanedContainerID)
 | 
			
		||||
	exportCmd := exec.Command(exportCmdFinal)
 | 
			
		||||
	if out, _, err = runCommandWithOutput(exportCmd); err != nil {
 | 
			
		||||
		t.Fatalf("failed to export container: %s, %v", out, err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	importCmdFinal := `cat /tmp/testexp.tar | docker import - repo/testexp:v1`
 | 
			
		||||
	importCmd := exec.Command(importCmdFinal)
 | 
			
		||||
	out, _, err = runCommandWithOutput(importCmd)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatalf("failed to import image: %s, %v", out, err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	cleanedImageID := stripTrailingCharacters(out)
 | 
			
		||||
 | 
			
		||||
	inspectCmd = exec.Command(dockerBinary, "inspect", cleanedImageID)
 | 
			
		||||
	if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
 | 
			
		||||
		t.Fatalf("output should've been an image id: %s, %v", out, err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	deleteContainer(cleanedContainerID)
 | 
			
		||||
	deleteImages("repo/testexp:v1")
 | 
			
		||||
 | 
			
		||||
	os.Remove("/tmp/testexp.tar")
 | 
			
		||||
 | 
			
		||||
	logDone("export - export a container with output flag")
 | 
			
		||||
	logDone("import - import an image with output flag")
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue