diff --git a/.gitignore b/.gitignore index 58bf788..700f3db 100644 --- a/.gitignore +++ b/.gitignore @@ -54,10 +54,6 @@ typings/ # Output of 'npm pack' *.tgz -# dotenv environment variables file -.env -.env.test - # parcel-bundler cache (https://parceljs.org/) .cache @@ -100,3 +96,6 @@ snyk.sarif # Jetbrains IntelliJ IDEA .idea/ + +# Environment variables file +*.env diff --git a/Dockerfile b/Dockerfile index e7f0b00..98a9e61 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,14 +5,16 @@ WORKDIR /usr/share/service/ RUN mkdir -p /usr/share/service/config RUN mkdir -p /usr/share/service/dump RUN mkdir -p /usr/share/service/public +RUN mkdir -p /usr/share/service/artifacts ENV LANG en_CA.UTF-8 ENV LANGUAGE en_CA.UTF-8 ENV LC_ALL en_CA.UTF-8 COPY InstallCert.java . +COPY HealthCheck.java . -COPY ./target/backend-start-api.jar /usr/share/service/service.jar +COPY ./target/backend-start-api.jar /usr/share/service/artifacts/backend-start-api.jar COPY dockerfile-entrypoint.sh /usr/share/service/dockerfile-entrypoint.sh RUN chmod -R g+w . && \ chmod g+x dockerfile-entrypoint.sh && \ diff --git a/HealthCheck.java b/HealthCheck.java new file mode 100644 index 0000000..95ae8a9 --- /dev/null +++ b/HealthCheck.java @@ -0,0 +1,24 @@ +import java.io.IOException; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse.BodyHandlers; + +public class HealthCheck { + + public static void main(String[] args) throws IOException, InterruptedException { + var client = HttpClient.newHttpClient(); + + var request = + HttpRequest.newBuilder() + .uri(URI.create("http://localhost:8090/actuator/health")) + .header("accept", "application/json") + .build(); + + var response = client.send(request, BodyHandlers.ofString()); + + if (response.statusCode() != 200 || !response.body().matches("^\\{\"status\":\"UP\".*")) { + throw new RuntimeException("Healthcheck failed"); + } + } +} diff --git a/README.md b/README.md index 6c7b8c0..6d33a61 100644 --- a/README.md +++ b/README.md @@ -102,9 +102,13 @@ docker run -p 8090:8090 \ However, if you have docker-compose you can do: ``` -docker-compose --env-file .env -f ./docker-compose.yml up --build --force-recreate --no-deps +docker-compose up --build --force-recreate --no-deps ``` +You'll need to provide the address of the DNS server to be used by the container in the environment +variable `DNS_ADDRESS`: you can find the address of the DNS server you're using right now using +[nslookup](https://en.wikipedia.org/wiki/Nslookup), for instance. + You can then check the API documentation accessing `localhost:8090/swagger-ui.html`. ## Getting help diff --git a/docker-compose-with-database.yml b/docker-compose-with-database.yml deleted file mode 100644 index 48e8c05..0000000 --- a/docker-compose-with-database.yml +++ /dev/null @@ -1,40 +0,0 @@ -version: '3.9' - -services: - database: - container_name: database - image: postgres:12 - environment: - POSTGRES_USER: default - POSTGRES_PASSWORD: default - POSTGRES_DB: default - hostname: database - restart: always - healthcheck: - test: ["CMD-SHELL", "pg_isready -U default"] - interval: 10s - timeout: 5s - retries: 5 - volumes: - - /pgdata - - backend: - depends_on: - database: - condition: service_healthy - container_name: service-api - image: service-api:latest - environment: - NODE_ENV: development - POSTGRESQL_HOST: database - POSTGRESQL_USER: default - POSTGRESQL_PASSWORD: default - POSTGRESQL_DATABASE: default - hostname: service-api - links: - - database - ports: - - "8090:8090" - build: - context: . - dockerfile: Dockerfile diff --git a/docker-compose.yml b/docker-compose.yml index 08e8e04..84b4413 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,8 +2,10 @@ version: '3.9' services: backend: - container_name: service-api - image: service-api:latest + build: + context: . + container_name: backend + dns: ${DNS_ADDRESS} environment: NRBESTAPI_VERSION: ${NRBESTAPI_VERSION} DATABASE_HOST: ${DATABASE_HOST} @@ -12,10 +14,13 @@ services: DATABASE_USER: ${DATABASE_USER} DATABASE_PASSWORD: ${DATABASE_PASSWORD} KEYCLOAK_REALM_URL: ${KEYCLOAK_REALM_URL} - hostname: service-api - dns: 142.22.202.100 - ports: - - "8090:8090" - build: - context: . - dockerfile: Dockerfile + healthcheck: + test: ["CMD", "java", "HealthCheck"] + interval: 1m30s + timeout: 10s + retries: 3 + start_period: 40s + image: bcgov/nrbestapi-test-service-api:latest + network_mode: "host" + volumes: + - ./target:/usr/share/service/artifacts diff --git a/dockerfile-entrypoint.sh b/dockerfile-entrypoint.sh index ec74228..9763839 100644 --- a/dockerfile-entrypoint.sh +++ b/dockerfile-entrypoint.sh @@ -4,7 +4,10 @@ java --source 17 InstallCert.java --quiet "${DATABASE_HOST}:${DATABASE_PORT}" keytool -exportcert -alias "${DATABASE_HOST}-1" -keystore jssecacerts -storepass changeit -file oracle.cer keytool -importcert -alias orakey -noprompt -cacerts -storepass changeit -file oracle.cer +javac HealthCheck.java + java \ -Djava.security.egd=file:/dev/./urandom \ + ${JAVA_OPTS} \ -jar \ - /usr/share/service/service.jar + /usr/share/service/artifacts/backend-start-api.jar