Docker-Based Web App Deployment from macOS to Windows 11 via GitHub Actions

コンテナアプリ(Tomcat+MySQL)を作成し、それをGithubから自動で、クライアントマシンに展開する方法。
Chatgptで自動作成したマニュアルが完璧すぎるので、そのまま貼り付けます。

https://chatgpt.com/canvas/shared/682eb39db2a88191be1fc27e174a6a90

# Docker-Based Web App Deployment from macOS to Windows 11 via GitHub Actions

## 🛍️ Overview

This guide walks through setting up a Dockerized web application environment on macOS, pushing it to GitHub, and deploying it to a Windows 11 PC using GitHub Actions and a self-hosted runner.

## 🏗️ High-Level Architecture

```
[macOS (Dev)] --push--> [GitHub Repo] --trigger--> [Windows 11 Self-Hosted Runner] --> [Docker Compose: Tomcat + MySQL (Sakila)]
```

---

## 🧱 Step 1: Prepare Your Docker Environment on macOS

### 📁 Project Structure

The following structure should be **created by yourself** on your **local Mac PC**:

```
your-project/
├── docker-compose.yml
├── sakila-init.sql
└── .github/
    └── workflows/
        └── deploy.yml
```

### 📄 docker-compose.yml

Defines a Tomcat container and a MySQL container preloaded with the Sakila sample database.

```yaml
version: '3.8'

services:
  mysql:
    image: mysql:8.0
    container_name: sakila_mysql
    environment:
      MYSQL_ROOT_PASSWORD: rootpass
      MYSQL_DATABASE: sakila
    ports:
      - "3306:3306"
    volumes:
      - mysql_data:/var/lib/mysql
      - ./sakila-init.sql:/docker-entrypoint-initdb.d/sakila-init.sql

  tomcat:
    image: tomcat:9.0
    container_name: sakila_tomcat
    ports:
      - "8080:8080"
    volumes:
      - ./webapps:/usr/local/tomcat/webapps
    depends_on:
      - mysql

volumes:
  mysql_data:
```

### 🻃️ sakila-init.sql

This file contains both schema and data for Sakila.

#### How to Prepare:

1. Download `sakila-schema.sql` and `sakila-data.sql` from the [MySQL website](https://dev.mysql.com/doc/index-other.html).
2. Concatenate them into one file:

   ```bash
   cat sakila-schema.sql sakila-data.sql > sakila-init.sql
   ```
3. Place `sakila-init.sql` in the project root.


## ⚙️ Step 2: Set Up GitHub Actions Workflow

### 📄 .github/workflows/deploy.yml

This is a **GitHub Actions workflow definition file**. It automates deployment of your Docker containers to your Windows 11 PC whenever you push to your GitHub repository.

#### 🔁 What Triggers It?

The workflow is **triggered automatically by a `git push`**, usually to the `main` branch.

#### 🧱 Typical Structure of `deploy.yml`

```yaml
name: Deploy to Windows 11 via Self-Hosted Runner

on:
  push:
    branches:
      - main  # Trigger only when pushing to main branch

jobs:
  deploy:
    runs-on: self-hosted  # Uses your Windows 11 self-hosted runner
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Stop and remove old containers (if running)
        run: |
          docker compose down || echo "Containers not running"

      - name: Start containers using Docker Compose
        run: |
          docker compose up -d --build
```

#### 🔍 What Each Part Does

| Section                | Explanation                                                                                           |
| ---------------------- | ----------------------------------------------------------------------------------------------------- |
| `name`                 | A friendly name for your workflow.                                                                    |
| `on: push: branches:`  | Specifies the Git branch that will trigger this workflow.                                             |
| `jobs:`                | Defines the set of actions to perform.                                                                |
| `runs-on: self-hosted` | Instructs GitHub to run this job on your **Windows 11 PC** that’s registered as a self-hosted runner. |
| `steps:`               | The individual commands GitHub will run on the Windows machine.                                       |
| `actions/checkout`     | Clones the repository so Docker can access the files.                                                 |
| Docker commands        | Build and launch containers using Docker Compose.                                                     |

#### 🧠 Why Important?

* Automates deployments with **zero manual intervention**
* Ensures every `git push` leads to **a consistent and repeatable setup**
* Minimizes the chance of human error in manual deployment steps

---

## 🌐 Step 3: Push Project to GitHub (After Workflow Setup)

1. Create a new GitHub repository (e.g., `Docker_deploy`).
2. Clone it to your Mac.
3. Copy the project files into the repo folder.
4. Run:

   ```bash
   git add .
   git commit -m "Initial Docker setup"
   git push origin main
   ```

---

## 🖥️ Step 4: Prepare Windows 11 Target Machine

### ✅ Prerequisites:

* Docker Desktop installed and running
* Git installed
* Java and Maven (for building WAR files)

### 📌 Self-Hosted Runner Setup:

1. Go to GitHub → Your Repo → Settings → Actions → Runners → "Add Runner"
2. Choose Windows and follow the instructions to:

   * Download the runner
   * Extract it
   * Run `config.cmd` with the given token
   * Launch `run.cmd` (not as service!)

### 🔐 Why Use a Self-Hosted Runner?

* **Bypasses firewall restrictions**: GitHub cannot directly push to private client machines behind NAT/firewalls.
* **Secure reverse connection**: The runner establishes an **outgoing connection** to GitHub, making deployment secure and possible even behind enterprise firewalls.
* **Full control**: You control the host, execution, and Docker environment.

---

## 🚢 Step 5: Deploy and Verify

1. From your Mac, push code updates:

   ```bash
   git add .
   git commit -m "Trigger deploy"
   git push origin main
   ```
2. GitHub Actions will execute `deploy.yml` on your Windows PC.
3. Check `http://localhost:8080` on the Windows machine.

---

## 🎯 Step 6: Build and Deploy a WAR Web App (Movie Search)

### On Windows 11:

1. Open PowerShell and run:

   ```bash
   mvn archetype:generate -DgroupId=com.example -DartifactId=sample-webapp \
   -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
   cd sample-webapp
   mvn package
   ```
2. Move the WAR file:

   ```bash
   copy target\sample-webapp.war C:\temp
   docker cp C:\temp\sample-webapp.war sakila_tomcat:/usr/local/tomcat/webapps/
   docker restart sakila_tomcat
   ```
3. Access at `http://localhost:8080/sample-webapp`

---

## 🖱️ Optional: Create a One-Click Installer for New Clients

### install-and-launch.cmd

```cmd
@echo off
REM Install Docker, Git, Java, Maven (manual steps or chocolatey)
REM Setup folders
mkdir C:\docker-deploy
cd C:\docker-deploy
REM Clone GitHub project
git clone https://github.com/YOUR_USER/YOUR_REPO.git .
REM Start GitHub Actions Runner
cd actions-runner
run.cmd
```

> You can enhance this script with pre-checks and logging.

---

## 🧰 Troubleshooting Tips

* Port 8080 inaccessible → Check Windows Firewall
* GitHub Actions stuck in queue → Ensure `run.cmd` is open in admin PowerShell
* Docker errors → Run Docker Desktop as Admin

---

## 🧾 Summary

This guide helps you:

* Set up a containerized Spring Boot + MySQL app on macOS
* Push it to GitHub
* Deploy it automatically to Windows 11 via a secure self-hosted runner
* Maintain full control without opening up inbound firewall ports

🛆 This workflow enables scalable, secure distribution of Docker apps across internal machines with minimal effort.

---

Let me know if you want a printable PDF or slideshow version!