Copy a file or directory from the local filesystem to a Docker container. The copy operation is recursive for directories. Be extremely careful with trailing slashes in src_path and dest_path, the behavior of docker cp is also different depending on whether the destination exists.
(
container: DockerEnvironment,
src_path: str | Path,
dest_path: str | Path,
)
| 100 | excluded_path.unlink() |
| 101 | |
| 102 | # Ensure destination folder exists |
| 103 | assert_zero_exit_code(dest_container.execute(f"mkdir -p {Path(dest_path).parent}")) |
| 104 | |
| 105 | # Copy from temporary local directory to destination container |
| 106 | cmd_dest = [ |
| 107 | "docker", |
| 108 | "cp", |
| 109 | str(temp_path), |
| 110 | f"{dest_container.container_id}:{dest_path}", |
| 111 | ] |
| 112 | result_dest = subprocess.run(cmd_dest, check=False, capture_output=True, text=True) |
| 113 | if result_dest.returncode != 0: |
| 114 | raise RuntimeError( |
| 115 | f"Failed to copy from local temp to {dest_container.container_id}: {result_dest.stdout}{result_dest.stderr}" |
| 116 | ) |
| 117 | |
| 118 | |
| 119 | def copy_to_container( |
| 120 | container: DockerEnvironment, |
| 121 | src_path: str | Path, |
| 122 | dest_path: str | Path, |
| 123 | ): |
| 124 | """ |
| 125 | Copy a file or directory from the local filesystem to a Docker container. |
| 126 | |
| 127 | The copy operation is recursive for directories. |
| 128 | |
| 129 | Be extremely careful with trailing slashes in src_path and dest_path, the behavior |
| 130 | of docker cp is also different depending on whether the destination exists. |
| 131 | """ |
| 132 | if not str(dest_path).startswith("/"): |
| 133 | # If not an absolute path, assume relative to container's cwd |
| 134 | dest_path = f"{container.config.cwd}/{dest_path}" |
| 135 | cmd = [ |
no test coverage detected