├── application ├── gradle.properties ├── gradle │ └── wrapper │ │ └── gradle-wrapper.properties ├── src │ ├── main │ │ ├── resources │ │ │ └── application.yml │ │ └── java │ │ │ └── griffio │ │ │ └── Application.java │ └── test │ │ └── java │ │ └── griffio │ │ └── ApplicationConfigurationTests.java ├── build.gradle ├── gradlew.bat └── gradlew ├── nginx ├── Dockerfile └── nginx.conf ├── docker-compose-base.yml ├── docker-compose-devl.yml ├── docker-compose-prod.yml ├── .gitignore └── README.md /application/gradle.properties: -------------------------------------------------------------------------------- 1 | org.gradle.daemon=true 2 | -------------------------------------------------------------------------------- /nginx/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM nginx:1.9 2 | 3 | RUN rm /etc/nginx/conf.d/default.conf 4 | 5 | ADD nginx.conf /etc/nginx/conf.d/ 6 | -------------------------------------------------------------------------------- /docker-compose-base.yml: -------------------------------------------------------------------------------- 1 | nginx: 2 | build: nginx 3 | restart: always 4 | 5 | application: 6 | build: ./application/build/docker 7 | restart: always 8 | 9 | -------------------------------------------------------------------------------- /application/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Apr 19 14:58:05 BST 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.9-bin.zip 7 | -------------------------------------------------------------------------------- /application/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8080 3 | 4 | spring: 5 | application: 6 | name: app 7 | 8 | ribbon: 9 | ServerListRefreshInterval: 1000 10 | 11 | endpoints: 12 | health: 13 | sensitive: false 14 | restart: 15 | enabled: true 16 | shutdown: 17 | enabled: true 18 | -------------------------------------------------------------------------------- /docker-compose-devl.yml: -------------------------------------------------------------------------------- 1 | #local environment 8080 is available outside container 2 | localapplication: 3 | extends: 4 | file: docker-compose-base.yml 5 | service: application 6 | ports: 7 | - "8080:8080" 8 | 9 | localnginx: 10 | extends: 11 | file: docker-compose-base.yml 12 | service: nginx 13 | ports: 14 | - "80:80" 15 | links: 16 | - localapplication:application 17 | 18 | -------------------------------------------------------------------------------- /docker-compose-prod.yml: -------------------------------------------------------------------------------- 1 | #production environment is port 80 (8080 is only exposed for container linking) 2 | prodapplication: 3 | extends: 4 | file: docker-compose-base.yml 5 | service: application 6 | expose: 7 | - "8080:8080" 8 | 9 | prodnginx: 10 | extends: 11 | file: docker-compose-base.yml 12 | service: nginx 13 | ports: 14 | - "80:80" 15 | links: 16 | - prodapplication:application 17 | -------------------------------------------------------------------------------- /nginx/nginx.conf: -------------------------------------------------------------------------------- 1 | # Configuration for the server 2 | server { 3 | charset utf-8; 4 | listen 80; 5 | location / { 6 | proxy_pass http://application:8080; 7 | proxy_redirect off; 8 | proxy_set_header Host $host; 9 | proxy_set_header X-Real-IP $remote_addr; 10 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 11 | proxy_set_header X-Forwarded-Host $server_name; 12 | } 13 | } 14 | 15 | -------------------------------------------------------------------------------- /application/src/main/java/griffio/Application.java: -------------------------------------------------------------------------------- 1 | package griffio; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | @SpringBootApplication 9 | @RestController 10 | public class Application { 11 | 12 | @RequestMapping("/") 13 | public String index() { 14 | return "Welcome"; 15 | } 16 | 17 | public static void main(String[] args) { 18 | SpringApplication.run(Application.class, args); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by http://www.gitignore.io 2 | 3 | ### Java ### 4 | *.class 5 | 6 | # Mobile Tools for Java (J2ME) 7 | .mtj.tmp/ 8 | 9 | # Package Files # 10 | *.jar 11 | *.war 12 | *.ear 13 | 14 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 15 | hs_err_pid* 16 | 17 | # Created by http://www.gitignore.io 18 | 19 | ### Intellij ### 20 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm 21 | 22 | ## Directory-based project format 23 | .idea/ 24 | # if you remove the above rule, at least ignore user-specific stuff: 25 | # .idea/workspace.xml 26 | # .idea/tasks.xml 27 | # and these sensitive or high-churn files: 28 | # .idea/dataSources.ids 29 | # .idea/dataSources.xml 30 | # .idea/sqlDataSources.xml 31 | # .idea/dynamic.xml 32 | 33 | ## File-based project format 34 | *.ipr 35 | *.iws 36 | *.iml 37 | 38 | ## Additional for IntelliJ 39 | out/ 40 | classes/ 41 | 42 | # generated by mpeltonen/sbt-idea plugin 43 | .idea_modules/ 44 | 45 | # generated by JIRA plugin 46 | atlassian-ide-plugin.xml 47 | 48 | # generated by Crashlytics plugin (for Android Studio and Intellij) 49 | com_crashlytics_export_strings.xml 50 | 51 | # Created by http://www.gitignore.io 52 | 53 | ### Gradle ### 54 | .gradle 55 | build/ 56 | -------------------------------------------------------------------------------- /application/src/test/java/griffio/ApplicationConfigurationTests.java: -------------------------------------------------------------------------------- 1 | package griffio; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.beans.factory.annotation.Value; 6 | import org.springframework.boot.test.IntegrationTest; 7 | import org.springframework.boot.test.SpringApplicationConfiguration; 8 | import org.springframework.boot.test.TestRestTemplate; 9 | import org.springframework.http.HttpStatus; 10 | import org.springframework.http.ResponseEntity; 11 | import org.springframework.test.annotation.DirtiesContext; 12 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 13 | import org.springframework.test.context.web.WebAppConfiguration; 14 | 15 | import static org.junit.Assert.assertEquals; 16 | 17 | @RunWith(SpringJUnit4ClassRunner.class) 18 | @SpringApplicationConfiguration(classes = Application.class) 19 | @WebAppConfiguration 20 | @IntegrationTest("server.port=0") 21 | @DirtiesContext 22 | public class ApplicationConfigurationTests { 23 | 24 | @Value("${local.server.port}") 25 | private int port; 26 | 27 | @Test 28 | public void index_Ok() throws Exception { 29 | ResponseEntity entity; 30 | entity = new TestRestTemplate().getForEntity("http://localhost:" + this.port + "/", String.class); 31 | assertEquals(HttpStatus.OK, entity.getStatusCode()); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /application/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | jcenter() 4 | } 5 | 6 | dependencies { 7 | classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE", 8 | "com.bmuschko:gradle-docker-plugin:3.0.1") 9 | } 10 | } 11 | 12 | apply plugin: 'java' 13 | apply plugin: 'spring-boot' 14 | apply plugin: 'com.bmuschko.docker-remote-api' 15 | 16 | repositories { 17 | jcenter() 18 | } 19 | 20 | dependencies { 21 | compile("org.springframework.boot:spring-boot-starter-web") 22 | testCompile("org.springframework.boot:spring-boot-starter-test") 23 | } 24 | 25 | jar { 26 | baseName = 'griffio-app' 27 | version = '1.0.0' 28 | } 29 | 30 | repositories { 31 | mavenCentral() 32 | } 33 | 34 | tasks.withType(JavaCompile) { 35 | description = "javac: ignore processor hints, keep all other hints" 36 | sourceCompatibility = JavaVersion.VERSION_1_8 37 | targetCompatibility = JavaVersion.VERSION_1_8 38 | options.compilerArgs += ["-Xlint:all,-processing"] 39 | } 40 | 41 | dependencies { 42 | compile("org.springframework.boot:spring-boot-starter-web") 43 | testCompile("org.springframework.boot:spring-boot-starter-test") 44 | } 45 | 46 | task wrapper(type: Wrapper) { 47 | gradleVersion = '2.14' 48 | } 49 | 50 | import com.bmuschko.gradle.docker.tasks.image.Dockerfile 51 | 52 | task createDockerfile(type: Dockerfile, dependsOn: build) { 53 | destFile = project.file("build/docker/Dockerfile") 54 | 55 | from "azul/zulu-openjdk-centos:8" 56 | maintainer "griffio@users.noreply.github.com" 57 | runCommand "bash -c 'touch /main.jar'" 58 | addFile "${jar.archiveName}", "main.jar" 59 | entryPoint { ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/main.jar"] } 60 | volume "/tmp" 61 | doFirst { 62 | copy { 63 | from jar 64 | into project.file("build/docker") 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # example-docker-compose-01 2 | 3 | Example docker containers composed of Nginx and Spring Boot Java application 4 | 5 | [application](/tree/master/application) 6 | [nginx](/tree/master/nginx) 7 | 8 | [docker-compose](https://docs.docker.com/compose/) 9 | 10 | Launches two containers - for nginx, JVM; that can be configured for local and production use. 11 | 12 | Required docker configuration files: 13 | 14 | ~~~ 15 | /docker-compose-base.yml - Shared config that can be extended 16 | /docker-compose-devl.yml - Localhost deploy 17 | /docker-compose-prod.yml - Cloud provider deploy 18 | /nginx/DockerFile - static file 19 | /application/build/docker/DockerFile - generated docker file 20 | ~~~ 21 | 22 | * Docker Gradle plugin task [com.bmuschko.gradle.docker.tasks.image.Dockerfile](https://github.com/bmuschko/gradle-docker-plugin) generates a DockerFile. 23 | * The build jar is copied to DockerFile context location. 24 | * Local files can only be added to container from Docker build context location. 25 | 26 | ~~~ 27 | cd application 28 | ~~~ 29 | 30 | Generates java docker file 31 | 32 | ~~~ 33 | ./gradlew createDockerFile 34 | ~~~ 35 | 36 | --- 37 | 38 | docker-compose.yml is the default filename when --file is not specified. 39 | 40 | ~~~ 41 | docker-compose --file docker-compose-devl.yml build 42 | docker-compose --file docker-compose-devl.yml up # -d can be appended for detached process 43 | ~~~ 44 | 45 | ### Production environment 46 | 47 | docker-machine create a docker host on a vm, openstack provider, carina etc 48 | 49 | ~~~ 50 | docker-machine create --driver yourprovider springboot 51 | ~~~ 52 | 53 | 2 mins later... 54 | 55 | ~~~ 56 | docker-machine env springboot 57 | docker-compose --file docker-compose-prod.yml up -d 58 | ~~~ 59 | 60 | ### Carina 61 | 62 | Using Carina - https://app.getcarina.com/app/signup 63 | 64 | ~~~ 65 | export CARINA_USERNAME="username@example.com" 66 | 67 | export CARINA_APIKEY="abc..." 68 | 69 | carina create cluster001 --wait --nodes=1 70 | 71 | carina credentials cluster001 72 | 73 | eval $(carina env cluster001) 74 | 75 | env | grep DOCKER 76 | 77 | docker-compose --file docker-compose-prod.yml up -d 78 | ~~~ 79 | -------------------------------------------------------------------------------- /application/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /application/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | --------------------------------------------------------------------------------