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

Function CopyIntoContainer

pkg/dockerutil/containers.go:1010–1079  ·  view source on GitHub ↗

CopyIntoContainer copies a path (file or directory) into a specified container and location

(srcPath string, containerName string, dstPath string, exclusion string)

Source from the content-addressed store, hash-verified

1008
1009// CopyIntoContainer copies a path (file or directory) into a specified container and location
1010func CopyIntoContainer(srcPath string, containerName string, dstPath string, exclusion string) error {
1011 startTime := time.Now()
1012 fi, err := os.Stat(srcPath)
1013 if err != nil {
1014 return err
1015 }
1016 // If a file has been passed in, we'll copy it into a temp directory
1017 if !fi.IsDir() {
1018 dirName, err := os.MkdirTemp("", "")
1019 if err != nil {
1020 return err
1021 }
1022 defer os.RemoveAll(dirName)
1023 err = fileutil.CopyFile(srcPath, filepath.Join(dirName, filepath.Base(srcPath)))
1024 if err != nil {
1025 return err
1026 }
1027 srcPath = dirName
1028 }
1029
1030 ctx, apiClient, err := GetDockerClient()
1031 if err != nil {
1032 return err
1033 }
1034 cid, err := FindContainerByName(containerName)
1035 if err != nil {
1036 return err
1037 }
1038 if cid == nil {
1039 return fmt.Errorf("copyIntoContainer unable to find a container named %s", containerName)
1040 }
1041
1042 uid, _, _ := GetContainerUser()
1043 _, stderr, err := Exec(cid.ID, "mkdir -p "+dstPath, uid)
1044 if err != nil {
1045 return fmt.Errorf("unable to mkdir -p %s inside %s: %v (stderr=%s)", dstPath, containerName, err, stderr)
1046 }
1047
1048 tarball, err := os.CreateTemp(os.TempDir(), "containercopytmp*.tar.gz")
1049 if err != nil {
1050 return err
1051 }
1052 err = tarball.Close()
1053 if err != nil {
1054 return err
1055 }
1056 // nolint: errcheck
1057 defer os.Remove(tarball.Name())
1058
1059 // Tar up the source directory into the tarball
1060 err = archive.Tar(srcPath, tarball.Name(), exclusion)
1061 if err != nil {
1062 return err
1063 }
1064 t, err := os.Open(tarball.Name())
1065 if err != nil {
1066 return err
1067 }

Callers 4

PushGlobalTraefikConfigFunction · 0.92
ImportDBMethod · 0.92
TestCopyIntoContainerFunction · 0.92
CopyIntoVolumeFunction · 0.85

Calls 8

CopyFileFunction · 0.92
TarFunction · 0.92
DebugFunction · 0.92
GetDockerClientFunction · 0.85
FindContainerByNameFunction · 0.85
GetContainerUserFunction · 0.85
ExecFunction · 0.85
ErrorfMethod · 0.80

Tested by 1

TestCopyIntoContainerFunction · 0.74