Skip to content

Commit

Permalink
Grails application runnable as Docker container
Browse files Browse the repository at this point in the history
Initial import of the sources.
  • Loading branch information
mrhaki committed Oct 21, 2015
1 parent d053fdb commit 052d8e5
Show file tree
Hide file tree
Showing 61 changed files with 11,751 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@

# "temporary" build files
/target

/build
.gradle
3 changes: 3 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
= Grails 3 Docker Sample

Sample Grails 3 application with Docker support.
82 changes: 82 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
buildscript {
ext {
grailsVersion = project.grailsVersion
}
repositories {
mavenLocal()
maven { url "https://repo.grails.org/grails/core" }
jcenter()
}
dependencies {
classpath "org.grails:grails-gradle-plugin:$grailsVersion"
classpath 'com.bertramlabs.plugins:asset-pipeline-gradle:2.5.0'
classpath "org.grails.plugins:hibernate:4.3.10.5"
}
}

plugins {
id "io.spring.dependency-management" version "0.5.2.RELEASE"
}

version "1.0"

group "docker.app"

apply plugin: "spring-boot"
apply plugin: "asset-pipeline"
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: "org.grails.grails-web"
apply plugin: "org.grails.grails-gsp"

ext {
grailsVersion = project.grailsVersion
gradleWrapperVersion = project.gradleWrapperVersion
}

assets {
minifyJs = true
minifyCss = true
}

repositories {
mavenLocal()
maven { url "https://repo.grails.org/grails/core" }
}

dependencyManagement {
imports {
mavenBom "org.grails:grails-bom:$grailsVersion"
}
applyMavenExclusions false
}

dependencies {
compile "org.springframework.boot:spring-boot-starter-logging"
compile "org.springframework.boot:spring-boot-starter-actuator"
compile "org.springframework.boot:spring-boot-autoconfigure"
compile "org.springframework.boot:spring-boot-starter-tomcat"
compile "org.grails:grails-dependencies"
compile "org.grails:grails-web-boot"

compile "org.grails.plugins:hibernate"
compile "org.grails.plugins:cache"
compile "org.hibernate:hibernate-ehcache"
compile "org.grails.plugins:scaffolding"

runtime "org.grails.plugins:asset-pipeline"

testCompile "org.grails:grails-plugin-testing"
testCompile "org.grails.plugins:geb"

// Note: It is recommended to update to a more robust driver (Chrome, Firefox etc.)
testRuntime 'org.seleniumhq.selenium:selenium-htmlunit-driver:2.44.0'

console "org.grails:grails-console"
}

task wrapper(type: Wrapper) {
gradleVersion = gradleWrapperVersion
}

apply from: "$rootDir/gradle/docker.gradle"
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
grailsVersion=3.0.9
gradleWrapperVersion=2.3
195 changes: 195 additions & 0 deletions gradle/docker.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
buildscript {
repositories {
jcenter()
}
dependencies {
// Add Gradle Docker plugin.
classpath 'com.bmuschko:gradle-docker-plugin:2.6.1'
}
}


// Add Gradle Docker plugin.
// Use plugin type, because it is used with apply from:
// in main Gradle build script.
apply plugin: com.bmuschko.gradle.docker.DockerRemoteApiPlugin


ext {
// Define tag for Docker image. Include project version.
dockerTag = "mrhaki/${project.name}:${project.version}".toString()

// Base name for Docker container with Grails application.
dockerContainerName = 'grails-sample'

// Staging directory for create Docker image.
dockerBuildDir = mkdir("${buildDir}/docker")

// Group name for tasks related to Docker.
dockerBuildGroup = 'Docker'
}


docker {
// Set Docker host URL based on existence of environment
// variable DOCKER_HOST.
url = System.env.DOCKER_HOST ?
System.env.DOCKER_HOST.replace("tcp", "https") :
'unix:///var/run/docker.sock'
}


import com.bmuschko.gradle.docker.tasks.image.Dockerfile
import com.bmuschko.gradle.docker.tasks.image.DockerBuildImage
import com.bmuschko.gradle.docker.tasks.image.DockerRemoveImage
import com.bmuschko.gradle.docker.tasks.container.DockerCreateContainer
import com.bmuschko.gradle.docker.tasks.container.DockerStartContainer
import com.bmuschko.gradle.docker.tasks.container.DockerStopContainer
import com.bmuschko.gradle.docker.tasks.container.DockerRemoveContainer

