feat: add dockerfile

main
Rohmad Eko Wahyudi 2025-09-16 12:17:23 +07:00
parent 96459df5ce
commit 1e19288b4e
No known key found for this signature in database
GPG Key ID: 4CCEDA68CB778BAF
6 changed files with 210 additions and 5 deletions

74
.dockerignore 100644
View File

@ -0,0 +1,74 @@
# .dockerignore - Exclude unnecessary files from Docker build context
# Git
.git
.gitignore
.gitattributes
# Documentation
*.md
README*
# IDE and editor files
.vscode/
.vs/
*.swp
*.swo
*~
# OS generated files
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
# Build outputs
bin/
obj/
out/
dist/
# Node modules (will be installed in container)
node_modules/
npm-debug.log*
# .NET
*.user
*.userosscache
*.sln.docstates
*.userprefs
# Test results
TestResults/
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
build/
bld/
[Bb]in/
[Oo]bj/
# Package files
*.nupkg
*.snupkg
# Logs
logs
*.log
# Environment files
.env
.env.local
.env.development.local
.env.test.local
.env.production.local
# Temporary files
*.tmp
*.temp

13
.env.example 100644
View File

@ -0,0 +1,13 @@
# Environment variables for Docker Compose
# Copy this file to .env and update the values
# Application Configuration
ASPNETCORE_ENVIRONMENT=Production
ASPNETCORE_URLS=http://+:8080
# Logging
LOG_LEVEL=Information
# Optional: External service configurations
# API_KEY=your_api_key_here
# EXTERNAL_SERVICE_URL=https://api.example.com

67
Dockerfile 100644
View File

@ -0,0 +1,67 @@
# Multi-stage Dockerfile for .NET 9 ASP.NET Core application with pnpm
# Stage 1: Base runtime image
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base
WORKDIR /app
EXPOSE 8080
EXPOSE 8081
# Create non-root user for security
RUN groupadd -r appgroup && useradd -r -g appgroup appuser
# Stage 2: Node.js build environment for frontend assets
FROM node:20-alpine AS node-build
WORKDIR /src
# Install pnpm globally
RUN npm install -g pnpm
# Copy package files and install dependencies
COPY package.json pnpm-lock.yaml ./
RUN pnpm install --frozen-lockfile
# Copy source files and build CSS
COPY . .
RUN pnpm run build
# Stage 3: .NET SDK for building the application
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
WORKDIR /src
# Copy project file and restore dependencies
COPY *.csproj .
RUN dotnet restore "BankSampahApp.csproj"
# Copy source code
COPY . .
# Copy built frontend assets from node-build stage
COPY --from=node-build /src/wwwroot/css/site.css ./wwwroot/css/
# Build the application
RUN dotnet build "BankSampahApp.csproj" -c Release -o /app/build
# Stage 4: Publish the application
FROM build AS publish
RUN dotnet publish "BankSampahApp.csproj" -c Release -o /app/publish /p:UseAppHost=false
# Stage 5: Final runtime image
FROM base AS final
WORKDIR /app
# Copy published application
COPY --from=publish /app/publish .
# Change ownership to non-root user
RUN chown -R appuser:appgroup /app
USER appuser
# Configure environment
ENV ASPNETCORE_ENVIRONMENT=Production
ENV ASPNETCORE_URLS=http://+:8080
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD curl -f http://localhost:8080/health || exit 1
# Entry point
ENTRYPOINT ["dotnet", "BankSampahApp.dll"]

View File

@ -48,7 +48,7 @@ bank-sampah/
### Prerequisites
- .NET 9.0 SDK atau lebih baru
- Node.js 18+ dan npm (untuk Tailwind CSS)
- Node.js 18+ dan pnpm (untuk Tailwind CSS)
- Visual Studio 2022 17.8+ atau VS Code dengan C# extension
### Langkah Instalasi
@ -61,12 +61,12 @@ bank-sampah/
2. **Install dependencies Node.js**
```bash
npm install
pnpm install
```
3. **Build CSS dengan Tailwind**
```bash
npm run build-css
pnpm run build-css
```
4. **Restore .NET packages**
@ -88,7 +88,7 @@ bank-sampah/
```bash
# Terminal 1: Watch CSS changes
npm run build-css
pnpm run build-css
# Terminal 2: Run aplikasi dengan hot reload
dotnet watch run
@ -100,7 +100,7 @@ dotnet watch run
```bash
# Build optimized CSS
npm run build
pnpm run build
# Publish aplikasi .NET
dotnet publish -c Release -o ./publish

BIN
README.pdf 100644

Binary file not shown.

51
docker-compose.yml 100644
View File

@ -0,0 +1,51 @@
version: '3.8'
services:
# Main application service
bank-sampah-app:
build:
context: .
dockerfile: Dockerfile
target: final
container_name: bank-sampah-app
ports:
- "8080:8080"
- "8081:8081"
environment:
- ASPNETCORE_ENVIRONMENT=Production
- ASPNETCORE_URLS=http://+:8080
volumes:
# Mount configuration files (optional)
- ./appsettings.Production.json:/app/appsettings.Production.json:ro
# Mount logs directory
- ./logs:/app/logs
networks:
- bank-sampah-network
restart: unless-stopped
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
# Nginx reverse proxy (optional)
nginx:
image: nginx:alpine
container_name: bank-sampah-nginx
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- ./nginx/ssl:/etc/nginx/ssl:ro
- ./logs/nginx:/var/log/nginx
networks:
- bank-sampah-network
restart: unless-stopped
depends_on:
- bank-sampah-app
networks:
bank-sampah-network:
driver: bridge