Overview
RealtimeChat includes full Docker support with a multi-stage Dockerfile for optimized production builds and a docker-compose configuration for easy deployment. This guide walks you through deploying the application using Docker.Dockerfile Architecture
The application uses a multi-stage Dockerfile based on .NET 9.0 that optimizes for both build time and image size:Dockerfile Stages
Base Stage
Base Stage
Uses the .NET 9.0 ASP.NET runtime image for minimal size. Exposes ports 8080 (HTTP) and 8081 (HTTPS).
Build Stage
Build Stage
Uses the .NET SDK to restore dependencies and build the application. Supports configurable build configuration via
BUILD_CONFIGURATION argument.Publish Stage
Publish Stage
Creates an optimized release build with framework-dependent deployment (no AppHost).
Final Stage
Final Stage
Copies the published application from the publish stage into the minimal runtime image.
Docker Compose Configuration
Thedocker-compose.yml file provides a simple deployment configuration:
The
extra_hosts configuration allows the container to access services running on the host machine (like a PostgreSQL database) via host.docker.internal.Step-by-Step Deployment
Prepare the Environment
Ensure you have Docker and Docker Compose installed on your system. Verify PostgreSQL is running and accessible.
Configure Database Connection
The Docker environment uses Update the connection string if your PostgreSQL instance uses different credentials.
appsettings.Docker.json which connects to PostgreSQL on the host:Run Database Migrations
Before starting the application, ensure your database schema is up to date:See the Database Setup guide for detailed migration instructions.
Start the Application
Launch the application using docker-compose:The
-d flag runs the container in detached mode.Environment Variables
The application can be configured using environment variables in the docker-compose file:| Variable | Description | Default |
|---|---|---|
ASPNETCORE_ENVIRONMENT | Application environment (Docker, Production) | Docker |
ConnectionStrings__DefaultConnectionString | Database connection string | See appsettings |
AllowCorsAddress | Allowed CORS origin | http://localhost:5173 |
Networking and Ports
Exposed Ports
- 8080: HTTP endpoint
- 8081: HTTPS endpoint (requires certificate configuration)
Accessing the Host Network
The Docker configuration includeshost.docker.internal mapping, which allows the container to connect to services on the host machine:
Production Considerations
Multi-Container Setup
For production deployments, run PostgreSQL in a separate container:Security Best Practices
Use Secrets
Never hardcode passwords in docker-compose files. Use environment variables or Docker secrets:
Health Checks
Add health checks to monitor container status:Logging
Configure logging drivers for centralized log management:Troubleshooting
Container fails to start
Container fails to start
Check logs for errors:Common issues:
- Database connection string is incorrect
- PostgreSQL is not accessible
- Migrations have not been run
Cannot connect to database
Cannot connect to database
Verify network connectivity:Test database connection from the container:
Port conflicts
Port conflicts
If port 8080 is already in use, remap the port in docker-compose.yml:
Next Steps
- Database Setup - Configure PostgreSQL and run migrations
- Configuration - Customize application settings
- API Reference - Explore available endpoints