Skip to content

Commit 259e12d

Browse files
author
dharanpu
committed
enable copyFileToContainer feature during container startup
1 parent 3b974e9 commit 259e12d

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,15 @@ default void addFileSystemBind(final String hostPath, final String containerPath
128128
*/
129129
void addExposedPorts(int... ports);
130130

131+
/**
132+
* add a file to be copied before starting a created container
133+
*
134+
* @param mountableFile a Mountable file with path of source file / folder on host machine
135+
* @param containerPath a destination path on conatiner to which the files / folders to be copied
136+
* @return this
137+
*/
138+
void addCopyFileToContainer(MountableFile mountableFile, String containerPath);
139+
131140
/**
132141
* Specify the {@link WaitStrategy} to use to determine if the container is ready.
133142
*
@@ -175,6 +184,15 @@ default SELF withFileSystemBind(String hostPath, String containerPath) {
175184
*/
176185
SELF withExposedPorts(Integer... ports);
177186

187+
/**
188+
* Set the file to be copied before starting a created container
189+
*
190+
* @param mountableFile a Mountable file with path of source file / folder on host machine
191+
* @param containerPath a destination path on conatiner to which the files / folders to be copied
192+
* @return this
193+
*/
194+
SELF withCopyFileToContainer(MountableFile mountableFile, String containerPath);
195+
178196
/**
179197
* Add an environment variable to be passed to the container.
180198
*

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ default boolean isRunning() {
4343
}
4444
}
4545

46+
/**
47+
* @return is the container created?
48+
*/
49+
default boolean isCreated() {
50+
if (getContainerId() == null) {
51+
return false;
52+
}
53+
54+
try {
55+
Boolean created = getCurrentContainerInfo().getState().getStatus().equalsIgnoreCase("Created");
56+
return Boolean.TRUE.equals(created);
57+
} catch (DockerException e) {
58+
return false;
59+
}
60+
}
61+
4662
/**
4763
* @return has the container health state 'healthy'?
4864
*/

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ public class GenericContainer<SELF extends GenericContainer<SELF>>
138138
@Nullable
139139
private String workingDirectory = null;
140140

141+
private Map<MountableFile, String> copyToFileContainerPathMap = new HashMap<>();
142+
141143
/*
142144
* Unique instance of DockerClient for use by this container object.
143145
*/
@@ -231,6 +233,7 @@ private void tryStart(Profiler profiler) {
231233
applyConfiguration(createCommand);
232234

233235
containerId = createCommand.exec().getId();
236+
copyToFileContainerPathMap.forEach(this::copyFileToContainer);
234237

235238
logger().info("Starting container with ID: {}", containerId);
236239
profiler.start("Start container");
@@ -835,6 +838,21 @@ public SELF withWorkingDirectory(String workDir) {
835838
return self();
836839
}
837840

841+
842+
@Override
843+
public void addCopyFileToContainer(MountableFile mountableFile, String containerPath) {
844+
copyToFileContainerPathMap.put(mountableFile, containerPath);
845+
}
846+
847+
/**
848+
* {@inheritDoc}
849+
*/
850+
@Override
851+
public SELF withCopyFileToContainer(MountableFile mountableFile, String containerPath) {
852+
this.addCopyFileToContainer(mountableFile, containerPath);
853+
return self();
854+
}
855+
838856
/**
839857
* Get the IP address that this container may be reached on (may not be the local machine).
840858
*
@@ -940,8 +958,8 @@ public ExecResult execInContainer(String... command)
940958
@Override
941959
public void copyFileToContainer(MountableFile mountableLocalFile, String containerPath) {
942960

943-
if (!isRunning()) {
944-
throw new IllegalStateException("copyFileToContainer can only be used while the Container is running");
961+
if (!isRunning() && !isCreated()) {
962+
throw new IllegalStateException("opyFileToContainer can only be used with created / running container");
945963
}
946964

947965
this.dockerClient

0 commit comments

Comments
 (0)