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