def copy_files_container(container: docker.models.containers.Container, files: FILES_SRC_TARGET): """ Args: container: the container object. files: a list of (target path in container, source path in machine). """ if files: with tempfile.NamedTemporaryFile() as tar_file_path: with tarfile.open(name=tar_file_path.name, mode='w') as tar_file: for src, dst in files: try: tar_file.add(src, arcname=dst) except Exception as error: logger.debug(error) with open(tar_file_path.name, 'rb') as byte_file: container.put_archive('/', byte_file.read())
def put_file( container: docker.models.containers.Container, path: str, fname: str, name: str = None, mode: int = 0o640, ) -> None: """Put a single file into a container. Only works on single regular files and ignores all metadata. :param path: The directory in the container to extract to. :param fname: The filename of the local file. :param name: The name to store in the tar file; defaults to the basename of the file. :param mode: The mode for the stored file (3-digit octal number). """ with _SingleFileTar(fname, name, mode=mode) as f: container.put_archive(path, f)
def copy_host_to_container(container: docker.models.containers.Container, file: str, dest_path: str) -> None: """Copies a file or a folder from the host to the container. file may be a source folder or a file.""" if Path(file).is_dir(): archive = create_archive(file, arcname=Path(dest_path).name) container.put_archive(path=Path(dest_path).parent.as_posix(), data=archive) else: archive = create_archive(file) container.put_archive(path=dest_path, data=archive) archive.close() with create_archive(file) as archive: container.put_archive(path=dest_path, data=archive)