|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: Docker Guide |
| 4 | +nav_order: 6 |
| 5 | +permalink: /docker-guide |
| 6 | +--- |
| 7 | + |
| 8 | +# Docker Guide |
| 9 | +{: .no_toc } |
| 10 | + |
| 11 | +This guide explains the Docker setup for the Clean Architecture Full-Stack application. It covers all Dockerfiles, docker-compose configurations, and environment variables. |
| 12 | + |
| 13 | +## Table of Contents |
| 14 | +{: .no_toc .text-delta } |
| 15 | + |
| 16 | +1. TOC |
| 17 | +{:toc} |
| 18 | + |
| 19 | +## Overview |
| 20 | + |
| 21 | +The application uses Docker to containerize all services for consistent development and deployment. The Docker setup includes: |
| 22 | + |
| 23 | +- Frontend container (Angular 19) |
| 24 | +- Backend API container (.NET 9) |
| 25 | +- Database container (PostgreSQL) |
| 26 | +- Nginx container (Reverse proxy) |
| 27 | + |
| 28 | +## Dockerfiles |
| 29 | + |
| 30 | +### Frontend Dockerfile |
| 31 | + |
| 32 | +Located at `frontend/Dockerfile`, this file builds the Angular application: |
| 33 | + |
| 34 | +```dockerfile |
| 35 | +# Build stage |
| 36 | +FROM node:20-alpine AS build |
| 37 | +WORKDIR /app |
| 38 | +COPY package*.json ./ |
| 39 | +RUN npm ci |
| 40 | +COPY . . |
| 41 | +RUN npm run build |
| 42 | + |
| 43 | +# Production stage |
| 44 | +FROM nginx:alpine |
| 45 | +COPY --from=build /app/dist/browser /usr/share/nginx/html |
| 46 | +COPY nginx.conf /etc/nginx/conf.d/default.conf |
| 47 | +EXPOSE 80 |
| 48 | +CMD ["nginx", "-g", "daemon off;"] |
| 49 | +``` |
| 50 | + |
| 51 | +This is a multi-stage build that: |
| 52 | +1. Uses Node.js to build the Angular application |
| 53 | +2. Copies the built files to an Nginx container |
| 54 | +3. Uses a custom Nginx configuration |
| 55 | +4. Exposes port 80 |
| 56 | + |
| 57 | +### Backend Dockerfile |
| 58 | + |
| 59 | +Located at `backend/src/Dockerfile`, this file builds the .NET API: |
| 60 | + |
| 61 | +```dockerfile |
| 62 | +# Build stage |
| 63 | +FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build |
| 64 | +WORKDIR /src |
| 65 | +COPY ["Contact.Api/Contact.Api.csproj", "Contact.Api/"] |
| 66 | +COPY ["Contact.Application/Contact.Application.csproj", "Contact.Application/"] |
| 67 | +COPY ["Contact.Domain/Contact.Domain.csproj", "Contact.Domain/"] |
| 68 | +COPY ["Contact.Infrastructure/Contact.Infrastructure.csproj", "Contact.Infrastructure/"] |
| 69 | +RUN dotnet restore "Contact.Api/Contact.Api.csproj" |
| 70 | +COPY . . |
| 71 | +WORKDIR "/src/Contact.Api" |
| 72 | +RUN dotnet build "Contact.Api.csproj" -c Release -o /app/build |
| 73 | + |
| 74 | +# Publish stage |
| 75 | +FROM build AS publish |
| 76 | +RUN dotnet publish "Contact.Api.csproj" -c Release -o /app/publish |
| 77 | + |
| 78 | +# Final stage |
| 79 | +FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS final |
| 80 | +WORKDIR /app |
| 81 | +COPY --from=publish /app/publish . |
| 82 | +ENTRYPOINT ["dotnet", "Contact.Api.dll"] |
| 83 | +``` |
| 84 | + |
| 85 | +This uses a multi-stage build to: |
| 86 | +1. Restore NuGet packages |
| 87 | +2. Build the .NET application |
| 88 | +3. Publish the application |
| 89 | +4. Create a lightweight runtime image |
| 90 | + |
| 91 | +### Nginx Dockerfile |
| 92 | + |
| 93 | +Located at `loadbalancer/Dockerfile`, this serves as the entry point for all requests: |
| 94 | + |
| 95 | +```dockerfile |
| 96 | +FROM nginx:alpine |
| 97 | +COPY nginx.conf /etc/nginx/conf.d/default.conf |
| 98 | +``` |
| 99 | + |
| 100 | +This simple Dockerfile: |
| 101 | +1. Uses the official Nginx Alpine image |
| 102 | +2. Replaces the default configuration with our custom one |
| 103 | + |
| 104 | +## Docker Compose Configuration |
| 105 | + |
| 106 | +The primary docker-compose file is `docker-compose.yml`: |
| 107 | + |
| 108 | +```yaml |
| 109 | +version: '3.8' |
| 110 | + |
| 111 | +services: |
| 112 | + frontend: |
| 113 | + build: |
| 114 | + context: ./frontend |
| 115 | + dockerfile: Dockerfile |
| 116 | + container_name: contact-frontend |
| 117 | + restart: always |
| 118 | + depends_on: |
| 119 | + - api |
| 120 | + networks: |
| 121 | + - contact-network |
| 122 | + |
| 123 | + api: |
| 124 | + build: |
| 125 | + context: ./backend/src |
| 126 | + dockerfile: Dockerfile |
| 127 | + container_name: contact-api |
| 128 | + restart: always |
| 129 | + depends_on: |
| 130 | + - postgres |
| 131 | + environment: |
| 132 | + - ASPNETCORE_ENVIRONMENT=Production |
| 133 | + - ConnectionStrings__DefaultConnection=${POSTGRES_CONNECTION_STRING} |
| 134 | + - JwtSettings__SecretKey=${JWT_SECRET_KEY} |
| 135 | + - JwtSettings__Issuer=${JWT_ISSUER} |
| 136 | + - JwtSettings__Audience=${JWT_AUDIENCE} |
| 137 | + - JwtSettings__ExpiryMinutes=${JWT_EXPIRY_MINUTES} |
| 138 | + networks: |
| 139 | + - contact-network |
| 140 | + |
| 141 | + postgres: |
| 142 | + image: postgres:16-alpine |
| 143 | + container_name: contact-postgres |
| 144 | + restart: always |
| 145 | + environment: |
| 146 | + - POSTGRES_USER=${POSTGRES_USER} |
| 147 | + - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} |
| 148 | + - POSTGRES_DB=${POSTGRES_DB} |
| 149 | + volumes: |
| 150 | + - postgres-data:/var/lib/postgresql/data |
| 151 | + - ./backend/db/init.sql:/docker-entrypoint-initdb.d/init.sql |
| 152 | + networks: |
| 153 | + - contact-network |
| 154 | + |
| 155 | + nginx: |
| 156 | + build: |
| 157 | + context: ./loadbalancer |
| 158 | + dockerfile: Dockerfile |
| 159 | + container_name: contact-nginx |
| 160 | + restart: always |
| 161 | + ports: |
| 162 | + - "80:80" |
| 163 | + depends_on: |
| 164 | + - frontend |
| 165 | + - api |
| 166 | + networks: |
| 167 | + - contact-network |
| 168 | + |
| 169 | +networks: |
| 170 | + contact-network: |
| 171 | + driver: bridge |
| 172 | + |
| 173 | +volumes: |
| 174 | + postgres-data: |
| 175 | +``` |
| 176 | +
|
| 177 | +This configuration: |
| 178 | +1. Defines all four services (frontend, api, postgres, nginx) |
| 179 | +2. Establishes service dependencies |
| 180 | +3. Sets environment variables from the `.env` file |
| 181 | +4. Configures networking |
| 182 | +5. Creates a persistent volume for PostgreSQL data |
| 183 | + |
| 184 | +## Environment Variables (.env file) |
| 185 | + |
| 186 | +The application uses a `.env` file (copied from `.env.example`) to configure various settings. Here's an explanation of each variable: |
| 187 | + |
0 commit comments