You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Rust Testcontainers lets you seed container filesystems before startup, collect artifacts produced inside containers, and bind host paths at runtime. The APIs deliver smooth ergonomics while staying idiomatic to Rust.
4
+
5
+
## Copying Files Into Containers (Before Startup)
6
+
7
+
Use `ImageExt::with_copy_to` to stage files or directories before the container starts. Content can come from raw bytes or host paths:
8
+
9
+
```rust
10
+
// Example: copying inline bytes and directories into a container
Everything is packed into a TAR archive, preserving nested directories. The helper accepts either `Vec<u8>` or any path-like value implementing `CopyDataSource`.
21
+
By default, the destination path inherits the mode of the source file on Unix hosts (or falls back to `0o644` elsewhere). Use `CopyTargetOptions` when you need to override per-copy metadata such as permissions:
`CopyTargetOptions::new` wraps any path-like target and keeps backward compatibility with string literals—existing code continues to compile. Symbolic links still follow Docker’s TAR semantics; the `mode` override only applies to the final file entry recorded in the archive.
38
+
39
+
## Copying Files From Containers (After Execution)
40
+
41
+
Use `copy_file_from` to pull data produced inside the container:
42
+
43
+
```rust
44
+
// Example: copying a file from a running container to the host
-`copy_file_from` streams file contents into any destination implementing `CopyFileFromContainer` (for example `&Path` or `&mut Vec<u8>`). When the requested path is not a regular file you’ll receive a `CopyFromContainerError`.
66
+
- Targets like `Vec<u8>` and filesystem paths overwrite existing data: vectors are cleared before writing, and files are truncated or recreated if they already exist.
Bind mounts share host state directly. Tmpfs mounts create ephemeral in-memory storage useful for scratch data or caches.
95
+
96
+
## Selecting an Approach
97
+
98
+
-**Copy before startup** — for deterministic inputs.
99
+
-**Copy from containers** — to capture build artifacts, logs, or test fixtures produced during a run.
100
+
-**Use mounts** — when containers need to read/write large amounts of data efficiently without re-tarring.
101
+
102
+
Mixing these tools keeps tests hermetic (isolated and reproducible) while letting you inspect outputs locally.
103
+
Document each choice in code so teammates know whether data is ephemeral (`tmpfs`), seeded once (`with_copy_to`), or captured for later assertions (`copy_file_from`).
0 commit comments