From 7aa27166006447fbc25f797eb62e98d8480dbded Mon Sep 17 00:00:00 2001 From: Alfredo Gutierrez Date: Fri, 19 Jul 2024 10:43:14 -0600 Subject: [PATCH] Adding files needed for the dockerization of the app Signed-off-by: Alfredo Gutierrez --- .gitignore | 2 ++ server/build.gradle.kts | 37 ++++++++++++++++++++++++++++++++ server/docker/.dockerignore | 9 ++++++++ server/docker/Dockerfile | 20 +++++++++++++++++ server/docker/README.md | 8 +++++++ server/docker/docker-build.sh | 16 ++++++++++++++ server/docker/docker-compose.yml | 9 ++++++++ server/docker/update-env.sh | 16 ++++++++++++++ 8 files changed, 117 insertions(+) create mode 100644 server/docker/.dockerignore create mode 100644 server/docker/Dockerfile create mode 100644 server/docker/README.md create mode 100755 server/docker/docker-build.sh create mode 100644 server/docker/docker-compose.yml create mode 100755 server/docker/update-env.sh diff --git a/.gitignore b/.gitignore index cfc4ee37..f0f492d9 100644 --- a/.gitignore +++ b/.gitignore @@ -47,3 +47,5 @@ gradle-app.setting # JDT-specific (Eclipse Java Development Tools) .classpath +# .env files +server/docker/.env diff --git a/server/build.gradle.kts b/server/build.gradle.kts index a09bef8a..af349719 100644 --- a/server/build.gradle.kts +++ b/server/build.gradle.kts @@ -29,3 +29,40 @@ testModuleInfo { requires("org.mockito") requires("org.mockito.junit.jupiter") } + +var updateDockerEnv = + tasks.register("updateDockerEnv") { + description = + "Creates the .env file in the docker folder that contains environment variables for docker" + group = "docker" + + workingDir(layout.projectDirectory.dir("docker")) + commandLine("./update-env.sh", project.version) + } + +tasks.register("createDockerImage") { + description = "Creates the docker image of the Block Node Server based on the current version" + group = "docker" + + dependsOn(updateDockerEnv, tasks.assemble) + workingDir(layout.projectDirectory.dir("docker")) + commandLine("./docker-build.sh", project.version, layout.projectDirectory.dir("..").asFile) +} + +tasks.register("startDockerContainer") { + description = "Starts the docker container of the Block Node Server of the current version" + group = "docker" + + dependsOn(updateDockerEnv) + workingDir(layout.projectDirectory.dir("docker")) + commandLine("sh", "-c", "docker-compose -p block-node up -d") +} + +tasks.register("stopDockerContainer") { + description = "Stops running docker containers of the Block Node Server" + group = "docker" + + dependsOn(updateDockerEnv) + workingDir(layout.projectDirectory.dir("docker")) + commandLine("sh", "-c", "docker-compose -p block-node stop") +} diff --git a/server/docker/.dockerignore b/server/docker/.dockerignore new file mode 100644 index 00000000..2234c260 --- /dev/null +++ b/server/docker/.dockerignore @@ -0,0 +1,9 @@ +*.iml +*.md +*.sw* +*.csv + +mvnw* +LICENSE* + +.java-version diff --git a/server/docker/Dockerfile b/server/docker/Dockerfile new file mode 100644 index 00000000..58b93cec --- /dev/null +++ b/server/docker/Dockerfile @@ -0,0 +1,20 @@ +# Use Eclipse Temurin with Java 21 as the base image +FROM eclipse-temurin:21 + +# Define version +ARG VERSION + +# Set the working directory inside the container +WORKDIR /app + +# Copy Distribution TAR file +COPY --from=distributions server-${VERSION}.tar . + +# Extract the TAR file +RUN tar -xvf server-${VERSION}.tar + +# Expose the port that the application will run on +EXPOSE 8080 + +# RUN the bin script for starting the server +ENTRYPOINT ["sh", "-c", "/app/server-${VERSION}/bin/server"] diff --git a/server/docker/README.md b/server/docker/README.md new file mode 100644 index 00000000..7ab3d39e --- /dev/null +++ b/server/docker/README.md @@ -0,0 +1,8 @@ +This folder contains file that are used to start the services as docker containers. +All docker workflows are defined with a corresponding gradle task. No script or file in this folder should be called 'by hand'. + +Gradle tasks are of group `docker` + - updateDockerEnv + - createDockerImage + - startDockerContainer + - stopDockerContainer diff --git a/server/docker/docker-build.sh b/server/docker/docker-build.sh new file mode 100755 index 00000000..e3284ad8 --- /dev/null +++ b/server/docker/docker-build.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash + +if [[ $# -lt 1 ]]; then + echo "Usage: ${0} [version] [project_dir]" + exit 1 +fi + +VERSION=${1} + +echo "CREATING CONTAINER FOR VERSION ${VERSION}" +echo "Using project directory: ${2}" +echo + +# run docker build +echo "Building container:" +docker buildx build --load -t "block-node-server:${1}" --build-context distributions=../build/distributions --build-arg VERSION="${VERSION}" . || exit "${?}" diff --git a/server/docker/docker-compose.yml b/server/docker/docker-compose.yml new file mode 100644 index 00000000..2cda840b --- /dev/null +++ b/server/docker/docker-compose.yml @@ -0,0 +1,9 @@ +version: '3.8' + +services: + block-node-server: + image: block-node-server:${VERSION} + env_file: + - .env + ports: + - "8080:8080" diff --git a/server/docker/update-env.sh b/server/docker/update-env.sh new file mode 100755 index 00000000..6f655f8d --- /dev/null +++ b/server/docker/update-env.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash + +# This scripts create a '.env' file that is used for docker & docker-compose as an input of environment variables. +# This script is called by gradle and get the current project version as an input param + +if [ $# -lt 1 ]; then + echo "USAGE: $0 " + exit 1 +fi + +echo "VERSION=$1" > .env +echo "REGISTRY_PREFIX=" >> .env +# Storage root path, this is temporary until we have a proper .properties file for all configs +echo "BLOCKNODE_STORAGE_ROOT_PATH=/app/storage" >> .env + +echo "VERSION/TAG UPDATED TO $1"