{"record":{"id":"d3d2d4fe1c55eb84","repo":"testcontainers/testcontainers-java","slug":"copyfilefromcontainer-can-only-be-used-when-the-container-is","errorCode":null,"errorMessage":"copyFileFromContainer can only be used when the Container is created.","messagePattern":"copyFileFromContainer can only be used when the Container is created\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/org/testcontainers/containers/ContainerState.java","lineNumber":406,"sourceCode":"            inputStream -> {\n                try (FileOutputStream output = new FileOutputStream(destinationPath)) {\n                    IOUtils.copy(inputStream, output);\n                    return null;\n                }\n            }\n        );\n    }\n\n    /**\n     * Streams a file which resides inside the container\n     *\n     * @param containerPath path to file which is copied from container\n     * @param function function that takes InputStream of the copied file\n     */\n    @SneakyThrows\n    default <T> T copyFileFromContainer(String containerPath, ThrowingFunction<InputStream, T> function) {\n        if (getContainerId() == null) {\n            throw new IllegalStateException(\"copyFileFromContainer can only be used when the Container is created.\");\n        }\n\n        DockerClient dockerClient = getDockerClient();\n        try (\n            InputStream inputStream = dockerClient.copyArchiveFromContainerCmd(getContainerId(), containerPath).exec();\n            TarArchiveInputStream tarInputStream = new TarArchiveInputStream(inputStream)\n        ) {\n            tarInputStream.getNextTarEntry();\n            return function.apply(tarInputStream);\n        }\n    }\n}\n","sourceCodeStart":388,"sourceCodeEnd":419,"githubUrl":"https://github.com/testcontainers/testcontainers-java/blob/8e549514e3f01c57d70546fbb8599d138f3903e5/core/src/main/java/org/testcontainers/containers/ContainerState.java#L388-L419","documentation":"copyFileFromContainer reads a file out of the container via the Docker copy-archive API, which requires the container to exist. If getContainerId() is null it throws IllegalStateException — there is no container to copy from yet.","triggerScenarios":"Invoking copyFileFromContainer(path, fn) on a container that was never started (or never reached created state), e.g. reading logs/results files in a test before start().","commonSituations":"Reading an output file produced by the container before the container ran; calling copy on a fresh GenericContainer in a helper; failing start swallowed earlier so the ID was never set.","solutions":["Call start() and let the process complete before copying files out","Check isCreated()/isRunning() before copying, or guard with getContainerId() != null","Fix any earlier exception that prevented container startup","Use execInContainer or wait strategies to ensure the file exists after startup"],"exampleFix":"// before\nGenericContainer c = new GenericContainer<>(\"alpine:3\");\nString content = c.copyFileFromContainer(\"/tmp/out.txt\", is -> new String(is.readAllBytes()));\nc.start();\n// after\nc.start();\nString content = c.copyFileFromContainer(\"/tmp/out.txt\", is -> new String(is.readAllBytes()));","handlingStrategy":"validation","validationCode":"if (container.getContainerId() == null || !container.isRunning()) throw new IllegalStateException(\"container must be started before copyFileFromContainer\");","typeGuard":null,"tryCatchPattern":"try { return container.copyFileFromContainer(path, fn); } catch (IllegalStateException e) { if (e.getMessage().contains(\"copyFileFromContainer\")) { throw new IllegalStateException(\"Forgot to start container before reading \" + path, e); } throw e; }","preventionTips":["Copy files out only in test teardown after the workload ran","Check container.isRunning() before interacting","Fail fast on start() exceptions so later copy calls don't confuse the root cause"],"tags":["container-lifecycle","file-copy","container-state"],"backgroundTag":"invalid-state-transition","analyzedSha":"8e549514e3f01c57d70546fbb8599d138f3903e5","analyzedAt":"2026-09-12T14:56:41.227Z","contentChangedAt":"2026-09-12T14:56:41.227Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}