Simplifies usage of Docker Compose for local development and integration testing in Gradle environment.
composeUp
task starts the application and waits till 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.
- 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 up
and 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 up
before 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 exported TCP ports of all services are open.
- If waiting for open TCP ports timeouts (default 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.servicesInfo
map.
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'
dockerCompose.isRequiredBy(test) // hooks 'dependsOn composeUp' and 'finalizedBy composeDown'
dockerCompose {
// useComposeFiles = ['docker-compose.yml', 'docker-compose.prod.yml'] // like 'docker-compose -f <file>'
// stopContainers = false // useful for debugging
}
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 customintegrationTest
task. - 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
dockerCompose
have meaningfull default values so you don't have to touch it. If you are interested then you can look at ComposeExtension.groovy for reference. dockerCompose.servicesInfos
contains information about running containers so you must access this property aftercomposeUp
task is finished. SodoFirst
of your test task is perfect place where to access it.- Plugin honours a
docker-compose.override.yml
file, but only when no files are specified withuseComposeFiles
(conform command-line behavior). - Check ServiceInfo.groovy to see what you can know about running containers.