From c002182dd444e14b97e33ef01010220ab46bba30 Mon Sep 17 00:00:00 2001 From: viktorKhan Date: Fri, 8 Mar 2024 20:59:44 +0100 Subject: [PATCH 1/4] [conluz-57] Created a dockerfile to be able to generate an image for the app and added two new services to the docker compose file: conluz and influxdb --- deploy/Dockerfile | 11 +++++++++++ deploy/docker-compose.yaml | 16 +++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 deploy/Dockerfile diff --git a/deploy/Dockerfile b/deploy/Dockerfile new file mode 100644 index 0000000..66b3bb1 --- /dev/null +++ b/deploy/Dockerfile @@ -0,0 +1,11 @@ +# This base image contains OpenJDK 17 on Alpine Linux which is a lightweight Linux distribution. +FROM openjdk:17-jdk-alpine + +# Tries to find a .jar file from within the build/libs directory from the current path. +ARG JAR_FILE=../build/libs/*.jar +# Copy the jar file to the container image. +COPY ${JAR_FILE} conluz.jar + +# Run the application using the java -jar command. +# This sets conluz as default application, which Docker will execute when a container is launched from the image. +ENTRYPOINT ["java", "-jar", "/conluz.jar"] diff --git a/deploy/docker-compose.yaml b/deploy/docker-compose.yaml index 32056bd..7cbeb8b 100755 --- a/deploy/docker-compose.yaml +++ b/deploy/docker-compose.yaml @@ -9,6 +9,20 @@ services: - POSTGRES_USER=admin - POSTGRES_PASSWORD=blank volumes: - - ../../conluz-backup/postgres:/var/lib/postgresql/data # Get a backup locally + - ./postgres/data:/var/lib/postgresql/data # Get a backup locally - ./conluz-postgres-init.sh:/docker-entrypoint-initdb.d/conluz-postgres-init.sh # Mount the init.sql script restart: always + influxdb: + image: influxdb:1.8 + ports: + - "8086:8086" + volumes: + - ./influxdb:/var/lib/influxdb + conluz: + build: . + image: conluz:0.0.1 + ports: + - "8080:8080" + depends_on: + - postgres + - influxdb From f2ae056f1fc7269d1c7d7f4dad14530267ec6215 Mon Sep 17 00:00:00 2001 From: viktorKhan Date: Fri, 8 Mar 2024 22:53:23 +0100 Subject: [PATCH 2/4] [conluz-57] Added an explanation about how to deploy the app --- README.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/README.md b/README.md index d17dd4e..82de913 100644 --- a/README.md +++ b/README.md @@ -205,6 +205,39 @@ Alternatively, you can access the API documentation through the Swagger UI, a us - Explore and interact with the API endpoints in a visually appealing and intuitive way. - Swagger UI provides an easy way to understand request and response formats, and even allows you to make sample requests directly from the documentation. +## Deployment + +> **Important!** +> +> Ensure that Docker is installed on your system. If not, you can download it from Docker's [official website](https://www.docker.com/get-started/). + +Here are the steps to deploy the app: +1. Clone the repository +```shell + git clone git@github.com:lucoenergia/conluz.git + ``` +2. Navigate to the project directory +```shell + cd conluz + ``` +3. Build the Docker image +```shell + docker build -t conluz:1.0 -f deploy/Dockerfile . + ``` +This command builds the Docker image from the Dockerfile and tags it with the name conluz:1.0. You have to replace the version number by the one you want to deploy. +4. Run the Docker container +```shell + docker run -d --name conluz -p 8080:8080 conluz:1.0 + ``` +This command runs the Docker image as a container, maps the host's port 8080 to the container's port 8080, and names the container conluz. + +At this point, the application should be running at http://localhost:8080. + +To stop the application, you can `run docker stop conluz`. + +To delete the container, you can run `docker rm conluz`. + + ## Contributing Pull requests are welcome. For major changes, please open an issue first From d2f793d87ca8eae391d54ac2e83e9259bdf1a637 Mon Sep 17 00:00:00 2001 From: viktorKhan Date: Fri, 8 Mar 2024 22:54:18 +0100 Subject: [PATCH 3/4] [conluz-57] Added a reference to the env var required to set the JWT secret key in the docker compose file --- .gitignore | 1 + deploy/docker-compose.yaml | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/.gitignore b/.gitignore index c2065bc..9273bd4 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ build/ !gradle/wrapper/gradle-wrapper.jar !**/src/main/**/build/ !**/src/test/**/build/ +deploy/.env ### STS ### .apt_generated diff --git a/deploy/docker-compose.yaml b/deploy/docker-compose.yaml index 7cbeb8b..4d6f315 100755 --- a/deploy/docker-compose.yaml +++ b/deploy/docker-compose.yaml @@ -23,6 +23,10 @@ services: image: conluz:0.0.1 ports: - "8080:8080" + # Create a .env file in the folder where this file is located and define the environment variables there. + # When run docker-compose up, Docker will automatically pick up the .env file in the same directory as this docker-compose.yml file, and substitute the values of the environment variables accordingly. + environment: + - CONLUZ_JWT_SECRET_KEY depends_on: - postgres - influxdb From 1c463f1fa9a9cbf34094d44dbca31488a03ab002 Mon Sep 17 00:00:00 2001 From: viktorKhan Date: Fri, 8 Mar 2024 23:37:02 +0100 Subject: [PATCH 4/4] [conluz-57] Added env vars to connect with postgres and influxdb --- .gitignore | 2 ++ README.md | 58 ++++++++++++++++++++++++++++++++------ deploy/docker-compose.yaml | 2 ++ 3 files changed, 53 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 9273bd4..2a945c1 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,8 @@ build/ !**/src/main/**/build/ !**/src/test/**/build/ deploy/.env +deploy/postgres +deploy/influxdb ### STS ### .apt_generated diff --git a/README.md b/README.md index 82de913..a0783fd 100644 --- a/README.md +++ b/README.md @@ -209,34 +209,74 @@ Alternatively, you can access the API documentation through the Swagger UI, a us > **Important!** > -> Ensure that Docker is installed on your system. If not, you can download it from Docker's [official website](https://www.docker.com/get-started/). +> Docker: Ensure that Docker is installed on your system. If not, you can download it from Docker's [official website](https://www.docker.com/get-started/). +> Docker Compose: This is a tool for defining and managing multi-container Docker applications. If you have Docker Desktop on your machine, Docker Compose should be pre-installed. If not, you can get it from [here](https://docs.docker.com/compose/install/). + +### Deploy the app from the scratch Here are the steps to deploy the app: 1. Clone the repository ```shell git clone git@github.com:lucoenergia/conluz.git ``` -2. Navigate to the project directory +2. Navigate to the deploy folder within the project directory ```shell - cd conluz + cd conluz/deploy ``` -3. Build the Docker image +3. Place an `.env` file in the same directory as your `docker-compose.yml` with the following content: ```shell - docker build -t conluz:1.0 -f deploy/Dockerfile . +CONLUZ_JWT_SECRET_KEY=your-secret-key +``` +Replace your-secret-key with your actual secret key. +4. Build the Docker image +```shell + docker build -t conluz:1.0 -f Dockerfile . ``` This command builds the Docker image from the Dockerfile and tags it with the name conluz:1.0. You have to replace the version number by the one you want to deploy. -4. Run the Docker container +Here's what's happening in the command: +- `-t conluz:1.0` names (or "tags") the image. +- `-f deploy/Dockerfile` tells Docker where to find your Dockerfile. +- `.` tells Docker to use the current directory as the context for the build (i.e., sets the "build context"). +To confirm if the Docker image has been built successfully, you can list all available Docker images with the command: ```shell - docker run -d --name conluz -p 8080:8080 conluz:1.0 +docker images +``` +You should see the image (conluz:1.0) in the resulting list. +5. Start the services with Docker Compose +```shell + docker compose up -d ``` -This command runs the Docker image as a container, maps the host's port 8080 to the container's port 8080, and names the container conluz. +This command will start all your services in the background. Docker Compose will start all the services defined in the `docker-compose.yml` file, in the correct order. At this point, the application should be running at http://localhost:8080. To stop the application, you can `run docker stop conluz`. -To delete the container, you can run `docker rm conluz`. +To delete the application container, you can run `docker rm conluz`. + +### Re-deploy a new version of the app + +1. Stop the currently running Docker container. + +You can do this with the `docker stop command`. is the id of your currently running Docker container. +2. Remove the stopped Docker container + +This isn't strictly necessary if you're using anonymous/non-persistent volumes, but it's a good practice nonetheless to clean up after yourself. You can use the `docker rm ` command to remove the stopped container. + +3. Build a new Docker image with the version of the app you want to deploy + +This can be done using the `docker build` command. Make sure to tag your image with a new version tag, something like: + ```shell +docker build -t conluz:2.0 -f deploy/Dockerfile . +``` +4. Run a new Docker container with the new image + +This can be done running this command: + +```shell +docker compose up conluz -d +``` ## Contributing diff --git a/deploy/docker-compose.yaml b/deploy/docker-compose.yaml index 4d6f315..ce91858 100755 --- a/deploy/docker-compose.yaml +++ b/deploy/docker-compose.yaml @@ -27,6 +27,8 @@ services: # When run docker-compose up, Docker will automatically pick up the .env file in the same directory as this docker-compose.yml file, and substitute the values of the environment variables accordingly. environment: - CONLUZ_JWT_SECRET_KEY + - SPRING_DATASOURCE_URL + - SPRING_INFLUXDB_URL depends_on: - postgres - influxdb