Simplifies usage of Docker Compose for local development and integration testing in Gradle environment.
composeUp task starts the application and waits till all containers become healthy and all exposed TCP ports are open (so till the application is ready). It reads assigned host and ports of particular containers and stores them into dockerCompose.servicesInfos property.
composeDown task stops the application and removes the containers.
composePull task pulls and optionally builds the images required by the application. This is useful, for example, with a CI platform that caches docker images to decrease build times.
- I want to be able to run my application on my computer, and it must work for my colleagues as well. Just execute
docker-compose upand I'm done. - I want to be able to test my application on my computer - I don't wanna wait till my application is deployed into dev/testing environment and acceptance/end2end tests get executed. I want to execute these tests on my computer - it means execute
docker-compose upbefore these tests.
You could easily ensure that docker-compose up is called before your tests but there are few gotchas that this plugin solves:
- If you execute
docker-compose up -d(detached) then this command returns immediately and your application is probably not able to serve requests at this time. This plugin waits till all containers become healthy and all exported TCP ports of all services are open.
- If waiting for healthy state or open TCP ports timeouts (default is 15 minutes) then it prints log of related service.
- It's recommended not to assign fixed values of exposed ports in
docker-compose.yml(i.e.8888:80) because it can cause ports collision on integration servers. If you don't assign a fixed value for exposed port (use just80) then the port is exposed as a random free port. This plugin reads assigned ports (and even IP addresses of containers) and stores them intodockerCompose.servicesInfomap.
The plugin must be applied on project that contains docker-compose.yml file. It supposes that Docker Engine and Docker Compose are installed and available in PATH.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "com.avast.gradle:docker-compose-gradle-plugin:$versionHere"
}
}
apply plugin: 'java'
apply plugin: 'docker-compose'
// Or use the new Gradle Portal plugins:
// plugins {
// id 'com.avast.gradle.docker-compose' version "$versionHere"
// }
dockerCompose.isRequiredBy(test) // hooks 'dependsOn composeUp' and 'finalizedBy composeDown'
dockerCompose {
// useComposeFiles = ['docker-compose.yml', 'docker-compose.prod.yml'] // like 'docker-compose -f <file>'
// captureContainersOutput = true // prints output of all containers to Gradle output - very useful for debugging
// stopContainers = false // doesn't call `docker-compose down` - useful for debugging
// removeContainers = false
// removeImages = "None" // Other accepted values are: "All" and "Local"
// removeVolumes = false
// projectName = 'my-project' // allow to set custom docker-compose project name (defaults to directory name)
// executable = '/path/to/docker-compose' // allow to set the path of the docker-compose executable (usefull if not present in PATH)
// dockerExecutable = '/path/to/docker' // allow to set the path of the docker executable (usefull if not present in PATH)
// environment.put 'BACKEND_ADDRESS', '192.168.1.100' // Pass environment variable to 'docker-compose' for substitution in compose file
}
test.doFirst {
// exposes "${serviceName}_HOST" and "${serviceName}_TCP_${exposedPort}" environment variables
// for example exposes "WEB_HOST" and "WEB_TCP_80" environment variables for service named `web` with exposed port `80`
dockerCompose.exposeAsEnvironment(test)
// exposes "${serviceName}.host" and "${serviceName}.tcp.${exposedPort}" system properties
// for example exposes "web.host" and "web.tcp.80" system properties for service named `web` with exposed port `80`
dockerCompose.exposeAsSystemProperties(test)
// get information about container of service `web` (declared in docker-compose.yml)
def webInfo = dockerCompose.servicesInfos.web
// pass host and exposed TCP port 80 as custom-named Java System properties
systemProperty 'myweb.host', webInfo.host
systemProperty 'myweb.port', webInfo.ports[80]
}- You can call
dockerCompose.isRequiredBy(anyTask)for any task, for example for your customintegrationTesttask. - If some Dockerfile needs an artifact generated by Gradle then you can declare this dependency in a standard way, like
composeUp.dependsOn project(':my-app').distTar - All properties in
dockerComposehave meaningful default values so you don't have to touch it. If you are interested then you can look at ComposeExtension.groovy for reference. dockerCompose.servicesInfoscontains information about running containers so you must access this property aftercomposeUptask is finished. SodoFirstof your test task is perfect place where to access it.- Plugin honours a
docker-compose.override.ymlfile, but only when no files are specified withuseComposeFiles(conform command-line behavior). - Check ServiceInfo.groovy to see what you can know about running containers.