export.go 1.8 KB

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