diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..906c067 --- /dev/null +++ b/.env.example @@ -0,0 +1,3 @@ +POSTGRES_USER=postgres +POSTGRES_PASSWORD=postgres +POSTGRES_DB=orservice_db \ No newline at end of file diff --git a/.gitignore b/.gitignore index ab35d1a..7393da8 100644 --- a/.gitignore +++ b/.gitignore @@ -36,4 +36,7 @@ build/ mvnw mvnw.cmd -postgres-data \ No newline at end of file +postgres-data + +### env ### +.env \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index dd91fe3..b65ec2f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,14 @@ +# Build stage +FROM maven:3.8.4-openjdk-17-slim AS build +WORKDIR /app +COPY pom.xml . +RUN mvn dependency:go-offline +COPY src ./src +RUN mvn clean package -DskipTests + +# Runtime stage FROM openjdk:17-jdk-alpine -COPY target/orservice-0.0.1-SNAPSHOT.jar app-1.0.0.jar +WORKDIR /app +COPY --from=build /app/target/orservice-0.0.1-SNAPSHOT.jar app-1.0.0.jar +EXPOSE 8080 ENTRYPOINT [ "java", "-jar", "app-1.0.0.jar", "-Dspring.profiles.active=prod" ] diff --git a/README.md b/README.md index 45e920c..21d0d00 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,53 @@ Spring Boot REST Service to provide a REST API replacement to current Sarapis OR Service. ## To run the database +Copy the environment variables from the `.env` file. +```bash +cp .env.example .env +``` + +Start the db container with the following command ```bash docker-compose up ``` +In your terminal you will find the logs of the database. If you see +the following log the database has started gracefully. +``` +LOG: database system is ready to accept connections +``` + +To shut down the container database use +```bash +docker-compose down +``` ## To run the service +Build the Java service. This command will generate the application `jar` inside +the `target` directory. +```bash +mvn clean install +``` +If the build is successful, then run the service ```bash java -jar target/orservice-0.0.1-SNAPSHOT.jar +``` +You will start seeing logs from the service booting up. If you see the following log +``` +Started OrserviceApplication in {} seconds (process running for {}) +``` +the service has started successfully. + +## Troubleshooting +If the db has not started successfully do the following: +1. Check that the container is running +```bash +docker ps +``` +You should see all the containers running in your machine. + +2. Try rebuilding the database +```bash +docker-compose down --volumes +rm -rf postgres_data +docker-compose up --build ``` \ No newline at end of file diff --git a/docker-compose.prod.yaml b/docker-compose.prod.yaml new file mode 100644 index 0000000..0de08a3 --- /dev/null +++ b/docker-compose.prod.yaml @@ -0,0 +1,46 @@ +services: + db: + container_name: orservice_db + env_file: ".env" + restart: on-failure + image: postgres:latest + ports: + - "5432:5432" + networks: + orservice: + - ipv4_address: 10.10.2.2 + environment: + - POSTGRES_USER=${POSTGRES_USER} + - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} + - POSTGRES_DB=${POSTGRES_DB} + volumes: + - ./postgres-data:/var/lib/postgresql/data + - ./initdb:/docker-entrypoint-initdb.d + logging: + options: + max-size: "10m" + max-file: "3" + healthcheck: + test: + ["CMD", "pg_isready", "-U", "${POSTGRES_USER}", "-d", "${POSTGRES_DB}"] + interval: 10s + timeout: 5s + retries: 5 + wt: + image: containrrr/watchtower + labels: + - "com.centurylinklabs.watchtower.scope=orservice" + volumes: + - /var/run/docker.sock:/var/run/docker.sock + command: --interval 30 --scope orservice + +volumes: + postgres-data: + +networks: + orservice: + driver: bridge + ipam: + config: + - subnet: 10.10.2.0/24 + gateway: 10.10.2.1 \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index 4291782..a176ed9 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -5,10 +5,11 @@ services: image: postgres:latest ports: - "5432:5432" + env_file: ".env" environment: - - POSTGRES_USER=postgres - - POSTGRES_PASSWORD=postgres - - POSTGRES_DB=orservice_db + - POSTGRES_USER=${POSTGRES_USER} + - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} + - POSTGRES_DB=${POSTGRES_DB} volumes: - ./postgres-data:/var/lib/postgresql/data - ./initdb:/docker-entrypoint-initdb.d