Skip to content

Commit 58a6c69

Browse files
fzakariakiview
authored andcommitted
Allow to set Shared Memory size (shmSize) for GenericContainer (#957)
1 parent cd35993 commit 58a6c69

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.github.dockerjava.api.model.Bind;
77
import com.github.dockerjava.api.model.ContainerNetwork;
88
import com.github.dockerjava.api.model.ExposedPort;
9+
import com.github.dockerjava.api.model.HostConfig;
910
import com.github.dockerjava.api.model.Info;
1011
import com.github.dockerjava.api.model.Link;
1112
import com.github.dockerjava.api.model.PortBinding;
@@ -153,6 +154,13 @@ public class GenericContainer<SELF extends GenericContainer<SELF>>
153154
@Nullable
154155
private String workingDirectory = null;
155156

157+
/**
158+
* The shared memory size to use when starting the container.
159+
* This value is in bytes.
160+
*/
161+
@Nullable
162+
private Long shmSize;
163+
156164
private Map<MountableFile, String> copyToFileContainerPathMap = new HashMap<>();
157165

158166
/*
@@ -318,6 +326,17 @@ private void tryStart(Profiler profiler) {
318326
}
319327
}
320328

329+
/**
330+
* Set any custom settings for the create command such as shared memory size.
331+
*/
332+
private HostConfig buildHostConfig() {
333+
HostConfig config = new HostConfig();
334+
if (shmSize != null) {
335+
config.withShmSize(shmSize);
336+
}
337+
return config;
338+
}
339+
321340
private void connectToPortForwardingNetwork(String networkMode) {
322341
PortForwardingContainer.INSTANCE.getNetwork().map(ContainerNetwork::getNetworkID).ifPresent(networkId -> {
323342
if (!Arrays.asList(networkId, "none", "host").contains(networkMode)) {
@@ -434,7 +453,9 @@ public Set<Integer> getLivenessCheckPortNumbers() {
434453
}
435454

436455
private void applyConfiguration(CreateContainerCmd createCommand) {
437-
456+
HostConfig hostConfig = buildHostConfig();
457+
createCommand.withHostConfig(hostConfig);
458+
438459
// Set up exposed ports (where there are no host port bindings defined)
439460
ExposedPort[] portArray = exposedPorts.stream()
440461
.map(ExposedPort::new)
@@ -1157,6 +1178,16 @@ public SELF withCreateContainerCmdModifier(Consumer<CreateContainerCmd> modifier
11571178
return self();
11581179
}
11591180

1181+
/**
1182+
* Size of /dev/shm
1183+
* @param bytes The number of megabytes to assign the shared memory. If null, it will apply the Docker default which is 64 MB.
1184+
* @return this
1185+
*/
1186+
public SELF withSharedMemorySize(Long bytes) {
1187+
this.shmSize = bytes;
1188+
return self();
1189+
}
1190+
11601191
/**
11611192
* Convenience class with access to non-public members of GenericContainer.
11621193
*

core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package org.testcontainers.junit;
22

3+
import com.github.dockerjava.api.model.HostConfig;
34
import com.google.common.collect.ImmutableMap;
45
import com.google.common.util.concurrent.Uninterruptibles;
56
import com.mongodb.MongoClient;
67
import com.mongodb.client.MongoCollection;
78
import com.mongodb.client.MongoDatabase;
89
import com.rabbitmq.client.*;
10+
import org.apache.commons.io.FileUtils;
911
import org.bson.Document;
1012
import org.junit.*;
1113
import org.rnorth.ducttape.RetryCountExceededException;
@@ -375,4 +377,18 @@ public void addExposedPortAfterWithExposedPortsTest() {
375377
assertTrue("withExposedPort should be exposed", redis.getExposedPorts().contains(REDIS_PORT));
376378
assertTrue("addExposedPort should be exposed", redis.getExposedPorts().contains(8987));
377379
}
380+
381+
@Test
382+
public void sharedMemorySetTest() {
383+
try (GenericContainer containerWithSharedMemory = new GenericContainer("busybox:1.29")
384+
.withSharedMemorySize(1024L * FileUtils.ONE_MB)) {
385+
386+
containerWithSharedMemory.start();
387+
388+
HostConfig hostConfig =
389+
containerWithSharedMemory.getDockerClient().inspectContainerCmd(containerWithSharedMemory.getContainerId())
390+
.exec().getHostConfig();
391+
assertEquals("Shared memory not set on container", hostConfig.getShmSize(), 1024 * FileUtils.ONE_MB);
392+
}
393+
}
378394
}

0 commit comments

Comments
 (0)