Complete guide to building and deploying a full-stack application with ASP.NET Core 8 Web API, React frontend, and Docker containerization.
Step 1: Create ASP.NET Core API
# Create Web API
dotnet new webapi -n FullStackApp --no-https
cd FullStackApp
# Add packages
dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.AspNetCore.Cors
# Create model
public class TodoItem
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public bool IsCompleted { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime? CompletedAt { get; set; }
}
Step 2: Create React Frontend
# Create React app with TypeScript
npx create-react-app client --template typescript
cd client
# Install dependencies
npm install axios react-router-dom @types/react-router-dom
npm install @mui/material @emotion/react @emotion/styled
# Create API service
// src/api/todoApi.ts
import axios from 'axios';
const api = axios.create({
baseURL: 'http://localhost:5000/api',
headers: {
'Content-Type': 'application/json'
}
});
export const todoApi = {
getAll: () => api.get('/todos'),
getById: (id: number) => api.get(`/todos/${id}`),
create: (todo: any) => api.post('/todos', todo),
update: (id: number, todo: any) => api.put(`/todos/${id}`, todo),
delete: (id: number) => api.delete(`/todos/${id}`)
};
Step 3: Dockerize Both
# Dockerfile for API FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build WORKDIR /src COPY FullStackApp.csproj . RUN dotnet restore COPY . . RUN dotnet publish -c Release -o /app/publish FROM mcr.microsoft.com/dotnet/aspnet:8.0 WORKDIR /app COPY --from=build /app/publish . EXPOSE 80 ENTRYPOINT ["dotnet", "FullStackApp.dll"] # Dockerfile for React FROM node:18-alpine AS build WORKDIR /app COPY client/package.json client/package-lock.json ./ RUN npm ci COPY client/ . RUN npm run build FROM nginx:alpine COPY --from=build /app/build /usr/share/nginx/html COPY nginx.conf /etc/nginx/conf.d/default.conf EXPOSE 80
Step 4: Docker Compose
# docker-compose.yml
version: '3.8'
services:
postgres:
image: postgres:16-alpine
container_name: fullstack-db
environment:
POSTGRES_USER: appuser
POSTGRES_PASSWORD: apppass
POSTGRES_DB: fullstack
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U appuser"]
interval: 10s
timeout: 5s
retries: 5
api:
build: ./FullStackApp
container_name: fullstack-api
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ConnectionStrings__DefaultConnection=Host=postgres;Port=5432;Database=fullstack;Username=appuser;Password=apppass
ports:
- "5000:80"
depends_on:
postgres:
condition: service_healthy
restart: unless-stopped
client:
build: ./client
container_name: fullstack-client
ports:
- "3000:80"
depends_on:
- api
restart: unless-stopped
volumes:
postgres_data:
Step 5: Nginx Configuration
# nginx.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /api/ {
proxy_pass http://api:80/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Step 6: Run Everything
# Build and start
docker-compose up --build
# Access the app
# Frontend: http://localhost:3000
# Backend API: http://localhost:5000/api/todos
# Database: localhost:5432
# Test the API
curl http://localhost:5000/api/todos
curl -X POST http://localhost:5000/api/todos \
-H "Content-Type: application/json" \
-d '{"title":"Test Todo","isCompleted":false}'
# View logs
docker-compose logs -f api
docker-compose logs -f client
# Stop all
docker-compose down
