From 4ae3722636482ed4873c7a2d4a3b81501f97957f Mon Sep 17 00:00:00 2001 From: Dennis Schulte Date: Thu, 2 Jul 2015 11:23:56 +0200 Subject: [PATCH] Add Docker Compose --- .gitignore | 2 + README.md | 45 +++++++++++++++--- Vagrantfile | 2 +- build.sh | 10 ++++ docker-compose.yml | 47 +++++++++++++++++++ init.sh | 2 +- movie-database-actors/Dockerfile | 19 ++++++++ .../src/main/resources/application.properties | 6 +-- movie-database-monitoring/Dockerfile | 19 ++++++++ movie-database-movies/Dockerfile | 19 ++++++++ .../src/main/resources/application.properties | 6 +-- movie-database-navigation/Dockerfile | 20 ++++++++ .../src/main/resources/application.properties | 2 +- movie-database-shop-app/Dockerfile | 16 +++++++ movie-database-shop-app/moviedatabase.conf | 35 ++++++++++++++ movie-database-shop-app/run.sh | 4 ++ movie-database-shop-rest/Dockerfile | 19 ++++++++ .../src/main/resources/application.properties | 4 +- shutdown.sh | 2 +- buildAndStartup.sh => startup.sh | 19 +++----- vagrant-etc-hosts | 7 +++ 21 files changed, 274 insertions(+), 31 deletions(-) create mode 100644 build.sh create mode 100644 docker-compose.yml create mode 100644 movie-database-actors/Dockerfile create mode 100644 movie-database-monitoring/Dockerfile create mode 100644 movie-database-movies/Dockerfile create mode 100644 movie-database-navigation/Dockerfile create mode 100644 movie-database-shop-app/Dockerfile create mode 100644 movie-database-shop-app/moviedatabase.conf create mode 100644 movie-database-shop-app/run.sh create mode 100644 movie-database-shop-rest/Dockerfile rename buildAndStartup.sh => startup.sh (70%) create mode 100644 vagrant-etc-hosts diff --git a/.gitignore b/.gitignore index 42e079d..2a0735a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ /.settings/ /.project /logs/ +.vagrant +dump.rdb diff --git a/README.md b/README.md index ea67253..75dcff5 100644 --- a/README.md +++ b/README.md @@ -14,21 +14,45 @@ An SSO mechanism is used for security, the infrastructure setup to support this Clone this repository, jump into the subdirectory movie-database-vagrant and do vagrant up + That will take a while (and it will change some of the application.properties files, don't be confused about that). Edit your /private/etc/hosts file and add the line 192.168.56.13 moviedatabase.com + When everything is started, access [http://moviedatabase.com](http://moviedatabase.com) in your browser for the application. Currently there are two users, admin/admin and user/user. For monitoring with Spring Boot Admin access [http://192.168.56.13:8083](http://192.168.56.13:8083). This repo that you cloned is shared with the virtual machine. So you can change things in your IDE, log into the virtual machine and rebuild / restart changed services there. +### Build & Run with Docker + +For the installation of Docker and Docker Compose please refer to [https://docs.docker.com/installation] and [https://docs.docker.com/compose/install]. + +Edit your /private/etc/hosts file and add the lines (on the Mac please verify the IP with "boot2docker ip") + + 192.168.59.103 moviedatabase.com + +Clone this repository and do + + ./build.sh + docker-compose up + +When everything is started, access [http://moviedatabase.com](http://moviedatabase.com) in your browser for the application. Currently there are two users, admin/admin and user/user. For monitoring with Spring Boot Admin access [http://192.168.59.103:8083](http://192.168.59.103:8083). + ### Build & Run without virtualization You need to have [Homebrew](http://brew.sh/) installed to do the following. #### nginx -Follow [these](https://gist.github.com/netpoetica/5879685) instructions to install nginx. When editing the /private/etc/hosts file, add the line +Follow [these](https://gist.github.com/netpoetica/5879685) instructions to install nginx. When editing the /private/etc/hosts file, add the lines 127.0.0.1 moviedatabase.com + 127.0.0.1 redis + 127.0.0.1 monitoring + 127.0.0.1 navigation + 127.0.0.1 actors + 127.0.0.1 movies + 127.0.0.1 shop + Now copy [moviedatabase.conf](https://github.com/tobiasflohre/movie-database/blob/master/moviedatabase.conf) to /usr/local/etc/nginx/conf.d/. Start nginx with `sudo nginx`, stop it with `sudo nginx -s stop`. #### Redis @@ -47,32 +71,39 @@ Redis should now reply PONG -Update application.properties in movie-database-movies and movie-database-actors and set +##### Mac - spring.redis.host=127.0.0.1 - +You can install Redis with Homebrew + + brew install redis +And start it with -##### Mac and Windows + redis-server -One fast way to get Redis running is to use a Docker image. If you're on Mac or Windows, you'll need to install [Boot2Docker](http://boot2docker.io/) first and start it. My colleague Ben Ripkens wrote a [nice tool](https://github.com/bripkens/dock) to install standard Docker images fast. To install it, do the following +##### Alternative way for Mac and Windows + +Another fast way to get Redis running is to use a Docker image. If you're on Mac or Windows, you'll need to install [Boot2Docker](http://boot2docker.io/) first and start it. My colleague Ben Ripkens wrote a [nice tool](https://github.com/bripkens/dock) to install standard Docker images fast. To install it, do the following brew tap bripkens/dock brew install dock + To install and startup Redis: dock redis + Now Redis is running under 192.168.59.103:6379, which is exactly what the movie database expects. If you already have a Redis installed somewhere else, take a look at the `application.properties` files contained in the projects, there you may change the host and port used for connecting to Redis (for changing the port you need to add the property `spring.redis.port`, for a list of all Spring Boot properties take a look [here](http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties)). #### Running the applications -Now clone this repository and check out master. From command line you may just call `./buildAndStartup.sh`. This script stops boot2docker, nginx and all movie-database applications and then builds all applications, starts boot2docker and Redis, starts nginx, copies the static Angular app to a specific place and then starts all movie-database applications. You need to have `mvn` and `java` and `boot2docker` and `dock` and `grunt` and `bower` on your path for that (and maybe some other stuff, better take a look at the script). If you're not on OSX it should be easy to adapt the script to your OS. I piped the logs to `dev/null` because the applications themselves are logging to `/tmp`. +Now clone this repository and check out master. From command line you may just call `./build.sh` and then `./startup.sh`. The first script builds all the applactions and the second script stops all services and then starts all applications, starts Redis, starts nginx, copies the static Angular app to a specific place and then starts all movie-database applications. You need to have `mvn` and `java` and `grunt` and `bower` on your path for that (and maybe some other stuff, better take a look at the script). If you're not on OSX it should be easy to adapt the script to your OS. I piped the logs to `dev/null` because the applications themselves are logging to `/tmp`. Alternatively start nginx and Redis yourself, build the Angular app yourself and copy it to the folder accessed by nginx, and then in your IDE import all Maven projects, then run the class `ShopApplication` in the project movie-database-shop-rest, the class `NavigationApplication` in the project movie-database-navigation, the class `ActorsApplication` in the project movie-database-actors, the class `MoviesApplication` in the project movie-database-movies and the class `MonitoringApplication`in the project movie-database-monitoring. Then access [http://moviedatabase.com](http://moviedatabase.com) in your browser for the application. Currently there are two users, admin/admin and user/user. For monitoring with Spring Boot Admin access [http://localhost:8083](http://localhost:8083). ### Build & Run without security + If you really shy away from installing nginx and Redis, you may just start the applications without security. Remove the dependency to movie-database-security from movie-database-movies and movie-database-actors, then remove the reference to `SecurityConfiguration` from those two projects (making them compile-clean again). Now start `NavigationApplication` and `MoviesApplication` (or `ActorsApplication`) and browse to [http://localhost:8080/movie-app](http://localhost:8080/movie-app) ([http://localhost:8082/actor-app](http://localhost:8082/actor-app)). Navigation links won't work, search works. ## Build & Run (classic branch) diff --git a/Vagrantfile b/Vagrantfile index 50b1e40..6010a7c 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -17,6 +17,6 @@ Vagrant.configure("2") do |config| vb.customize ["modifyvm", :id, "--memory", "4096", "--cpus", "2"] vb.name = "movie-database" end - + #config.vm.provision :shell, :inline => "sudo ln -fs /home/vagrant/share/vagrant-etc-hosts /etc/hosts" config.vm.provision :shell, path: "init.sh" end diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..deecffd --- /dev/null +++ b/build.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +echo "Build all services..." +mvn clean install +cd movie-database-shop-app +bower install +grunt +sudo chmod 777 /opt/moviedatabase +rm -rf /opt/moviedatabase/shop-app +cp -R dist/. /opt/moviedatabase/shop-app +cd .. \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..e194fed --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,47 @@ +# Security Storage +redis: + image: redis +movies: + build: movie-database-movies + ports: + - "8080:8080" + links: + - redis + - monitoring + - navigation +actors: + build: movie-database-actors + ports: + - "8082:8082" + links: + - redis + - monitoring + - navigation +navigation: + build: movie-database-navigation + ports: + - "8081:8081" + links: + - redis + - monitoring +shop: + build: movie-database-shop-rest + ports: + - "8084:8084" + links: + - redis + - monitoring +shopgui: + build: movie-database-shop-app + ports: + - "80:80" + links: + - movies + - actors + - navigation + - shop +monitoring: + build: movie-database-monitoring + ports: + - "8083:8083" + diff --git a/init.sh b/init.sh index 815d7a6..6fa0716 100644 --- a/init.sh +++ b/init.sh @@ -19,7 +19,7 @@ fi cd $MOVIE_DATABASE_HOME # fix redis host -find . -type f -name application.properties -exec sed -i 's/192.168.59.103/127.0.0.1/g' {} \;'' +#find . -type f -name application.properties -exec sed -i 's/192.168.59.103/127.0.0.1/g' {} \;'' mvn clean install diff --git a/movie-database-actors/Dockerfile b/movie-database-actors/Dockerfile new file mode 100644 index 0000000..a58d5a2 --- /dev/null +++ b/movie-database-actors/Dockerfile @@ -0,0 +1,19 @@ +FROM java:7 +MAINTAINER Dennis Schulte + +# install updates +RUN apt-get update && \ + apt-get clean && \ + rm -rf /var/lib/apt/* + +# default log directory +#VOLUME ["/data/log"] + +# copy spring boot jar as 'service.jar' +COPY target/movie-database-*.jar /service.jar + +# service port +EXPOSE 8082 + +# define default jar name +ENTRYPOINT java $JVM_OPTS -jar /service.jar $ARGS \ No newline at end of file diff --git a/movie-database-actors/src/main/resources/application.properties b/movie-database-actors/src/main/resources/application.properties index 2918be8..190cf04 100644 --- a/movie-database-actors/src/main/resources/application.properties +++ b/movie-database-actors/src/main/resources/application.properties @@ -2,11 +2,11 @@ spring.application.name=@pom.artifactId@ info.version=@pom.version@ info.stage=test server.port=8082 -moviedatabase.navigation.url.base=http://localhost:8081 +moviedatabase.navigation.url.base=http://navigation:8081 spring.thymeleaf.cache=false -spring.redis.host=192.168.59.103 +spring.redis.host=redis server.context-path=/actor-app -spring.boot.admin.url=http://localhost:8083 +spring.boot.admin.url=http://monitoring:8083 spring.boot.admin.client.url=http://moviedatabase.com/actor-app http.mappers.jsonPrettyPrint=true logging.file=/opt/moviedatabase/logs/@pom.artifactId@/@pom.artifactId@.log diff --git a/movie-database-monitoring/Dockerfile b/movie-database-monitoring/Dockerfile new file mode 100644 index 0000000..5abfd69 --- /dev/null +++ b/movie-database-monitoring/Dockerfile @@ -0,0 +1,19 @@ +FROM java:7 +MAINTAINER Dennis Schulte + +# install updates +RUN apt-get update && \ + apt-get clean && \ + rm -rf /var/lib/apt/* + +# default log directory +#VOLUME ["/data/log"] + +# copy spring boot jar as 'service.jar' +COPY target/movie-database-*.jar /service.jar + +# service port +EXPOSE 8083 + +# define default jar name +ENTRYPOINT java $JVM_OPTS -jar /service.jar $ARGS \ No newline at end of file diff --git a/movie-database-movies/Dockerfile b/movie-database-movies/Dockerfile new file mode 100644 index 0000000..477cc2a --- /dev/null +++ b/movie-database-movies/Dockerfile @@ -0,0 +1,19 @@ +FROM java:7 +MAINTAINER Dennis Schulte + +# install updates +RUN apt-get update && \ + apt-get clean && \ + rm -rf /var/lib/apt/* + +# default log directory +#VOLUME ["/data/log"] + +# copy spring boot jar as 'service.jar' +COPY target/movie-database-*.jar /service.jar + +# service port +EXPOSE 8080 + +# define default jar name +ENTRYPOINT java $JVM_OPTS -jar /service.jar $ARGS \ No newline at end of file diff --git a/movie-database-movies/src/main/resources/application.properties b/movie-database-movies/src/main/resources/application.properties index 6e69b78..9a00064 100644 --- a/movie-database-movies/src/main/resources/application.properties +++ b/movie-database-movies/src/main/resources/application.properties @@ -1,12 +1,12 @@ spring.application.name=@pom.artifactId@ info.version=@pom.version@ info.stage=test -moviedatabase.navigation.url.base=http://localhost:8081 +moviedatabase.navigation.url.base=http://navigation:8081 moviedatabase.actors.url.base=http://moviedatabase.com/actor-app spring.thymeleaf.cache=false -spring.redis.host=192.168.59.103 +spring.redis.host=redis server.context-path=/movie-app -spring.boot.admin.url=http://localhost:8083 +spring.boot.admin.url=http://monitoring:8083 spring.boot.admin.client.url=http://moviedatabase.com/movie-app http.mappers.jsonPrettyPrint=true logging.file=/opt/moviedatabase/logs/@pom.artifactId@/@pom.artifactId@.log \ No newline at end of file diff --git a/movie-database-navigation/Dockerfile b/movie-database-navigation/Dockerfile new file mode 100644 index 0000000..6e22367 --- /dev/null +++ b/movie-database-navigation/Dockerfile @@ -0,0 +1,20 @@ +#FROM registry.hub.docker.com/_/java/:oracle-java7 +FROM java:7 +MAINTAINER Dennis Schulte + +# install updates +RUN apt-get update && \ + apt-get clean && \ + rm -rf /var/lib/apt/* + +# default log directory +#VOLUME ["/data/log"] + +# copy spring boot jar as 'service.jar' +COPY target/movie-database-*.jar /service.jar + +# service port +EXPOSE 8081 + +# define default jar name +ENTRYPOINT java $JVM_OPTS -jar /service.jar $ARGS \ No newline at end of file diff --git a/movie-database-navigation/src/main/resources/application.properties b/movie-database-navigation/src/main/resources/application.properties index a114cbd..fd29d3e 100644 --- a/movie-database-navigation/src/main/resources/application.properties +++ b/movie-database-navigation/src/main/resources/application.properties @@ -6,7 +6,7 @@ moviedatabase.movies.url.base=http://moviedatabase.com/movie-app moviedatabase.actors.url.base=http://moviedatabase.com/actor-app moviedatabase.shop.url.base=http://moviedatabase.com/shop-app/ spring.thymeleaf.cache=false -spring.boot.admin.url=http://localhost:8083 +spring.boot.admin.url=http://monitoring:8083 spring.boot.admin.client.url=http://moviedatabase.com/navigation-app http.mappers.jsonPrettyPrint=true logging.file=/opt/moviedatabase/logs/@pom.artifactId@/@pom.artifactId@.log diff --git a/movie-database-shop-app/Dockerfile b/movie-database-shop-app/Dockerfile new file mode 100644 index 0000000..718ed6a --- /dev/null +++ b/movie-database-shop-app/Dockerfile @@ -0,0 +1,16 @@ +FROM nginx:1.7.9 +MAINTAINER Dennis Schulte + +RUN rm /etc/nginx/conf.d/* + +COPY moviedatabase.conf /etc/nginx/conf.d/moviedatabase.conf +COPY dist/. /usr/share/nginx/html/shop-app/ + +COPY run.sh /usr/local/bin/ + +RUN chmod 755 /usr/local/bin/run.sh + +# service port +EXPOSE 80 + +ENTRYPOINT ["/usr/local/bin/run.sh"] \ No newline at end of file diff --git a/movie-database-shop-app/moviedatabase.conf b/movie-database-shop-app/moviedatabase.conf new file mode 100644 index 0000000..c578622 --- /dev/null +++ b/movie-database-shop-app/moviedatabase.conf @@ -0,0 +1,35 @@ +server { + listen 80; + server_name moviedatabase.com; + + location / { + proxy_set_header Host $http_host; + proxy_pass http://movies:8080/movie-app/; + } + + location /movie-app/ { + proxy_set_header Host $http_host; + proxy_pass http://movies:8080/movie-app/; + } + + location /actor-app/ { + proxy_set_header Host $http_host; + proxy_pass http://actors:8082/actor-app/; + } + + location /shop-rest/ { + proxy_set_header Host $http_host; + proxy_pass http://shop:8084/shop-rest/; + } + + location /navigation-app/ { + proxy_set_header Host $http_host; + proxy_pass http://navigation:8081/; + } + + location /shop-app/ { + root /usr/share/nginx/html; + ssi on; + } + +} diff --git a/movie-database-shop-app/run.sh b/movie-database-shop-app/run.sh new file mode 100644 index 0000000..bde346e --- /dev/null +++ b/movie-database-shop-app/run.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +# start nginx +nginx -g "daemon off;" diff --git a/movie-database-shop-rest/Dockerfile b/movie-database-shop-rest/Dockerfile new file mode 100644 index 0000000..5485482 --- /dev/null +++ b/movie-database-shop-rest/Dockerfile @@ -0,0 +1,19 @@ +FROM java:7 +MAINTAINER Dennis Schulte + +# install updates +RUN apt-get update && \ + apt-get clean && \ + rm -rf /var/lib/apt/* + +# default log directory +#VOLUME ["/data/log"] + +# copy spring boot jar as 'service.jar' +COPY target/movie-database-*.jar /service.jar + +# service port +EXPOSE 8084 + +# define default jar name +ENTRYPOINT java $JVM_OPTS -jar /service.jar $ARGS \ No newline at end of file diff --git a/movie-database-shop-rest/src/main/resources/application.properties b/movie-database-shop-rest/src/main/resources/application.properties index 2eaa408..ee4a3fc 100644 --- a/movie-database-shop-rest/src/main/resources/application.properties +++ b/movie-database-shop-rest/src/main/resources/application.properties @@ -2,9 +2,9 @@ spring.application.name=@pom.artifactId@ info.version=@pom.version@ info.stage=test server.port=8084 -spring.redis.host=192.168.59.103 +spring.redis.host=redis server.context-path=/shop-rest -spring.boot.admin.url=http://localhost:8083 +spring.boot.admin.url=http://monitoring:8083 spring.boot.admin.client.url=http://moviedatabase.com/shop-rest http.mappers.jsonPrettyPrint=true logging.file=/opt/moviedatabase/logs/@pom.artifactId@/@pom.artifactId@.log diff --git a/shutdown.sh b/shutdown.sh index 33e0c19..1914eb5 100755 --- a/shutdown.sh +++ b/shutdown.sh @@ -1,3 +1,3 @@ -boot2docker stop +pkill -f redis-server sudo nginx -s stop pkill -f movie-database diff --git a/buildAndStartup.sh b/startup.sh similarity index 70% rename from buildAndStartup.sh rename to startup.sh index ebc0a43..f71d977 100755 --- a/buildAndStartup.sh +++ b/startup.sh @@ -1,18 +1,13 @@ -boot2docker stop +#!/usr/bin/env bash +echo "Stopping all services..." sudo nginx -s stop pkill -f movie-database -mvn clean install -cd movie-database-shop-app -bower install -grunt -rm -rf /opt/moviedatabase/shop-app -cp -R dist/. /opt/moviedatabase/shop-app -cd .. +pkill -f redis-server +echo "Starting HTTP Server..." sudo nginx -boot2docker init -boot2docker start -eval "$(boot2docker shellinit)" -dock redis +echo "Starting Redis Server..." +redis-server 2>&1 & +echo "Starting Java Apps..." java -jar movie-database-monitoring/target/movie-database-monitoring-0.0.1-SNAPSHOT.jar > /dev/null 2>&1 & java -jar movie-database-navigation/target/movie-database-navigation-0.0.1-SNAPSHOT.jar > /dev/null 2>&1 & java -jar movie-database-movies/target/movie-database-movies-0.0.1-SNAPSHOT.jar > /dev/null 2>&1 & diff --git a/vagrant-etc-hosts b/vagrant-etc-hosts new file mode 100644 index 0000000..5a3b4dd --- /dev/null +++ b/vagrant-etc-hosts @@ -0,0 +1,7 @@ +127.0.0.1 localhost +127.0.0.1 redis +127.0.0.1 monitoring +127.0.0.1 navigation +127.0.0.1 actors +127.0.0.1 movies +127.0.0.1 shop \ No newline at end of file