From de7335b2be850ca6a7b683bdbe2b86adc990b594 Mon Sep 17 00:00:00 2001 From: Jonathan Lermitage Date: Mon, 17 Sep 2018 00:42:49 +0200 Subject: [PATCH] (ops) build Docker image via Jib --- DEPLOY.md | 42 +++++++++++++++++++++++ README.md | 5 ++- do.cmd | 34 +++++++++++------- do.sh | 34 ++++++++++++------ pom.xml | 38 ++++++++++++++++++++ src/main/resources/application-docker.yml | 8 +++++ 6 files changed, 137 insertions(+), 24 deletions(-) create mode 100644 DEPLOY.md create mode 100644 src/main/resources/application-docker.yml diff --git a/DEPLOY.md b/DEPLOY.md new file mode 100644 index 00000000..8c00b336 --- /dev/null +++ b/DEPLOY.md @@ -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`. diff --git a/README.md b/README.md index 7920489c..5eccc8de 100644 --- a/README.md +++ b/README.md @@ -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 () -**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 @@ -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) @@ -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 diff --git a/do.cmd b/do.cmd index 616c1655..e0f94162 100644 --- a/do.cmd +++ b/do.cmd @@ -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] ( @@ -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 +) diff --git a/do.sh b/do.sh index 3a866d16..23aa030a 100644 --- a/do.sh +++ b/do.sh @@ -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") @@ -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 diff --git a/pom.xml b/pom.xml index 340da6f2..3e933a66 100644 --- a/pom.xml +++ b/pom.xml @@ -297,6 +297,44 @@ + + jib + + false + + + + + + com.google.cloud.tools + jib-maven-plugin + 0.9.10 + + + + + gcr.io/distroless/java@sha256:b430543bea1d8326e767058bdab3a2482ea45f59d7af5c5c61334cd29ede88a1 + + + lermitage-manon:${project.version} + + + + -Dfile.encoding=UTF-8 + -Djava.awt.headless=true + -Dserver.port=8080 + -Dspring.profiles.active=docker,metrics + + ${start-class} + + 8080 + + + + + + +