github.com/rawahars/moby@v24.0.4+incompatible/daemon/export.go (about)

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