export.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package daemon
  2. import (
  3. "fmt"
  4. "io"
  5. "runtime"
  6. "github.com/docker/docker/container"
  7. "github.com/docker/docker/pkg/archive"
  8. "github.com/docker/docker/pkg/ioutils"
  9. )
  10. // ContainerExport writes the contents of the container to the given
  11. // writer. An error is returned if the container cannot be found.
  12. func (daemon *Daemon) ContainerExport(name string, out io.Writer) error {
  13. container, err := daemon.GetContainer(name)
  14. if err != nil {
  15. return err
  16. }
  17. if runtime.GOOS == "windows" && container.Platform == "windows" {
  18. return fmt.Errorf("the daemon on this platform does not support exporting Windows containers")
  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. // Stream the entire contents of the container (basically a volatile snapshot)
  26. if _, err := io.Copy(out, data); err != nil {
  27. return fmt.Errorf("Error exporting container %s: %v", name, err)
  28. }
  29. return nil
  30. }
  31. func (daemon *Daemon) containerExport(container *container.Container) (io.ReadCloser, error) {
  32. if err := daemon.Mount(container); err != nil {
  33. return nil, err
  34. }
  35. archive, err := archivePath(container.BaseFS, container.BaseFS.Path(), &archive.TarOptions{
  36. Compression: archive.Uncompressed,
  37. UIDMaps: daemon.idMappings.UIDs(),
  38. GIDMaps: daemon.idMappings.GIDs(),
  39. })
  40. if err != nil {
  41. daemon.Unmount(container)
  42. return nil, err
  43. }
  44. arch := ioutils.NewReadCloserWrapper(archive, func() error {
  45. err := archive.Close()
  46. daemon.Unmount(container)
  47. return err
  48. })
  49. daemon.LogContainerEvent(container, "export")
  50. return arch, err
  51. }