task dockerRepackage(type: BootRepackage, dependsOn: jar) {
description = 'Repackage Grails application JAR to make it runnable.'
group = dockerBuildGroup

ext {
// Extra task property with file name for the
// repackaged JAR file.
// We can reference this extra task property from
// other tasks.
dockerJar = file("${dockerBuildDir}/${jar.archiveName}")
}

outputFile = dockerJar
withJarTask = jar
}

task prepareDocker(type: Copy, dependsOn: dockerRepackage) {
description = 'Copy files from src/main/docker to Docker build dir.'
group = dockerBuildGroup

into dockerBuildDir
from 'src/main/docker'
}

task createDockerfile(type: Dockerfile, dependsOn: prepareDocker) {
description = 'Create Dockerfile to build image.'
group = dockerBuildGroup

destFile = file("${dockerBuildDir}/Dockerfile")

// Contents of Dockerfile:
from 'java:8'
maintainer 'Hubert Klein Ikkink "mrhaki"'

// Expose default port 8080 for Grails application.
exposePort 8080

// Create environment variable so we can customize the
// grails.env Java system property via Docker's environment variable
// support. We can re-use this image for different Grails environment
// values with this construct.
environmentVariable 'GRAILS_ENV', 'production'

// Create a config directory and expose as volume.
// External configuration files in this volume are automatically
// picked up.
runCommand 'mkdir -p /app/config'
volume '/app/config'

// Working directory is set, so next commands are executed
// in the context of /app.
workingDir '/app'

// Copy JAR file from dockerRepackage task that was generated in
// build/docker.
copyFile dockerRepackage.dockerJar.name, 'application.jar'
// Copy shell script for starting application.
copyFile 'docker-entrypoint.sh', 'docker-entrypoint.sh'
// Make shell script executable in container.
runCommand 'chmod +x docker-entrypoint.sh'

// Define ENTRYPOINT to execute shell script.
// By using ENTRYPOINT we can add command line arguments
// when we run the container based on this image.
entryPoint './docker-entrypoint.sh'
}

task buildImage(type: DockerBuildImage, dependsOn: createDockerfile) {
description = 'Create Docker image with Grails application.'
group = dockerBuildGroup

inputDir = file(dockerBuildDir)
tag = dockerTag
}

task removeImage(type: DockerRemoveImage) {
description = 'Remove Docker image with Grails application.'
group = dockerBuildGroup

targetImageId { dockerTag }
}

//------------------------------------------------------------------------------
// Extra tasks to create, run, stop and remove containers
// for a development and production environment.
//------------------------------------------------------------------------------
['development', 'production'].each { environment ->

// Transform environment for use in task names.
final String taskName = environment.capitalize()

// Name for container contains the environment name.
final String name = "${dockerContainerName}-${environment}"

task "createContainer$taskName"(type: DockerCreateContainer) {
description = "Create Docker container $name with grails.env $environment."
group = dockerBuildGroup

targetImageId { dockerTag }
containerName = name

// Expose port 8080 from container to outside as port 8080.
portBindings = ['8080:8080']

// Set environment variable GRAILS_ENV to environment value.
// The docker-entrypoint.sh script picks up this environment
// variable and turns it into Java system property
// -Dgrails.env.
env = ["GRAILS_ENV=$environment"]

// Example of adding extra command line arguments to the
// java -jar app.jar that is executed in the container.
cmd = ["--app.dockerContainerName=${containerName}"]

// The image has a volume /app/config for external configuration
// files that are automatically picked up by the Grails application.
// In this example we use a local directory with configuration files
// on our host and bind it to the volume in the container.
binds = [
(file("$projectDir/src/main/config/${environment}").absolutePath):
'/app/config']
}


task "startContainer$taskName"(type: DockerStartContainer) {
description = "Start Docker container $name."
group = dockerBuildGroup

targetContainerId { name }
}

task "stopContainer$taskName"(type: DockerStopContainer) {
description = "Stop Docker container $name."
group = dockerBuildGroup

targetContainerId { name }
}

task "removeContainer$taskName"(type: DockerRemoveContainer) {
description = "Remove Docker container $name."
group = dockerBuildGroup

targetContainerId { name }
}

}
Binary file added gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
6 changes: 6 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#Wed Feb 04 17:05:29 CST 2015
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.3-bin.zip
Loading

0 comments on commit 052d8e5

Please sign in to comment.