# Docker Deployment Guide — GB4.7 MFE

---

## How It Works

```
Developer Machine          Docker Hub             Client Machine
─────────────────          ──────────             ──────────────
Build Image          →     Push Image       →     Pull & Run
```

| Role          | Task |
|----------------------|
| **Developer** | Build the app image, push to Docker Hub, send 2 files to client |
| **Client**    | Install Docker, create `.env` from template, run the app |

---

## Part 1 — Developer Steps

## BUILD TYPE 1

1. Build image locally with tag(latest)

`docker compose build --no-cache`

OutPut(Docker) : gb47mfe-frontend:latest

2. Run locally for checking

`docker compose up -d`

OutPut : http://localhost:8080/

3. Create new image from latest image for new tag(version)

`docker tag gb47mfe-frontend:latest karunaunisoft/gb47mfe:4.7.0.61.3`

OutPut(Docker) : karunaunisoft/gb47mfe:4.7.0.61.3

4. Push version image to docker hub

`docker push karunaunisoft/gb47mfe:4.7.0.61.3`

OutPut(Docker Hub) : karunaunisoft/gb47mfe:4.7.0.61.3

## BUILD TYPE 2

1. Build image directly using version(tag)

`docker buildx build --progress=plain -t karunaunisoft/gb47mfe:4.7.0.61.3 .`

OutPut(Docker) : karunaunisoft/gb47mfe:4.7.0.61.3

2. Push version image to docker hub

`docker push karunaunisoft/gb47mfe:4.7.0.61.3`

OutPut(Docker Hub) : karunaunisoft/gb47mfe:4.7.0.61.3

---

## Part 2 — Client Steps

| # | Step               | Action                                      | Detail |
|-------------------------------------------------------------------------------|
| 1 | **Install Docker** | Download Docker Desktop                     | https://www.docker.com/products/docker-desktop |
| 2 | **Create folder**  | `mkdir C:\GoodBooks\GB47`                   | Any folder works |
| 3 | **Place files**    | Copy both files into the folder             | `docker-compose.yaml` and `.env` |
| 4 | **Config**         |  edit it                                    | Fill in your image and server URLs (see below) |
| 5 | **Start the app**  | `docker compose up -d`                      | Docker auto-pulls the image |
| 6 | **Verify**         | `docker ps` then open http://localhost:8080 | See expected output below |

### docker-compose.yaml` Values to Fill In

| Variable                 | Example Value                | Description |
|-------------------------------------------------------------------|
| `image`            | `karunaunisoft/gb47mfe:4.7.0.61.3` | Your docker hub image name with tag |
| `ports`            | `- "8080:80"`                      | Default port is 8080 you can change whatever port that available |

### `.env` Values to Fill In

| Variable                 | Example Value                | Description |
|-------------------------------------------------------------------|
| `GB5_VERSION_IDENTIFIER` | `https://api.client.com`     | Version check URL |

### Expected Output of `docker ps`

```
IMAGE                                  STATUS    PORTS
karunaunisoft/gb47mfe:4.7.0.61.3       Up        0.0.0.0:8080->80/tcp
```

### Debug Config (Optional)

```bash
docker exec gb47mfe-frontend-1 cat /usr/share/nginx/html/config/gbconfig.json
```
Confirms `.env` values are loaded correctly inside the container.

---

## Releasing a New Version

| Who           | Command |
|-------------------------|
| **Developer** | `docker build -t karunaunisoft/gb47mfe:4.7.0.61.3 .` |
| **Developer** | `docker push karunaunisoft/gb47mfe:4.7.0.61.3` |
| **Client**    | `docker compose pull` then `docker compose up -d` |

> Client does **not** need to change `.env` for routine version updates.

---

## File Ownership Reference

| File                         | Created By      | Sent to Client  | Inside Docker Image |
|----------------------------------------------------------------------------------------|
| `Dockerfile`                 | Developer       | No              | No |
| `docker-compose.yaml`        | Developer       | Yes             | No |
| `.env`                       | Client          | —               | No |
| Docker Image                 | Developer → Hub | Client pulls it | Yes |

---

## Quick Command Reference

### Compose Commands

| Command                          | What It Does |
|-------------------------------------------------|
| `docker compose up -d`           | Start all services in the background |
| `docker compose down`            | Stop and remove containers (keeps images) |
| `docker compose down -v`         | Stop and remove containers + volumes |
| `docker compose build`           | Build images defined in compose file |
| `docker compose build --no-cache`| Rebuild images from scratch (no layer cache) |
| `docker compose pull`            | Pull latest images from registry |
| `docker compose restart`         | Restart all services |
| `docker compose logs -f`         | Stream live logs from all services |
| `docker compose ps`              | Show status of compose-managed containers |
| `docker compose exec <svc> sh`   | Open shell in a running compose service |

### Container Commands

| Command               | What It Does |
|--------------------------------------|
| `docker ps`           | List running containers |
| `docker ps -a`        | List all containers including stopped |
| `docker stop <id>`    | Stop a container |
| `docker start <id>`   | Start a stopped container |
| `docker restart <id>` | Restart a container |

### Image Commands

| Command                               | What It Does |
|------------------------------------------------------|
| `docker images`                       | List all local images |
| `docker rmi <id>`                     | Delete a local image |
| `docker system prune -a`              | Remove all unused containers and images |
| `docker image rm gb47mfe-frontend`    | Delete the Docker image named gb47mfe-frontend from machine |


### Logs & Debug Commands

| Command                         | What It Does |
|------------------------------------------------|
| `docker logs <id>`              | View container log output |
| `docker exec -it <id> sh`       | Open a shell inside the container |

### Port & Process Commands (Windows)

| Command                              | What It Does |
|-----------------------------------------------------|
| `netstat -ano \| findstr :8080`      | Find PID using port 8080 |
| `netstat -ano \| findstr :80`        | Find PID using port 80 |
| `tasklist \| findstr <PID>`          | Find process name by PID |
| `taskkill /PID <PID> /F`             | Force kill a process by PID |
| `Get-Process -Id <PID>`              | Find process name by PID (PowerShell) |
| `Stop-Process -Id <PID> -Force`      | Force kill a process (PowerShell) |
| `docker kill <id>`                   | Force kill a running container immediately |

---

## Ports Reference

### Ports Used by This Project

| File | Service | Host Port → Container Port |
|------|---------|---------------------------|
| `docker-compose.yaml` | `frontend` | **8080** → 80 |

> The container always runs nginx on internal port **80**. Only the host-side port (left of `:`) changes between environments.

### Ports Already in Use on This Machine

| Port | Notes |
|------|-------|
| **8080** | GB4.7 frontend (this project) |
| **8081** | Another service already bound |
| **5040** | System service |
| **5357** | Windows Network Discovery |
| **49664–49719** | Windows ephemeral/system ports |

### Safe Ports for New Docker Services

| Port | Typical Use |
|------|-------------|
| **8082** | Good next pick after 8080/8081 |
| **8083–8090** | Additional MFE remotes or services |
| **4200** | Angular dev server convention |
| **4201–4210** | Additional Angular/frontend remotes |
| **3000** | Node/Express backend APIs |
| **3001–3010** | Additional backends |
| **9000** | Portainer, SonarQube, etc. |
| **9090** | Prometheus convention |

To check which ports are live before picking one (Windows):
```powershell
netstat -ano | findstr "LISTENING"
```

---

## Best Practices

| Rule                             | Why |
|----------------------------------------|
| Always tag with a version number | `latest`-only makes rollbacks impossible |
| Never share your `.env` file     | Contains server credentials and URLs |
| Test locally before pushing      | Catch broken builds before clients are affected |
