This guide explains how to use Docker and Docker Compose to run the Code Sharing Platform.
- Docker Desktop (Download)
- Docker Compose (included with Docker Desktop)
- Git
git clone <repository-url>
cd code-sharing-platform# Build images and start containers
docker-compose up -d
# View logs
docker-compose logs -f
# View specific service logs
docker-compose logs -f backend
docker-compose logs -f frontend- Frontend: http://localhost
- Backend API: http://localhost:8080/api
- GraphQL Playground: http://localhost:8080/api/graphiql
- PostgreSQL: localhost:5432
- MongoDB: localhost:27017
# Stop all containers
docker-compose down
# Stop and remove volumes
docker-compose down -v- Image: postgres:16-alpine
- Port: 5432
- Database: code_sharing_platform
- Username: postgres
- Password: postgres
- Volume: postgres_data
- Image: mongo:7-alpine
- Port: 27017
- Database: code_sharing_platform
- Username: root
- Password: password
- Volume: mongo_data
- Build: Multi-stage build with Maven
- Port: 8080
- Framework: Spring Boot
- JDK: 21
- Build: Multi-stage build with Node and Nginx
- Port: 80
- Framework: React + Vite
- Server: Nginx
docker-compose ps# Backend shell
docker-compose exec backend sh
# Frontend shell (nginx)
docker-compose exec frontend sh
# PostgreSQL CLI
docker-compose exec postgres psql -U postgres -d code_sharing_platform
# MongoDB CLI
docker-compose exec mongodb mongosh# All services
docker-compose logs
# Specific service
docker-compose logs backend
# Follow logs
docker-compose logs -f backend
# Last 100 lines
docker-compose logs --tail=100# Rebuild specific service
docker-compose build backend
docker-compose build frontend
# Rebuild all services
docker-compose build
# Rebuild and start
docker-compose up -d --build# Stop and remove containers
docker-compose down
# Stop, remove containers, and volumes
docker-compose down -v
# Remove images too
docker-compose down -v --rmi allFor development with hot reload, you can mount local volumes:
# Modify docker-compose.yml
services:
backend:
volumes:
- ./backend/src:/app/src
- ./backend/pom.xml:/app/pom.xmlThen run:
docker-compose up -dThe backend will detect changes and reload.
# Add to docker-compose.yml backend service
environment:
JAVA_TOOL_OPTIONS: -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005
ports:
- "5005:5005"Then connect your IDE debugger to localhost:5005
# Connect to PostgreSQL
docker-compose exec postgres psql -U postgres -d code_sharing_platform
# Connect to MongoDB
docker-compose exec mongodb mongosh# Check logs
docker-compose logs backend
# Rebuild from scratch
docker-compose down -v --rmi all
docker-compose up -d --build# Find process using port
lsof -i :8080
lsof -i :80
lsof -i :5432
lsof -i :27017
# Stop the process
kill -9 <PID>
# Or change port in docker-compose.yml# Check database is running
docker-compose ps
# Check logs
docker-compose logs postgres
docker-compose logs mongodb
# Test connection
docker-compose exec postgres pg_isready
docker-compose exec mongodb mongosh --eval "db.adminCommand('ping')"# Increase Docker Desktop resources:
# Mac: Preferences > Resources > Memory
# Windows: Settings > Resources > Memory
# Linux: Not applicable (uses system memory)For production, consider:
- Use environment variables:
docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d-
Health Checks: Already configured in docker-compose.yml
-
Resource Limits:
services:
backend:
deploy:
resources:
limits:
cpus: '2'
memory: 1G
reservations:
cpus: '1'
memory: 512M- Logging:
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "3"- Restart Policy:
restart_policy:
condition: on-failure
delay: 5s
max_attempts: 3
window: 120sServices communicate through the code-sharing-network bridge network:
- Frontend → Backend: http://backend:8080
- Backend → PostgreSQL: jdbc:postgresql://postgres:5432
- Backend → MongoDB: mongodb://root:password@mongodb:27017
- Multi-stage Builds: Reduces image size
- Alpine Images: Smaller base images
- Layer Caching: Order Dockerfile commands by frequency of change
- Volume Mounts: Use bind mounts for development, named volumes for databases
- Health Checks: Ensure services are ready before starting dependents