export.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package daemon // import "github.com/docker/docker/daemon"
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "github.com/docker/docker/container"
  7. "github.com/docker/docker/errdefs"
  8. "github.com/docker/docker/pkg/archive"
  9. "github.com/docker/docker/pkg/chrootarchive"
  10. )
  11. // ContainerExport writes the contents of the container to the given
  12. // writer. An error is returned if the container cannot be found.
  13. func (daemon *Daemon) ContainerExport(ctx context.Context, name string, out io.Writer) error {
  14. ctr, err := daemon.GetContainer(name)
  15. if err != nil {
  16. return err
  17. }
  18. if isWindows && ctr.OS == "windows" {
  19. return fmt.Errorf("the daemon on this operating system does not support exporting Windows containers")
  20. }
  21. if ctr.IsDead() {
  22. err := fmt.Errorf("You cannot export container %s which is Dead", ctr.ID)
  23. return errdefs.Conflict(err)
  24. }
  25. if ctr.IsRemovalInProgress() {
  26. err := fmt.Errorf("You cannot export container %s which is being removed", ctr.ID)
  27. return errdefs.Conflict(err)
  28. }
  29. err = daemon.containerExport(ctx, ctr, out)
  30. if err != nil {
  31. return fmt.Errorf("Error exporting container %s: %v", name, err)
  32. }
  33. return nil
  34. }
  35. func (daemon *Daemon) containerExport(ctx context.Context, container *container.Container, out io.Writer) error {
  36. err := daemon.imageService.PerformWithBaseFS(ctx, container, func(basefs string) error {
  37. archv, err := chrootarchive.Tar(basefs, &archive.TarOptions{
  38. Compression: archive.Uncompressed,
  39. IDMap: daemon.idMapping,
  40. }, basefs)
  41. if err != nil {
  42. return err
  43. }
  44. // Stream the entire contents of the container (basically a volatile snapshot)
  45. _, err = io.Copy(out, archv)
  46. return err
  47. })
  48. if err != nil {
  49. return err
  50. }
  51. daemon.LogContainerEvent(container, "export")
  52. return nil
  53. }