diff --git a/src/main/groovy/com/avast/gradle/dockercompose/tasks/ComposeUp.groovy b/src/main/groovy/com/avast/gradle/dockercompose/tasks/ComposeUp.groovy index 1e783bb..f843ad3 100644 --- a/src/main/groovy/com/avast/gradle/dockercompose/tasks/ComposeUp.groovy +++ b/src/main/groovy/com/avast/gradle/dockercompose/tasks/ComposeUp.groovy @@ -111,7 +111,10 @@ class ComposeUp extends DefaultTask { String gateway Map networkSettings = inspection.NetworkSettings Map networks = networkSettings.Networks - if (networks) { + if (networks && networks.every { it.key.toLowerCase().equals("host") }) { + gateway = 'localhost' + logger.debug("Will use $gateway as host of $serviceName because it is using HOST network") + } else if (networks) { Map.Entry firstNetworkPair = networks.find() gateway = firstNetworkPair.value.Gateway logger.debug("Will use $gateway (network ${firstNetworkPair.key}) as host of $serviceName") diff --git a/src/test/groovy/com/avast/gradle/dockercompose/DockerComposePluginTest.groovy b/src/test/groovy/com/avast/gradle/dockercompose/DockerComposePluginTest.groovy index e085903..a1400c9 100644 --- a/src/test/groovy/com/avast/gradle/dockercompose/DockerComposePluginTest.groovy +++ b/src/test/groovy/com/avast/gradle/dockercompose/DockerComposePluginTest.groovy @@ -223,6 +223,37 @@ class DockerComposePluginTest extends Specification { '''] } + def "expose localhost as a host for container with HOST networking"() { + def projectDir = new TmpDirTemporaryFileProvider().createTemporaryDirectory("gradle", "projectDir") + new File(projectDir, 'docker-compose.yml') << ''' + version: '2' + services: + web: + image: nginx + network_mode: host + ports: + - 80 + ''' + def project = ProjectBuilder.builder().withProjectDir(projectDir).build() + project.plugins.apply 'java' + project.plugins.apply 'docker-compose' + project.tasks.composeUp.up() + Test test = project.tasks.test as Test + when: + project.dockerCompose.exposeAsEnvironment(test) + project.dockerCompose.exposeAsSystemProperties(test) + then: + test.environment.get('WEB_HOST') == 'localhost' + test.systemProperties.get('web.host') == 'localhost' + cleanup: + project.tasks.composeDown.down() + try { + projectDir.delete() + } catch(ignored) { + projectDir.deleteOnExit() + } + } + def "reads logs of service"() { def projectDir = new TmpDirTemporaryFileProvider().createTemporaryDirectory("gradle", "projectDir") new File(projectDir, 'docker-compose.yml') << '''