add react frontend to docker

This commit is contained in:
Evan
2026-01-17 11:29:50 +05:00
parent 8b8a7949ee
commit daf5629d23
4 changed files with 55 additions and 16 deletions
-1
View File
@@ -15,7 +15,6 @@
**/bin **/bin
**/charts **/charts
**/docker-compose* **/docker-compose*
**/Dockerfile*
**/node_modules **/node_modules
**/npm-debug.log **/npm-debug.log
**/obj **/obj
+23 -13
View File
@@ -13,8 +13,8 @@ This project uses Docker Compose to run both the frontend and API in containers
┌─────────────────────────────────────────┐ ┌─────────────────────────────────────────┐
Nginx Container Frontend Container (Nginx)
│ - Serves static frontend files │ - Serves built React/Vite app
│ - Proxies /api/* to backend │ │ - Proxies /api/* to backend │
│ - Proxies /swagger to backend │ │ - Proxies /swagger to backend │
└────────────────┬────────────────────────┘ └────────────────┬────────────────────────┘
@@ -45,7 +45,7 @@ docker compose up -d
### 2. Access the Application ### 2. Access the Application
- **Frontend**: http://localhost:9755 - **Frontend**: http://localhost:9755
- **API** (via proxy): http://localhost:9755/api/* - **API** (via proxy): http://localhost:9755/api/\*
- **Swagger UI**: http://localhost:9755/swagger - **Swagger UI**: http://localhost:9755/swagger
### 3. View Logs ### 3. View Logs
@@ -67,10 +67,10 @@ docker compose down
## Making Changes ## Making Changes
### Frontend Changes (HTML/CSS/JS) ### Frontend Changes (React/TypeScript)
1. Edit files in the `Frontend/` directory 1. Edit files in the `frontend-react/src/` directory
2. Rebuild the nginx container: 2. Rebuild the frontend container:
```bash ```bash
docker compose build nginx && docker compose up -d docker compose build nginx && docker compose up -d
``` ```
@@ -147,6 +147,7 @@ docker compose build submission.api
The API connects to MongoDB on your host machine using `host.docker.internal:27017`. The API connects to MongoDB on your host machine using `host.docker.internal:27017`.
To change the MongoDB connection: To change the MongoDB connection:
1. Edit `compose.yaml` 1. Edit `compose.yaml`
2. Update the `MongoDbSettings__ConnectionString` environment variable 2. Update the `MongoDbSettings__ConnectionString` environment variable
3. Restart containers 3. Restart containers
@@ -159,20 +160,22 @@ environment:
### Port Configuration ### Port Configuration
The application is exposed on port 9755. To change this: The application is exposed on port 9755. To change this:
1. Edit `compose.yaml` 1. Edit `compose.yaml`
2. Update the nginx ports mapping: 2. Update the nginx ports mapping:
```yaml ```yaml
ports: ports:
- "9755:80" # Change 9755 to your desired port - "9755:80" # Change 9755 to your desired port
``` ```
3. Restart containers 3. Restart containers
### API Base URL (Frontend) ### API Base URL (Frontend)
The frontend API configuration is in `Frontend/index.html` at line 105: The frontend API configuration is in `Frontend/index.html` at line 105:
```javascript ```javascript
const API_CONFIG = { const API_CONFIG = {
baseUrl: '' // Empty for same-origin requests baseUrl: "", // Empty for same-origin requests
}; };
``` ```
@@ -184,12 +187,13 @@ This is set to empty because Nginx proxies all `/api/*` requests to the backend.
. .
├── compose.yaml # Docker Compose configuration ├── compose.yaml # Docker Compose configuration
├── nginx/ ├── nginx/
│ ├── Dockerfile # Nginx container definition
│ └── nginx.conf # Nginx configuration │ └── nginx.conf # Nginx configuration
├── Frontend/ # Static frontend files ├── frontend-react/ # React/Vite frontend
│ ├── index.html │ ├── Dockerfile # Multi-stage build (Node + Nginx)
│ ├── style.css │ ├── src/ # React source files
── fonts/ ── public/ # Static assets
│ └── dist/ # Built output (generated)
├── Frontend/ # Legacy static frontend (deprecated)
└── Submission.Api/ └── Submission.Api/
├── Dockerfile # API container definition ├── Dockerfile # API container definition
└── ... # .NET source files └── ... # .NET source files
@@ -202,6 +206,7 @@ This is set to empty because Nginx proxies all `/api/*` requests to the backend.
**Problem**: API can't connect to MongoDB on host **Problem**: API can't connect to MongoDB on host
**Solution**: **Solution**:
1. Verify MongoDB is running on host: `mongosh --eval "db.version()"` 1. Verify MongoDB is running on host: `mongosh --eval "db.version()"`
2. Check MongoDB is listening on `0.0.0.0:27017` not just `127.0.0.1` 2. Check MongoDB is listening on `0.0.0.0:27017` not just `127.0.0.1`
3. Check Windows Firewall isn't blocking the connection 3. Check Windows Firewall isn't blocking the connection
@@ -212,6 +217,7 @@ This is set to empty because Nginx proxies all `/api/*` requests to the backend.
**Problem**: Error binding to port 9755 **Problem**: Error binding to port 9755
**Solution**: **Solution**:
1. Find what's using the port: `netstat -ano | findstr :9755` 1. Find what's using the port: `netstat -ano | findstr :9755`
2. Either stop that process or change the port in `compose.yaml` 2. Either stop that process or change the port in `compose.yaml`
@@ -220,6 +226,7 @@ This is set to empty because Nginx proxies all `/api/*` requests to the backend.
**Problem**: Container crashes on startup **Problem**: Container crashes on startup
**Solution**: **Solution**:
```bash ```bash
# Check logs for errors # Check logs for errors
docker compose logs submission.api docker compose logs submission.api
@@ -237,6 +244,7 @@ docker compose up -d
**Problem**: Code changes don't appear after rebuild **Problem**: Code changes don't appear after rebuild
**Solution**: **Solution**:
```bash ```bash
# Force rebuild and restart # Force rebuild and restart
docker compose down docker compose down
@@ -249,6 +257,7 @@ docker compose up -d
**Problem**: Nginx can't reach the API **Problem**: Nginx can't reach the API
**Solution**: **Solution**:
1. Check API container is running: `docker compose ps` 1. Check API container is running: `docker compose ps`
2. Check API logs: `docker compose logs submission.api` 2. Check API logs: `docker compose logs submission.api`
3. Verify API is listening on port 8080 3. Verify API is listening on port 8080
@@ -292,6 +301,7 @@ services:
## Support ## Support
For issues or questions: For issues or questions:
1. Check container logs: `docker compose logs -f` 1. Check container logs: `docker compose logs -f`
2. Verify all services are running: `docker compose ps` 2. Verify all services are running: `docker compose ps`
3. Review this documentation 3. Review this documentation
+2 -2
View File
@@ -27,10 +27,10 @@
- PetitionSettings__AllowPetitionCreation=true - PetitionSettings__AllowPetitionCreation=true
nginx: nginx:
image: petition-nginx image: petition-frontend
build: build:
context: . context: .
dockerfile: nginx/Dockerfile dockerfile: frontend-react/Dockerfile
ports: ports:
- "9755:80" - "9755:80"
depends_on: depends_on:
+30
View File
@@ -0,0 +1,30 @@
# Build stage - compile the React/Vite app
FROM node:20-alpine AS builder
WORKDIR /app
# Copy package files first for better caching
COPY frontend-react/package*.json ./
# Install dependencies
RUN npm ci
# Copy source files
COPY frontend-react/ ./
# Build the production bundle
RUN npm run build
# Production stage - serve with Nginx
FROM nginx:alpine
# Copy custom nginx configuration
COPY nginx/nginx.conf /etc/nginx/nginx.conf
# Copy built React app from builder stage
COPY --from=builder /app/dist /usr/share/nginx/html/
# Expose port 80
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]