export.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. if runtime.GOOS == "windows" {
  14. return fmt.Errorf("the daemon on this platform does not support export of a container")
  15. }
  16. container, err := daemon.GetContainer(name)
  17. if err != nil {
  18. return err
  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. uidMaps, gidMaps := daemon.GetUIDGIDMaps()
  36. archive, err := archive.TarWithOptions(container.BaseFS, &archive.TarOptions{
  37. Compression: archive.Uncompressed,
  38. UIDMaps: uidMaps,
  39. GIDMaps: gidMaps,
  40. })
  41. if err != nil {
  42. daemon.Unmount(container)
  43. return nil, err
  44. }
  45. arch := ioutils.NewReadCloserWrapper(archive, func() error {
  46. err := archive.Close()
  47. daemon.Unmount(container)
  48. return err
  49. })
  50. daemon.LogContainerEvent(container, "export")
  51. return arch, err
  52. }