github.com/kobeld/docker@v1.12.0-rc1/daemon/export.go (about)

     1  package daemon
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  
     7  	"github.com/docker/docker/container"
     8  	"github.com/docker/docker/pkg/archive"
     9  	"github.com/docker/docker/pkg/ioutils"
    10  )
    11  
    12  // ContainerExport writes the contents of the container to the given
    13  // writer. An error is returned if the container cannot be found.
    14  func (daemon *Daemon) ContainerExport(name string, out io.Writer) error {
    15  	container, err := daemon.GetContainer(name)
    16  	if err != nil {
    17  		return err
    18  	}
    19  
    20  	data, err := daemon.containerExport(container)
    21  	if err != nil {
    22  		return fmt.Errorf("Error exporting container %s: %v", name, err)
    23  	}
    24  	defer data.Close()
    25  
    26  	// Stream the entire contents of the container (basically a volatile snapshot)
    27  	if _, err := io.Copy(out, data); err != nil {
    28  		return fmt.Errorf("Error exporting container %s: %v", name, err)
    29  	}
    30  	return nil
    31  }
    32  
    33  func (daemon *Daemon) containerExport(container *container.Container) (archive.Archive, error) {
    34  	if err := daemon.Mount(container); err != nil {
    35  		return nil, err
    36  	}
    37  
    38  	uidMaps, gidMaps := daemon.GetUIDGIDMaps()
    39  	archive, err := archive.TarWithOptions(container.BaseFS, &archive.TarOptions{
    40  		Compression: archive.Uncompressed,
    41  		UIDMaps:     uidMaps,
    42  		GIDMaps:     gidMaps,
    43  	})
    44  	if err != nil {
    45  		daemon.Unmount(container)
    46  		return nil, err
    47  	}
    48  	arch := ioutils.NewReadCloserWrapper(archive, func() error {
    49  		err := archive.Close()
    50  		daemon.Unmount(container)
    51  		return err
    52  	})
    53  	daemon.LogContainerEvent(container, "export")
    54  	return arch, err
    55  }