# Docker Deployment Guide

## Prerequisites

- Docker and Docker Compose installed
- MySQL database with `shukria_transactions` schema

---

## Build

### 1. Build the application
```bash
./gradlew clean installApp
```

### 2. Build the Docker image
```bash
docker build -t shukria-mw:latest -t shukria-mw:2.2.0-SNAPSHOT .
```

---

## Run (Windows / Local)

### 1. Create `.env` file (copy from `.env.example`)
```
DB_URL=jdbc:mysql://host.docker.internal:3306/shukria_transactions
DB_USERNAME=your_user
DB_PASSWORD=your_password
```

### 2. Start
```bash
docker compose up -d
```

### 3. Stop
```bash
docker compose down
```

### 4. Logs
```bash
docker logs -f shukria-mw
```

---

## Deploy to Linux Server

### Step 1 — Save and transfer image (Windows)
```powershell
docker save shukria-mw:latest -o shukria-mw-2.2.0-SNAPSHOT.tar
scp shukria-mw-2.2.0-SNAPSHOT.tar user@linux-server:/opt/shukria-mw/
```

### Step 2 — Load image (Linux)
```bash
docker load < shukria-mw-2.2.0-SNAPSHOT.tar
```

### Step 3 — Linux folder structure
```
/opt/shukria-mw/
├── docker-compose.yml
├── .env
├── cfg/                  ← copy from jpos/src/dist/cfg/
│   └── application.properties
├── deploy/               ← copy from jpos/src/dist/deploy/
│   └── *.xml
└── logs/                 ← auto-created by Docker
```

### Step 4 — One-time Linux setup

#### UFW firewall — allow Docker network to reach MySQL
```bash
sudo ufw allow from 172.20.0.0/16 to any port 3306
```

#### MySQL — grant access from Docker network
```bash
mysql -u root -p -e "
CREATE USER IF NOT EXISTS 'test'@'172.20.0.%' IDENTIFIED BY 'test';
GRANT ALL PRIVILEGES ON shukria_transactions.* TO 'test'@'172.20.0.%';
ALTER USER 'test'@'172.20.0.%' IDENTIFIED BY 'test';
FLUSH PRIVILEGES;"
```

> **Note:** `172.20.0.0/16` is the Docker bridge network on the demo server.
> If the bridge IP differs on another server, check with: `ip addr show | grep "172."`

### Step 5 — `.env` file (Linux)
```
DB_URL=jdbc:mysql://172.20.0.1:3306/shukria_transactions
DB_USERNAME=test
DB_PASSWORD=test
```

### Step 6 — Start
```bash
docker compose up -d
docker logs -f shukria-mw
```

---

## YSP Mock Server

The YSP acquirer IP/port is stored in the database (`acquirer_connection` table), not in config files.

The application connects to the mock server automatically on startup and retries every 3 seconds until the connection succeeds — no container restart is needed if the mock server starts after the application.

### If mock is running on the same Linux host
```sql
UPDATE acquirer_connection ac
JOIN acquirer a ON ac.acquirer_id = a.id
SET ac.ip_address = '172.20.0.1'
WHERE a.name = 'YSP';
```

### If mock is a Docker container (e.g. `ycs-server-mock`)
Use the container's IP directly:
```bash
docker inspect ycs-server-mock | grep IPAddress
```
Then update the DB with that IP.

---

## Common Commands

| Command | Description |
|---|---|
| `docker compose up -d` | Start in background |
| `docker compose down` | Stop and remove container |
| `docker compose stop` | Stop without removing |
| `docker compose start` | Start a stopped container |
| `docker compose restart` | Restart |
| `docker logs -f shukria-mw` | Tail logs |
| `docker logs --tail 100 shukria-mw` | Last 100 lines |
| `docker exec -it shukria-mw bash` | Shell into container |
| `docker ps` | List running containers |

---

## Notes

- `restart: unless-stopped` — container auto-restarts on crash
- Logs persist in `./logs/` on the host even after container restarts
- `cfg/` and `deploy/` volumes override files baked into the image at runtime
- On Linux, `host.docker.internal` requires `extra_hosts: host-gateway` in `docker-compose.yml`
