diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..3ffc8f5 --- /dev/null +++ b/.env.example @@ -0,0 +1,10 @@ +# Copy to .env and adjust values before `docker compose up -d` + +# PostgreSQL +POSTGRES_USER=postgres +POSTGRES_PASSWORD=change_me_postgres_password +POSTGRES_DB=game + +# MongoDB (required by the app) +MONGO_ROOT_USERNAME=wingsemu +MONGO_ROOT_PASSWORD=change_me_mongo_password diff --git a/README.md b/README.md index e8fdc1e..2d1c43a 100644 --- a/README.md +++ b/README.md @@ -116,22 +116,42 @@ Let's skip it using `Skip tutorial` button and... that's it - Congratulations! ![](https://i.imgur.com/AUY0n7c.png) -### Running Docker +### Running Docker (Windows + Linux) -To run PostgreSQL, Redis, MongoDB and MQTT Broker for our server, we have to create [Docker containers](https://www.docker.com/resources/what-container). +To run PostgreSQL, Redis, MongoDB and MQTT Broker for our server, use Docker Compose (cross-platform). -- **PowerShell** - - Go to the `.server/scripts/Docker` directory - - For each file in the directory, click right mouse button on the file and choose `Run with PowerShell` - - ![](https://i.imgur.com/1ar8Nkd.png) -- **Terminal** - - Go to the `.server/scripts/Docker` directory - - Open each `.ps1` file, select the entire script (starts with `docker run`) and copy it into Terminal (you can use this script anywhere) and then press [`ENTER`] key: - - ![](https://i.imgur.com/auCrU6q.png) +1. Copy env file: -After successfully using the commands, you should see 4 new containers in your Docker Hub. +```bash +cp .env.example .env +``` -![](https://i.imgur.com/Lz5O8PL.png) +2. Edit `.env` and set strong secrets (`POSTGRES_PASSWORD`, `MONGO_ROOT_PASSWORD`). + +3. Start dependencies: + +```bash +docker compose up -d +``` + +4. Check status: + +```bash +docker compose ps +``` + +5. Stop everything: + +```bash +docker compose down +``` + +Optional helper scripts (Linux/macOS): + +```bash +./scripts/docker-up.sh +./scripts/docker-down.sh +``` ___ diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..4708810 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,68 @@ +services: + postgres: + image: postgres:13 + container_name: wingsemu-postgres + restart: unless-stopped + environment: + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} + POSTGRES_DB: ${POSTGRES_DB:-game} + POSTGRES_USER: ${POSTGRES_USER:-postgres} + ports: + - "5432:5432" + volumes: + - postgres_data:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-postgres}"] + interval: 10s + timeout: 5s + retries: 10 + + redis: + image: redis:7-alpine + container_name: wingsemu-redis + restart: unless-stopped + command: ["redis-server", "--notify-keyspace-events", "Kx", "--appendonly", "yes"] + ports: + - "6379:6379" + volumes: + - redis_data:/data + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 10s + timeout: 5s + retries: 10 + + mongodb: + image: mongo:6 + container_name: wingsemu-mongodb + restart: unless-stopped + environment: + MONGO_INITDB_ROOT_USERNAME: ${MONGO_ROOT_USERNAME} + MONGO_INITDB_ROOT_PASSWORD: ${MONGO_ROOT_PASSWORD} + ports: + - "27017:27017" + volumes: + - mongodb_data:/data/db + healthcheck: + test: ["CMD", "mongosh", "--quiet", "--eval", "db.runCommand({ ping: 1 }).ok"] + interval: 15s + timeout: 10s + retries: 10 + + mqtt: + image: emqx/emqx:latest + container_name: wingsemu-mqtt + restart: unless-stopped + ports: + - "1883:1883" + - "18083:18083" + healthcheck: + test: ["CMD-SHELL", "emqx ctl status | grep -q 'is started'"] + interval: 15s + timeout: 10s + retries: 10 + +volumes: + postgres_data: + redis_data: + mongodb_data: diff --git a/scripts/docker-down.sh b/scripts/docker-down.sh new file mode 100644 index 0000000..2d9a137 --- /dev/null +++ b/scripts/docker-down.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +set -euo pipefail + +docker compose down + +echo "[OK] Containers stopped" diff --git a/scripts/docker-up.sh b/scripts/docker-up.sh new file mode 100644 index 0000000..60e7bd3 --- /dev/null +++ b/scripts/docker-up.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +set -euo pipefail + +if [[ ! -f .env ]]; then + echo "[INFO] .env not found. Creating from .env.example" + cp .env.example .env + echo "[WARN] Please edit .env secrets before using in production." +fi + +docker compose up -d + +echo "[OK] Containers started" +docker compose ps