Skip to content

Commit c4ae20f

Browse files
committed
Add checks that the container is running before copy to/from actions
1 parent 524b957 commit c4ae20f

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

core/src/main/java/org/testcontainers/containers/GenericContainer.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,11 @@ public ExecResult execInContainer(String... command)
865865
* {@inheritDoc}
866866
*/
867867
@Override
868-
public void copyFileToContainer(MountableFile mountableLocalFile, String containerPath) throws IOException, InterruptedException {
868+
public void copyFileToContainer(MountableFile mountableLocalFile, String containerPath) {
869+
870+
if (!isRunning()) {
871+
throw new IllegalStateException("copyFileToContainer can only be used while the Container is running");
872+
}
869873

870874
this.dockerClient
871875
.copyArchiveToContainerCmd(this.containerId)
@@ -878,7 +882,12 @@ public void copyFileToContainer(MountableFile mountableLocalFile, String contain
878882
* {@inheritDoc}
879883
*/
880884
@Override
881-
public void copyFileFromContainer(String containerPath, String destinationPath) throws IOException, InterruptedException {
885+
public void copyFileFromContainer(String containerPath, String destinationPath) throws IOException {
886+
887+
if (!isRunning()) {
888+
throw new IllegalStateException("copyFileToContainer can only be used while the Container is running");
889+
}
890+
882891
try (final TarArchiveInputStream tarInputStream = new TarArchiveInputStream(this.dockerClient
883892
.copyArchiveFromContainerCmd(this.containerId, containerPath)
884893
.exec())) {
@@ -902,7 +911,7 @@ public ExecResult execInContainer(Charset outputCharset, String... command)
902911
}
903912

904913
if (!isRunning()) {
905-
throw new IllegalStateException("Container is not running so exec cannot be run");
914+
throw new IllegalStateException("execInContainer can only be used while the Container is running");
906915
}
907916

908917
this.dockerClient

0 commit comments

Comments
 (0)