Allow setting /dev/shm size in DockerBuildImage and DockerCreateContainer
MODIFY
src/functTest/groovy/com/bmuschko/gradle/docker/DockerWorkflowFunctionalTest.groovy
src/functTest/groovy/com/bmuschko/gradle/docker/DockerWorkflowFunctionalTest.groovy
@@ -494,6 +494,49 @@ class DockerWorkflowFunctionalTest extends AbstractFunctionalTest {
result.output.contains("Devices : [/dev/sda:/dev/xvda:rwm]")
}
+ def "Can build an image, create a container and set /dev/shm size"() {
+ File imageDir = temporaryFolder.newFolder('images', 'minimal')
+ File dockerFile = createDockerfile(imageDir)
+
+ String uniqueContainerName = createUniqueContainerName()
+
+ buildFile << """
+ import com.bmuschko.gradle.docker.tasks.image.DockerBuildImage
+ import com.bmuschko.gradle.docker.tasks.container.DockerCreateContainer
+ import com.bmuschko.gradle.docker.tasks.container.DockerInspectContainer
+
+ task buildImage(type: DockerBuildImage) {
+ inputDir = file("${dockerFile.parentFile.path}")
+ tag = "${createUniqueImageId()}"
+ }
+
+ task createContainer(type: DockerCreateContainer) {
+ dependsOn buildImage
+ targetImageId { buildImage.getImageId() }
+ containerName = "${uniqueContainerName}"
+ shmSize = 128000
+ cmd = ['/bin/sh']
+ }
+
+ task inspectContainer(type: DockerInspectContainer) {
+ dependsOn createContainer
+ targetContainerId { createContainer.getContainerId() }
+ onNext { c ->
+ if(c.hostConfig.shmSize != 128000) {
+ throw new GradleException("Invalid ShmSize value!")
+ }
+ }
+ }
+
+ task workflow {
+ dependsOn inspectContainer
+ }
+ """
+
+ expect:
+ build('workflow')
+ }
+
def "Can build an image, create a container and assign a network and alias"() {
File imageDir = temporaryFolder.newFolder('images', 'minimal')
File dockerFile = createDockerfile(imageDir)

MODIFY
src/functTest/groovy/com/bmuschko/gradle/docker/tasks/image/DockerBuildImageFunctionalTest.groovy
src/functTest/groovy/com/bmuschko/gradle/docker/tasks/image/DockerBuildImageFunctionalTest.groovy
@@ -65,6 +65,32 @@ class DockerBuildImageFunctionalTest extends AbstractFunctionalTest {
}
+ def "can set /dev/shm size for image build"() {
+ buildFile << buildImageWithShmSize()
+
+ when:
+ build("buildWithShmSize")
+
+ then:
+ noExceptionThrown()
+ }
+
+ private String buildImageWithShmSize() {
+ """
+ import com.bmuschko.gradle.docker.tasks.image.Dockerfile
+ import com.bmuschko.gradle.docker.tasks.image.DockerBuildImage
+
+ task dockerfile(type: Dockerfile) {
+ from 'alpine'
+ }
+
+ task buildWithShmSize(type: DockerBuildImage) {
+ dependsOn dockerfile
+ inputDir = file("build/docker")
+ shmSize = 128000
+ }
+ """
+ }
private String imageCreation() {
"""

MODIFY
src/main/groovy/com/bmuschko/gradle/docker/tasks/container/DockerCreateContainer.groovy
src/main/groovy/com/bmuschko/gradle/docker/tasks/container/DockerCreateContainer.groovy
@@ -151,6 +151,15 @@ class DockerCreateContainer extends AbstractDockerRemoteApiTask {
@Optional
List<String> devices
+ /**
+ * Size of <code>/dev/shm</code> in bytes.
+ * The size must be greater than 0.
+ * If omitted the system uses 64MB.
+ */
+ @Input
+ @Optional
+ Long shmSize
+
@Internal
String containerId
@@ -326,6 +335,11 @@ class DockerCreateContainer extends AbstractDockerRemoteApiTask {
if(getTty()) {
containerCommand.withTty(getTty())
}
+
+ if(getShmSize() != null) { // 0 is valid input
+ def hostConfig = threadContextClassLoader.createHostConfig([shmSize: getShmSize()])
+ containerCommand.withHostConfig(hostConfig)
+ }
}
static class LogConfig {

MODIFY
src/main/groovy/com/bmuschko/gradle/docker/tasks/image/DockerBuildImage.groovy
src/main/groovy/com/bmuschko/gradle/docker/tasks/image/DockerBuildImage.groovy
@@ -76,6 +76,15 @@ class DockerBuildImage extends AbstractDockerRemoteApiTask implements RegistryCr
@Optional
Map<String, String> buildArgs = [:]
+ /**
+ * Size of <code>/dev/shm</code> in bytes.
+ * The size must be greater than 0.
+ * If omitted the system uses 64MB.
+ */
+ @Input
+ @Optional
+ Long shmSize
+
/**
* The target Docker registry credentials for building image.
*/
@@ -133,6 +142,10 @@ class DockerBuildImage extends AbstractDockerRemoteApiTask implements RegistryCr
buildImageCmd.withLabels(getLabels())
}
+ if(getShmSize() != null) { // 0 is valid input
+ buildImageCmd.withShmsize(getShmSize())
+ }
+
if (getRegistryCredentials()) {
def authConfig = threadContextClassLoader.createAuthConfig(getRegistryCredentials())
def authConfigurations = threadContextClassLoader.createAuthConfigurations([authConfig])
@@ -140,7 +153,7 @@ class DockerBuildImage extends AbstractDockerRemoteApiTask implements RegistryCr
}
buildArgs.each { arg, value ->
- buildImageCmd = buildImageCmd.withBuildArg(arg, value);
+ buildImageCmd = buildImageCmd.withBuildArg(arg, value)
}
def callback = onNext ? threadContextClassLoader.createBuildImageResultCallback(this.onNext)

Allow setting /dev/shm size in DockerBuildImage and DockerCreateContainer (#413)