Skip to content

Commit

Permalink
(ops) build Docker image via Jib
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanlermitage committed Sep 16, 2018
1 parent cf5f205 commit de7335b
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 24 deletions.
42 changes: 42 additions & 0 deletions DEPLOY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
## How do deploy and run application

This document will help you to run application.

### Manual run

* Install **JDK8** and **MongoDB 3.4.x**. MongoDB should listen on port 27017, accept username `root` and password `woot` on the `manondev` database (authentication and data). See `src/main/resources/application-dev.yml` for details.
* Package and run application via `do rd`. Application will start on port 8080 with `dev` Spring profile.
* To run with another Spring profile (e.g. `prod`), package application via `do p`, go to `target/` directory and run `java -jar -Xms128m -Xmx512m -Dspring.profiles.active=prod,metrics -Dfile.encoding=UTF-8 -Djava.awt.headless=true manon.jar`.

### Docker

* Install **Docker** Community Edition:
```bash
# tested on Lubuntu 18.04 LTS
sudo apt-get remove docker docker-engine docker.io
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update && sudo apt-get install docker-ce
sudo groupadd docker && sudo usermod -aG docker jon # replace 'jon' by your user name (see whoami)
sudo systemctl enable docker
```
* Run a **MongoDB 3.4.x** docker image:
```bash
# MongoDB 3.4.17
# find tags at https://github.com/docker-library/docs/blob/master/mongo/README.md
mkdir ~/data
docker run -d --net=host -p 27017:27017 -v ~/data:/data/mongodb mongo:3.4.17-jessie
# optional: install MongoDB command-line client and check connectivity
sudo apt-get install mongodb-clients
mongo localhost/manon
```
* Build and install application image via `do jib`.
* Then run **application image**:
```bash
mkdir ~/logs
docker run -d --name lermitage-manon --net=host -p 8080:8080 -v ~/logs:/logs lermitage-manon:1.0.0-SNAPSHOT
```
* To change Spring profiles, e.g. with `dev` profile, add `-e "SPRING_PROFILES_ACTIVE=dev"` to the `docker run` command.
* check connectivity by visiting `http://localhost:8080/actuator/health` (default login/password is `ROOT/woot`).
* MongoDB data is persisted in `~/data` and applications logs are in `~/logs`.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ Some experimentation with Spring Boot 2, JDK8+, NoSQL, etc. It demonstrates usag
* some **AOP** to capture performance of API endpoints
* Spring **Actuator** web endpoints configured
* **Swagger UI** to provide documentation about REST API endpoints
* Reproducible **Docker** builds without Docker daemon thanks to **[Jib](https://github.com/GoogleContainerTools/jib)**. Linux base image is [Distroless](https://github.com/GoogleContainerTools/distroless)

For fun and to show some skills :cat:

## Author

Jonathan Lermitage (<jonathan.lermitage@gmail.com>)
**Your're Canadian or American and wanna hire an experienced French devops? Contact me! I'm okay to move.**
Linkedin profile: [jonathan-lermitage-092711142](https://www.linkedin.com/in/jonathan-lermitage-092711142/)

## Branches
Expand All @@ -42,6 +42,7 @@ Linkedin profile: [jonathan-lermitage-092711142](https://www.linkedin.com/in/jon
* [spring5](https://github.com/jonathanlermitage/manon/tree/spring5): based on Spring Framework 5, **Spring Boot 2** and JDK8/9, use Spring Web (REST API), Security, Data (regular and embedded MongoDB), Batch, Cache (Redis), AOP, Actuator, Swagger UI
* **[spring5-light](https://github.com/jonathanlermitage/manon/tree/spring5-light)**: like [spring5](https://github.com/jonathanlermitage/manon/tree/spring5), JDK8+, without Spring Batch, Redis Cache, Swagger UI
* archived
* [spring5-light-docker-jib](https://github.com/jonathanlermitage/manon/tree/spring5-light-docker-jib): Docker build thanks to Jib. See [DEPLOY.md](./DEPLOY.md) to package and run application
* [spring5-embedmongo](https://github.com/jonathanlermitage/manon/tree/spring5-embedmongo): use **embedded MongoDB** during tests. See commits [37e1be5](https://github.com/jonathanlermitage/manon/commit/37e1be5f01c3ffa6ecf4d9c3e558b4ffb297227f) and [161d321](https://github.com/jonathanlermitage/manon/commit/161d3214ab72e76a2f041bbe8914077137513fb7)
* [spring5-swagger](https://github.com/jonathanlermitage/manon/tree/spring5-swagger): enable **Swagger UI**. Run application and check `http://localhost:8080/swagger-ui.html`, authenticate with `ROOT` / `woot`. See commit [429ae53](https://github.com/jonathanlermitage/manon/commit/429ae53bc5211d8d97e8ccca20a4b183f207c6ee)
* [spring5-redis](https://github.com/jonathanlermitage/manon/tree/spring5-redis): enable **Redis cache**, and prefer embedded cache during tests. See commits [a911f6a](https://github.com/jonathanlermitage/manon/commit/a911f6a08ce67b3b302f4ea3d17a73e8a0dcd6e6), [7e26822](https://github.com/jonathanlermitage/manon/commit/7e268222a745e5bbb88129d99b91379bafac7f58) and [ae6e0e6](https://github.com/jonathanlermitage/manon/commit/ae6e0e69ac37dbe44b51f449600943e09b9b149b)
Expand Down Expand Up @@ -71,6 +72,8 @@ do w 3.5.2 set or upgrade Maven wrapper to 3.5.2
do cv check plugins and dependencies versions
do uv update plugins and dependencies versions
do dt show dependencies tree
do jib build Docker image to a Docker daemon
do jibtar build and save Docker image to a tarball
```

## License
Expand Down
34 changes: 22 additions & 12 deletions do.cmd
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
@echo off

if [%1] == [help] (
echo t: test
echo tc: test and generate coverage data
echo sc: compute and upload Sonar analysis to SonarCloud
echo tsc: similar to "do tc" then "do sc"
echo b: compile
echo c: clean
echo p: package
echo rd: package and run application with dev profile
echo w $V: set or upgrade Maven wrapper to version $V
echo cv: check plugins and dependencies versions
echo uv: update plugins and dependencies versions
echo dt: show dependencies tree
echo t: test
echo tc: test and generate coverage data
echo sc: compute and upload Sonar analysis to SonarCloud
echo tsc: similar to "do tc" then "do sc"
echo b: compile
echo c: clean
echo p: package
echo rd: package and run application with dev profile
echo w $V: set or upgrade Maven wrapper to version $V
echo cv: check plugins and dependencies versions
echo uv: update plugins and dependencies versions
echo dt: show dependencies tree
echo jib: build Docker image to a Docker daemon
echo jibtar: build and save Docker image to a tarball
)

if [%1] == [t] (
Expand Down Expand Up @@ -70,3 +72,11 @@ if [%1] == [tsc] (
echo mvnw clean test sonar:sonar -Pcoverage -Dsonar.organization=%TK1_MANON_SONAR_ORGA% -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=%TK1_MANON_SONAR_LOGIN%
mvnw clean test sonar:sonar -Pcoverage -Dsonar.organization=%TK1_MANON_SONAR_ORGA% -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=%TK1_MANON_SONAR_LOGIN%
)
if [%1] == [jib] (
echo mvnw clean compile jib:dockerBuild -DskipTests -P jib
mvnw clean compile jib:dockerBuild -DskipTests -P jib
)
if [%1] == [jibtar] (
echo mvnw clean compile jib:buildTar -DskipTests -P jib
mvnw clean compile jib:buildTar -DskipTests -P jib
)
34 changes: 23 additions & 11 deletions do.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
case "$1" in

"help")
echo "t: test"
echo "tc: test and generate coverage data"
echo "sc: compute and upload Sonar analysis to SonarCloud"
echo "tsc: similar to \"do tc\" then \"do sc\""
echo "b: compile"
echo "c: clean"
echo "p: package"
echo "rd: package and run application with dev profile"
echo "t: test"
echo "tc: test and generate coverage data"
echo "sc: compute and upload Sonar analysis to SonarCloud"
echo "tsc: similar to \"do tc\" then \"do sc\""
echo "b: compile"
echo "c: clean"
echo "p: package"
echo "rd: package and run application with dev profile"
echo "w \$V: set or upgrade Maven wrapper to version \$V"
echo "cv: check plugins and dependencies versions"
echo "uv: update plugins and dependencies versions"
echo "dt: show dependencies tree"
echo "cv: check plugins and dependencies versions"
echo "uv: update plugins and dependencies versions"
echo "dt: show dependencies tree"
echo "jib: build Docker image to a Docker daemon"
echo "jibtar: build and save Docker image to a tarball"
;;

"t")
Expand Down Expand Up @@ -84,4 +86,14 @@ case "$1" in
sh ./mvnw clean test sonar:sonar -Pcoverage -Dsonar.organization=$TK1_MANON_SONAR_ORGA -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=$TK1_MANON_SONAR_LOGIN
;;

"jib")
echo "sh ./mvnw clean compile jib:dockerBuild -DskipTests -P jib"
sh ./mvnw clean compile jib:dockerBuild -DskipTests -P jib
;;

"jibtar")
echo "sh ./mvnw clean compile jib:buildTar -DskipTests -P jib"
sh ./mvnw clean compile jib:buildTar -DskipTests -P jib
;;

esac
38 changes: 38 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,44 @@
</plugins>
</build>
</profile>
<profile>
<id>jib</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<!-- https://github.com/GoogleContainerTools/jib/tree/master/jib-maven-plugin -->
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>0.9.10</version>
<configuration>
<from>
<!-- https://console.cloud.google.com/gcr/images/distroless/GLOBAL/java?gcrImageListsize=10 -->
<!-- latest Distroless version on August 8, 2018 at 5:26:31 PM UTC+2 -->
<image>gcr.io/distroless/java@sha256:b430543bea1d8326e767058bdab3a2482ea45f59d7af5c5c61334cd29ede88a1</image>
</from>
<to>
<image>lermitage-manon:${project.version}</image>
</to>
<container>
<jvmFlags>
<jvmFlag>-Dfile.encoding=UTF-8</jvmFlag>
<jvmFlag>-Djava.awt.headless=true</jvmFlag>
<jvmFlag>-Dserver.port=8080</jvmFlag>
<jvmFlag>-Dspring.profiles.active=docker,metrics</jvmFlag>
</jvmFlags>
<mainClass>${start-class}</mainClass>
<ports>
<port>8080</port>
</ports>
</container>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<!--profile>
<id>java11</id>
<properties>
Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/application-docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
### DOCKER CONTAINER CONFIGURATION ### (warning: avoid tabs, indent with two spaces)

spring:
data:
mongodb:
host: localhost
port: 27017
database: manon

0 comments on commit de7335b

Please sign in to comment.