Overview
Opiinix Trade uses Docker for containerizing infrastructure services and the entire application. The project includes:- docker-compose.yml - Infrastructure services (PostgreSQL, Redis)
- Dockerfile - Application containerization
Docker provides consistent development environments and simplifies deployment.
Infrastructure Services
Docker Compose Configuration
Thedocker-compose.yml file defines two essential services:
Services Breakdown
TimescaleDB (PostgreSQL)
TimescaleDB (PostgreSQL)
Image:
timescale/timescaledb:latest-pg12TimescaleDB is a PostgreSQL extension optimized for time-series data, perfect for tracking order book history and price movements.Configuration:- Port: 5432 (mapped to host)
- User:
dev - Password:
dev - Database:
repo - Volume:
timescale-data(persistent storage) - Restart: Always
- Time-series optimization for historical data
- Built on PostgreSQL (full SQL support)
- Efficient for storing order history
- Better compression for time-series data
Redis
Redis
Image:
redis:latestRedis serves multiple purposes in Opinix Trade:- Order queue (BullMQ)
- Pub/sub for WebSocket messages
- Caching layer
- Port: 6379 (mapped to host)
- Volume:
cache(persistent storage) - Restart: Always
- Queue management for asynchronous order processing
- Real-time pub/sub for WebSocket updates
- Session storage
- Rate limiting
Starting Infrastructure
Start services
Launch all infrastructure services:The
-d flag runs containers in detached mode (background).Application Dockerfile
The mainDockerfile containerizes the entire Opinix Trade application:
Dockerfile Breakdown
Base Image
Base Image
- Uses Node.js 20 on Alpine Linux
- Alpine provides a minimal image (~5MB base)
- Reduces container size and attack surface
Build Arguments
Build Arguments
DATABASE_URL as a build argument for Prisma client generation.File Copying
File Copying
- Core packages (db, order-queue, types, etc.)
- Client and server apps
- Root configuration files
Services (engine, wss) are not included in this Dockerfile. Consider separate containers for production.
Installation & Build
Installation & Build
- Install all dependencies
- Generate Prisma client with provided DATABASE_URL
- Build all packages and apps using Turborepo
Port Exposure
Port Exposure
- Port 3000: Next.js client
- Port 3001: Express server
Building the Docker Image
- Development Build
- Production Build
Build the Docker image for development:
Use
host.docker.internal to access localhost from inside the container.Running the Container
- Basic Run
- With Environment Variables
- With Network
Docker Compose for Full Stack
Create a complete docker-compose setup including the application:docker-compose.full.yml
Production Considerations
Multi-stage Builds
Use multi-stage Dockerfile to separate build and runtime:
Separate Services
Create separate containers for each service:
- Client container
- Server container
- Engine container
- WebSocket container
- Independent scaling
- Easier updates
- Better resource allocation
Health Checks
Add health checks to containers:
Security
Production security practices:
- Run as non-root user
- Scan for vulnerabilities
- Use specific image tags
- Minimal base images
- No secrets in images
Docker Commands Reference
Container Management
Container Management
Data Management
Data Management
Debugging
Debugging
Troubleshooting
Port Conflicts
Port Conflicts
Error:
Bind for 0.0.0.0:5432 failed: port is already allocatedSolutions:- Stop local PostgreSQL:
sudo service postgresql stop - Change port mapping in docker-compose.yml:
- Find and kill process using port:
lsof -ti:5432 | xargs kill -9
Container Won't Start
Container Won't Start
Solutions:
- Check logs:
docker-compose logs timescaledb - Remove and recreate:
docker-compose down && docker-compose up -d - Remove volumes:
docker-compose down -v(WARNING: deletes data) - Check disk space:
df -h
Database Connection Issues
Database Connection Issues
Error:
Can't reach database serverSolutions:- Ensure container is running:
docker-compose ps - Wait for initialization (first start takes time)
- Use correct hostname:
- From host:
localhost:5432 - From container:
timescaledb:5432
- From host:
- Check DATABASE_URL format
Build Failures
Build Failures
Solutions:
- Clear Docker cache:
docker builder prune - Build without cache:
docker build --no-cache . - Check available disk space
- Ensure DATABASE_URL is provided:
--build-arg DATABASE_URL=...