testcontainers/testcontainers-java · warning
folder-like containerPath in copyFileToContainer is…
Error message
folder-like containerPath in copyFileToContainer is deprecated, please explicitly specify a file path
What it means
ContainerState.copyFileToContainer warns when a file is copied to a container path ending in '/' (a folder-like path). Historically this appended the file name implicitly; now that behavior is deprecated and users should pass the explicit destination file path. The copy still succeeds with the file name appended.
Solutions
- Append the file name to the containerPath explicitly instead of relying on the trailing slash
- Drop the trailing slash and give the full destination file path
Example fix
// before
container.copyFileToContainer(MountableFile.forHostPath("app.properties"), "/etc/app/");
// after
container.copyFileToContainer(MountableFile.forHostPath("app.properties"), "/etc/app/app.properties"); Defensive patterns
Strategy: validation
Validate before calling
void copyFile(GenericContainer<?> c, MountableFile f, String containerPath) {
if (containerPath.endsWith("/")) {
String fileName = new File(f.getResolvedPath()).getName();
containerPath = containerPath + fileName;
}
c.copyFileToContainer(f, containerPath);
} Prevention
- Never end copyFileToContainer paths with '/' for files
- Always pass the full destination file path including the file name
- Search the codebase for copyFileToContainer calls with trailing slashes
When it happens
Trigger: Calling copyFileToContainer(mountableFile, "/some/dir/") where mountableFile resolves to a regular file and the containerPath ends with a slash.
Common situations: Copy-pasted path constants meant for directory mounting being reused for single-file copies; transitioning code from folder mounts to file copies.
Understand the failure class
Background: "is deprecated and will be removed" — deprecation warnings for old API names, keywords, and options, and how to migrate before the removal release — this error's family across 29 libraries.
Related errors
- This container's image does not have a healthcheck…
- Requested port ( ) is not mapped
- copyFileToContainer can only be used with created / running…
- copyFileFromContainer can only be used when the Container…
- TIMEOUT_ERROR
AI-assisted analysis of testcontainers/testcontainers-java@8e549514e3 (2026-09-12).
Data as JSON: /api/errors/d846004a326d3ca5.
Report an issue: GitHub.
Appendix: source
Thrown at core/src/main/java/org/testcontainers/containers/ContainerState.java:326
*/
default Container.ExecResult execInContainer(Charset outputCharset, ExecConfig execConfig)
throws UnsupportedOperationException, IOException, InterruptedException {
return ExecInContainerPattern.execInContainer(getDockerClient(), getContainerInfo(), outputCharset, execConfig);
}
/**
*
* Copies a file or directory to the container.
*
* @param mountableFile file or directory which is copied into the container
* @param containerPath destination path inside the container
*/
default void copyFileToContainer(MountableFile mountableFile, String containerPath) {
File sourceFile = new File(mountableFile.getResolvedPath());
if (containerPath.endsWith("/") && sourceFile.isFile()) {
final Logger logger = LoggerFactory.getLogger(GenericContainer.class);
logger.warn(
"folder-like containerPath in copyFileToContainer is deprecated, please explicitly specify a file path"
);
copyFileToContainer((Transferable) mountableFile, containerPath + sourceFile.getName());
} else {
copyFileToContainer((Transferable) mountableFile, containerPath);
}
}
/**
*
* Copies a file to the container.
*
* @param transferable file which is copied into the container
* @param containerPath destination path inside the container
*/
@SneakyThrows({ IOException.class, InterruptedException.class })
default void copyFileToContainer(Transferable transferable, String containerPath) {
if (getContainerId() == null) {View on GitHub (pinned to 8e549514e3)