Skip to content

Commit

Permalink
Demo 2: Docker-compose
Browse files Browse the repository at this point in the history
  • Loading branch information
ilopmar committed Nov 21, 2016
1 parent 5fbfe21 commit 9b20dc4
Show file tree
Hide file tree
Showing 15 changed files with 248 additions and 36 deletions.
19 changes: 19 additions & 0 deletions builder.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

DOCKER_USER_HOME=/home/user
CACHED_VOLUMES="m2 groovy grails gradle"

function get_cached_volumes {
volumes_str=""
for v in $CACHED_VOLUMES; do
volumes_str+="-v $HOME/.$v:$DOCKER_USER_HOME/.$v "
done
echo $volumes_str
}

docker run -it --rm \
-v `pwd`:$DOCKER_USER_HOME/work \
`get_cached_volumes` \
--entrypoint /home/user/entrypoint.sh \
-v /var/run/docker.sock:/var/run/docker.sock \
reverse-app/builder $1
7 changes: 6 additions & 1 deletion consumer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ group "consumer"

apply plugin:"eclipse"
apply plugin:"idea"
apply plugin:"war"
apply plugin:"org.grails.grails-web"
apply plugin:"org.grails.plugins.views-json"

Expand Down Expand Up @@ -68,4 +67,10 @@ dependencies {

bootRun {
jvmArgs = ['-Dspring.output.ansi.enabled=always']
}

task prepareDocker(type: Copy, dependsOn: build) {
from file('src/main/docker/Dockerfile')
from project.jar
into "${buildDir}/docker"
}
32 changes: 25 additions & 7 deletions consumer/grails-app/conf/application.groovy
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
environments {
development {
rabbitmq {
connections = [
[
host : 'localhost',
username: 'guest',
password: 'guest'
]
]
}
}
production {
rabbitmq {
connections = [
[
host : 'rabbitmq',
username: 'guest',
password: 'guest'
]
]
}
}
}

rabbitmq {
connections = [
[
host: 'localhost',
username: 'guest',
password: 'guest'
]
]
queues = [
[
name: 'reverse',
Expand Down
24 changes: 4 additions & 20 deletions consumer/grails-app/conf/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,7 @@ environments:
url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
production:
dataSource:
dbCreate: none
url: jdbc:h2:./prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
properties:
jmxEnabled: true
initialSize: 5
maxActive: 50
minIdle: 5
maxIdle: 25
maxWait: 10000
maxAge: 600000
timeBetweenEvictionRunsMillis: 5000
minEvictableIdleTimeMillis: 60000
validationQuery: SELECT 1
validationQueryTimeout: 3
validationInterval: 15000
testOnBorrow: true
testWhileIdle: true
testOnReturn: false
jdbcInterceptors: ConnectionState
defaultTransactionIsolation: 2 # TRANSACTION_READ_COMMITTED
dbCreate: create-drop
url: jdbc:postgresql://postgresql:5432/reverseapp
username: reverseuser
password: reversepass
14 changes: 14 additions & 0 deletions consumer/src/main/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM openjdk:8u111-jdk

VOLUME /tmp

RUN groupadd -r user && useradd -r -g user user

COPY consumer-0.1.jar /app/consumer.jar
RUN bash -c 'touch /app/consumer.jar'
RUN chown -R user:user /app

USER user
WORKDIR /app

CMD ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app/consumer.jar"]
27 changes: 27 additions & 0 deletions docker/02_docker-compose/builder/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FROM openjdk:8u111-jdk

# Locale setup
RUN apt-get update && \
apt-get install -yq locales ca-certificates wget sudo && \
rm -rf /var/lib/apt/lists/* && \
localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8
ENV LANG en_US.UTF-8
ENV LC_ALL en_US.UTF-8

# Docker
RUN echo 'deb http://httpredir.debian.org/debian jessie-backports main' > /etc/apt/sources.list.d/jessie-backports.list && \
apt-get update && apt-get install -yq docker.io && \
rm -rf /var/lib/apt/lists/*

# Add user
RUN useradd user -d /home/user -s /bin/bash && \
cp -r /etc/skel /home/user && \
chown user.user -R /home/user && \
echo "user ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers

USER user
WORKDIR /home/user

COPY files/entrypoint.sh /home/user/
COPY files/postgresql /home/user/postgresql
COPY files/rabbitmq /home/user/rabbitmq
3 changes: 3 additions & 0 deletions docker/02_docker-compose/builder/build-image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

docker build -t reverse-app/builder .
58 changes: 58 additions & 0 deletions docker/02_docker-compose/builder/files/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/bash

function build-postgresql {
echo "##########################"
echo "# Building postgresql... #"
echo "##########################"
sudo docker build -t reverse-app/postgresql ./postgresql/
}

function build-rabbitmq {
echo "########################"
echo "# Building rabbitmq... #"
echo "########################"
sudo docker build -t reverse-app/rabbitmq ./rabbitmq/
}


function build-producer {
echo "########################"
echo "# Building producer... #"
echo "########################"
(cd work/producer && ./gradlew clean build prepareDocker && \
sudo docker build -t reverse-app/producer ./build/docker/)
}

function build-consumer {
echo "########################"
echo "# Building consumer... #"
echo "########################"
(cd work/consumer && ./gradlew clean build prepareDocker && \
sudo docker build -t reverse-app/consumer ./build/docker/)
}


case $1 in
postgresql)
build-postgresql
;;
rabbitmq)
build-rabbitmq
;;
producer)
build-producer
;;
consumer)
build-consumer
;;
all)
build-postgresql
build-rabbitmq
build-producer
build-consumer
;;
*)
echo "USAGE: $0 [ postgresql | rabbitmq | producer | consumer | all ]"
exit 1
;;
esac
7 changes: 7 additions & 0 deletions docker/02_docker-compose/builder/files/postgresql/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM postgres:9.4.9

VOLUME /var/lib/postgresql/data

COPY ["setup.sh", "/docker-entrypoint-initdb.d/"]

EXPOSE 5432
15 changes: 15 additions & 0 deletions docker/02_docker-compose/builder/files/postgresql/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
echo "######### CREATING DATABASE ##########"

# Perform all actions as user 'postgres'
export PGUSER=postgres

# Create the reverse-app user and database
psql <<EOSQL
CREATE DATABASE reverseapp;
CREATE ROLE reverseuser WITH LOGIN PASSWORD 'reversepass';
GRANT ALL PRIVILEGES ON DATABASE reverseapp TO reverseuser;
EOSQL

echo ""
echo "######### DATABASE CREATED ##########"
5 changes: 5 additions & 0 deletions docker/02_docker-compose/builder/files/rabbitmq/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM rabbitmq:3.6.5-management

RUN rabbitmq-plugins enable --offline rabbitmq_management

EXPOSE 15672
20 changes: 20 additions & 0 deletions docker/02_docker-compose/docker-compose/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
version: '2'
services:
postgresql:
image: reverse-app/postgresql

rabbitmq:
image: reverse-app/rabbitmq

producer:
image: reverse-app/producer
ports:
- "8080:8080"
links:
- rabbitmq:rabbitmq

consumer:
image: reverse-app/consumer
links:
- rabbitmq:rabbitmq
- postgresql:postgresql
7 changes: 6 additions & 1 deletion producer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ group "producer"

apply plugin:"eclipse"
apply plugin:"idea"
apply plugin:"war"
apply plugin:"org.grails.grails-web"
apply plugin:"org.grails.grails-gsp"
apply plugin:"asset-pipeline"
Expand Down Expand Up @@ -65,3 +64,9 @@ assets {
minifyJs = true
minifyCss = true
}

task prepareDocker(type: Copy, dependsOn: build) {
from file('src/main/docker/Dockerfile')
from project.jar
into "${buildDir}/docker"
}
32 changes: 25 additions & 7 deletions producer/grails-app/conf/application.groovy
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
environments {
development {
rabbitmq {
connections = [
[
host : 'localhost',
username: 'guest',
password: 'guest'
]
]
}
}
production {
rabbitmq {
connections = [
[
host : 'rabbitmq',
username: 'guest',
password: 'guest'
]
]
}
}
}

rabbitmq {
connections = [
[
host: 'localhost',
username: 'guest',
password: 'guest'
]
]
exchanges = [
[
name: 'exchange.reverse',
Expand Down
14 changes: 14 additions & 0 deletions producer/src/main/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM openjdk:8u111-jdk

VOLUME /tmp

RUN groupadd -r user && useradd -r -g user user

COPY producer-0.1.jar /app/producer.jar
RUN bash -c 'touch /app/producer.jar'
RUN chown -R user:user /app

USER user
WORKDIR /app

CMD ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app/producer.jar"]

0 comments on commit 9b20dc4

Please sign in to comment.