MCPcopy Index your code
hub / github.com/ddev/ddev / CopyFromContainer

Function CopyFromContainer

pkg/dockerutil/containers.go:1082–1135  ·  view source on GitHub ↗

CopyFromContainer copies a path from a specified container and location to a dstPath on host

(containerName string, containerPath string, hostPath string)

Source from the content-addressed store, hash-verified

1080
1081// CopyFromContainer copies a path from a specified container and location to a dstPath on host
1082func CopyFromContainer(containerName string, containerPath string, hostPath string) error {
1083 startTime := time.Now()
1084 err := os.MkdirAll(hostPath, 0755)
1085 if err != nil {
1086 return err
1087 }
1088
1089 ctx, apiClient, err := GetDockerClient()
1090 if err != nil {
1091 return err
1092 }
1093 cid, err := FindContainerByName(containerName)
1094 if err != nil {
1095 return err
1096 }
1097 if cid == nil {
1098 return fmt.Errorf("copyFromContainer unable to find a container named %s", containerName)
1099 }
1100
1101 f, err := os.CreateTemp("", filepath.Base(hostPath)+".tar.gz")
1102 if err != nil {
1103 return err
1104 }
1105 //nolint: errcheck
1106 defer f.Close()
1107 //nolint: errcheck
1108 defer os.Remove(f.Name())
1109 // nolint: errcheck
1110
1111 reader, err := apiClient.CopyFromContainer(ctx, cid.ID, client.CopyFromContainerOptions{SourcePath: containerPath})
1112 if err != nil {
1113 return err
1114 }
1115
1116 defer reader.Content.Close()
1117
1118 _, err = io.Copy(f, reader.Content)
1119 if err != nil {
1120 return err
1121 }
1122
1123 err = f.Close()
1124 if err != nil {
1125 return err
1126 }
1127
1128 err = archive.Untar(f.Name(), hostPath, "")
1129 if err != nil {
1130 return err
1131 }
1132 util.Success("Copied %s:%s to %s in %v", containerName, containerPath, hostPath, time.Since(startTime))
1133
1134 return nil
1135}
1136
1137// GetContainerNames takes an array of Container
1138// and returns an array of strings with container names.

Callers 2

SnapshotMethod · 0.92
TestCopyFromContainerFunction · 0.92

Calls 5

UntarFunction · 0.92
SuccessFunction · 0.92
GetDockerClientFunction · 0.85
FindContainerByNameFunction · 0.85
ErrorfMethod · 0.80

Tested by 1

TestCopyFromContainerFunction · 0.74