diff --git a/.gitignore b/.gitignore index c2065bc..2a945c1 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,9 @@ build/ !gradle/wrapper/gradle-wrapper.jar !**/src/main/**/build/ !**/src/test/**/build/ +deploy/.env +deploy/postgres +deploy/influxdb ### STS ### .apt_generated diff --git a/README.md b/README.md index d17dd4e..a0783fd 100644 --- a/README.md +++ b/README.md @@ -205,6 +205,79 @@ 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!** +> +> 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 deploy folder within the project directory +```shell + cd conluz/deploy + ``` +3. Place an `.env` file in the same directory as your `docker-compose.yml` with the following content: +```shell +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. +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 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 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 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 Pull requests are welcome. For major changes, please open an issue first 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..ce91858 100755 --- a/deploy/docker-compose.yaml +++ b/deploy/docker-compose.yaml @@ -9,6 +9,26 @@ 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" + # 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 + - SPRING_DATASOURCE_URL + - SPRING_INFLUXDB_URL + depends_on: + - postgres + - influxdb