diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5b5a137 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.env +.env.prod +.env.dev +.DS_Store +.vscode/ \ No newline at end of file diff --git a/README.md b/README.md index 3b2be1b..01157c5 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,77 @@ -docker-compose build -docker-compose up +## alerteconso.com + +### Environment variables + +Those environment variables should be used when [building](#building) and [publishing](#publishing) the project + +|  Variable name  |  Description | Environment | +| ----------------- | ------------------------------------------------------------------------------------ | ------------ | +| POSTGRES_USER |  The PostgreSQL username | dev and prod | +| POSTGRES_PASSWORD | The PostgreSQL password  | dev and prod | +| POSTGRES_HOST | The PostgresSQL hostname  | dev and prod | +| POSTGRES_DB | The PostgreSQL database name  | dev and prod | +| TAG | The tag of the api image (default to `latest`)  | dev | +| SSL_MODE | The sslmode parameter used when connecting to database (only requird in production)  | prod | + +> [!TIP] +> You can create youself and `.env` file that defines values for those environment variables for `dev` and use `--env-file` option in the `docker compose` command instead of redfining each environment variable + +### Local development + +To run the application on your local machine (dev environment) please use following command to run the application in watch mode. + +```bash +POSTGRES_USER= POSTGRES_PASSWORD= POSTGRES_HOST= docker compose --profile dev watch +``` + +or if you have defined an `.env` file + +```bash +docker compose --env-file --profile dev watch +``` + +Keep your terminal open, the watch mode will follow the changes in the build context and update your application accordingly. + +> [!NOTE] +> To clean up the image and shared volume properly use `docker compose down --volume` + +### Publishing + +Once you have finished the development publish your compose application by following the following steps: + +> [!TIP] +> You can create youself and `.env` file that defines values for those environment variables for `prod` and use `--env-file` option in the `docker compose` command instead of redfining each environment variable + +1. Build the images in production profile + ```bash + TAG= POSTGRES_USER= POSTGRES_PASSWORD= POSTGRES_HOST= docker compose --profile prod build --platform linux/amd64,linux/arm64 + ``` + or if you have created an `.env` file + ```bash + docker compose --from-env --profile prod build --platform linux/amd64,linux/arm64 + ``` +2. Push the images to the local registry + ```bash + TAG= POSTGRES_USER= POSTGRES_PASSWORD= POSTGRES_HOST= docker compose --profile prod push + ``` + or if you have created an `.env` file + ```bash + docker compose --from-env --profile prod push + ``` +3. Publish the Docker stack + ```bash + TAG= POSTGRES_USER= POSTGRES_PASSWORD= POSTGRES_HOST= docker compose --profile prod --resolve-image-digests publish /: + ``` + or if you have created an `.env` file + ```bash + docker compose --from-env --profile prod publish + ``` + +### Deployment + +First create an `.env` file for the production environment, that defines values for [environment variables](#environment-variables) if you did not already. + +Once you have published the docker compose bundle and you have created the `.env` file, you can know deploy the application with: +```bash +docker compose -f oci:///: --env-file up -d +``` diff --git a/compose.yml b/compose.yml new file mode 100644 index 0000000..179453c --- /dev/null +++ b/compose.yml @@ -0,0 +1,59 @@ +services: + alerteconso-api-dev: + hostname: alerteconso-app + build: . + image: yagcikaan/alerteconso:${TAG:-latest} + container_name: alerteconso-api + restart: unless-stopped + profiles: ["dev"] + ports: + - "9092:8080" + depends_on: + db: + condition: service_healthy + environment: + - DATABASE_URL=postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:5432/${POSTGRES_DB}?sslmode=disable + networks: + - alerteconso-network + + alerteconso-api-prod: + hostname: alerteconso-app + build: . + image: yagcikaan/alerteconso:${TAG:-latest} + container_name: alerteconso-api + restart: unless-stopped + profiles: ["prod"] + ports: + - "9092:8080" + environment: + # TODO: Enable ssl mode in production + - DATABASE_URL=postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:5432/${POSTGRES_DB}?sslmode=${SSL_MODE:-enable} + networks: + - caddy-network-alerteconso + + db: + hostname: ${POSTGRES_HOST} + image: postgres:17 + container_name: alerteconso-db + restart: unless-stopped + profiles: ["dev"] + environment: + POSTGRES_USER: ${POSTGRES_USER} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} + POSTGRES_DB: ${POSTGRES_DB} + ports: + - "5433:5432" + volumes: + - pgdata:/var/lib/postgresql/data + - ./schema.sql:/docker-entrypoint-initdb.d/init.sql:ro + networks: + - alerteconso-network + +networks: + alerteconso-network: + driver: bridge + caddy-network-alerteconso: + driver: bridge + +volumes: + pgdata: diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index 68d21c2..0000000 --- a/docker-compose.yml +++ /dev/null @@ -1,37 +0,0 @@ -services: - rappel-api: - build: . - ports: - - "9092:8080" - depends_on: - - db - environment: - - DATABASE_URL=postgres://rappeluser:rappelpass@db:5432/rappeldb?sslmode=disable - networks: - - rappelconso-network - - caddy-network-rappelconso - - db: - image: postgres:17 - container_name: rappelconso-db - restart: unless-stopped - environment: - POSTGRES_USER: rappeluser - POSTGRES_PASSWORD: rappelpass - POSTGRES_DB: rappeldb - ports: - - "5433:5432" - volumes: - - pgdata:/var/lib/postgresql/data - - ./schema.sql:/docker-entrypoint-initdb.d/init.sql:ro - networks: - - rappelconso-network - -networks: - rappelconso-network: - driver: bridge - caddy-network-rappelconso: - driver: bridge - -volumes: - pgdata: