Skip to content

Commit

Permalink
Build with Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
FernandoAlvarez28 committed Aug 10, 2023
1 parent 285c4ea commit b260130
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 9 deletions.
8 changes: 8 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
**/README.md
.*
docker
docs
Makefile
LICENSE
gradle
build
18 changes: 18 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Recommended: https://snyk.io/blog/best-practices-to-build-java-containers-with-docker/

# https://hub.docker.com/_/gradle
FROM gradle:8.2.1-jdk17-alpine AS build
RUN mkdir /project
COPY . /project
WORKDIR /project
RUN gradle build --no-daemon

# https://hub.docker.com/r/bellsoft/liberica-runtime-container
FROM bellsoft/liberica-runtime-container:jre-17-slim-musl
RUN mkdir /app
RUN addgroup --system javauser && adduser -S -s /bin/false -G javauser javauser
COPY --from=build /project/build/libs/kotlincrud-0.0.1-SNAPSHOT.jar /app/application.jar
WORKDIR /app
RUN chown -R javauser:javauser /app
USER javauser
CMD "java" "-jar" "application.jar"
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
all: build-docker-image run-docker-image

build-docker-image:
docker build -t kotlincrud-postgres:latest .

run-docker-image:
cd docker; \
docker-compose up kotlincrud-postgres
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ Small project to experiment and learn Kotlin and other libraries.
There is also a CRUD with MongoDB: https://github.com/FernandoAlvarez28/kotlin-crud-mongodb

## Requirements
- **Java 17**.
- A [Kotlin/Gradle capable IDE](https://kotlinlang.org/docs/kotlin-ide.html).
- [docker-compose](https://docs.docker.com/compose/).
- **Java 17** and a [Kotlin/Gradle capable IDE](https://kotlinlang.org/docs/kotlin-ide.html).
- Or [Docker](https://www.docker.com/) and [docker-compose](https://docs.docker.com/compose/).

## Contains/Uses
- **Java 17**.
Expand All @@ -21,6 +20,7 @@ There is also a CRUD with MongoDB: https://github.com/FernandoAlvarez28/kotlin-c
- Load test with **K6** via Docker.

## How to run
### Locally
1. Clone this repository.
2. Import this project on your [Kotlin capable IDE](https://kotlinlang.org/docs/kotlin-ide.html), like [IntelliJ](https://www.jetbrains.com/idea/).
3. Run the [dockerized Postgres](docker/README.md#postgres) database (or you can use your own Postgres instance).
Expand All @@ -30,3 +30,20 @@ There is also a CRUD with MongoDB: https://github.com/FernandoAlvarez28/kotlin-c
1. Use the available [Postman collection](docs/fernandos-kotlin-crud.postman_collection.json) + [environment](docs/fernandos-kotlin-crud.postman_environment.json).
2. Use the login endpoint to generate a JWT token; it will be save on your active environment and used by the other endpoints.

### Via Docker
1. Clone this repository.
2. Build the [Dockerfile](Dockerfile) and run the Docker image with its dependencies using one of the methods:
- [Makefile](Makefile):
```shell
$ make
```
- [Docker compose](docker/docker-compose.yml):
```shell
./docker$ docker-compose up --build kotlincrud-postgres
```
- More details at [docker/README.md#kotlincrud-postgres](docker/README.md#kotlincrud-postgres).
6. Run [KotlincrudApplication.kt](src/main/kotlin/alvarez/fernando/kotlincrud/KotlincrudApplication.kt).
7. Access the endpoints exposed at http://localhost:8080.
1. Use the available [Postman collection](docs/fernandos-kotlin-crud.postman_collection.json) + [environment](docs/fernandos-kotlin-crud.postman_environment.json).
2. Use the login endpoint to generate a JWT token; it will be save on your active environment and used by the other endpoints.

13 changes: 13 additions & 0 deletions docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@
Some services that are used by this application are served via [Docker](https://www.docker.com/) to make easy
running without having to install everything locally.

## kotlincrud-postgres
The image of this application itself. There is a [Dockerfile](../Dockerfile) to build it, and it's defined in the [docker-compose](docker-compose.yml) along with its dependencies ([Mockoon](#mockoon) and [Postgres](#postgres)).

### How to run
1. Build and run the image with one of the following commands:
- [./build-start-application.sh](./build-start-application.sh) script from your terminal.
- Or manually via [docker-compose](https://docs.docker.com/compose/):
```shell
$ docker-compose up --build kotlincrud-postgres
```
- It will also run [Mockoon](#mockoon) and [Postgres](#postgres) that are explained below.
- [Makefile](../Makefile) that basically does the same thing as above.

## Mockoon
It's a small server that mocks REST APIs with custom data and configurations from a YAML file.
It has a GUI software to guide building this configuration file and also a CLI and Docker image.
Expand Down
1 change: 1 addition & 0 deletions docker/build-start-application.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker-compose up --build kotlincrud-postgres
25 changes: 21 additions & 4 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,29 @@ version: '3.4'

services:

kotlincrud-postgres:
container_name: kotlincrud-postgres
image: kotlincrud-postgres:latest
build:
dockerfile: Dockerfile
context: ./../
environment:
- APP_DATABASE_HOST=postgres
- APP_DATABASE_PORT=5432
- APP_DATABASE_NAME=kotlin_crud
- APP_DATABASE_SCHEMA=kotlin_crud
- APP_DATABASE_USERNAME=postgres
- APP_DATABASE_PASSWORD=blue-elephant
- USERS_API_MOCKOON_URL=http://mockoon:8180
depends_on:
- mockoon
- postgres
ports:
- "8080:8080"

mockoon:
image: mockoon/cli
command: -p 8180 -d /config/users-api.json
networks:
- mockoon
ports:
- "8180:8180"
volumes:
Expand All @@ -17,6 +35,7 @@ services:
environment:
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: "blue-elephant" #Postgres's logo is an elephant :P
POSTGRES_DB: "kotlin_crud"
ports:
- "65432:5432" #Different port to not conflict with any possible real/default Postgres installation

Expand Down Expand Up @@ -64,8 +83,6 @@ services:
- "host.docker.internal:host-gateway"

networks:
mockoon:
driver: bridge
k6:
driver: bridge
grafana:
1 change: 1 addition & 0 deletions docker/start-application-dependencies.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker-compose up mockoon postgres
1 change: 1 addition & 0 deletions docker/start-application.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker-compose up kotlincrud-postgres
6 changes: 4 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
spring.application.name=kotlin-crud

app.database.url=localhost:65432
app.database.name=postgres
app.database.host=localhost
app.database.port=65432
app.database.url=${app.database.host}:${app.database.port}
app.database.name=kotlin_crud
app.database.username=postgres
app.database.password=blue-elephant
app.database.schema=kotlin_crud
Expand Down

0 comments on commit b260130

Please sign in to comment.