Aller au contenu principal

Docker Deployment Configuration

Overview​

The Movie List Application uses Docker and Docker Compose to containerize all components, ensuring consistent deployment across different environments. This setup includes three main services: the React frontend, Spring Boot backend, and MongoDB database.

Docker Compose Configuration​

Main Configuration (docker-compose.yml)​

version: '3.8'

services:
movie-list-front:
container_name: movie-list-front
build:
context: ./movie-list-front
dockerfile: Dockerfile
ports:
- "3000:80"
depends_on:
- movie-api

mongo-local:
image: mongo:latest
container_name: mongo-local
ports:
- "27017:27017"
volumes:
- ./mongo-init:/docker-entrypoint-initdb.d

movie-api:
image: movie-api:latest
build: ./movie-api
container_name: movie-api
environment:
- SPRING_PROFILES_ACTIVE=local
ports:
- "8080:8080"
depends_on:
- mongo-local

Service Breakdown​

1. Frontend Service (movie-list-front)​

Configuration Details:

  • Container Name: movie-list-front
  • Build Context: ./movie-list-front directory
  • Port Mapping: Host port 3000 → Container port 80
  • Dependencies: Waits for movie-api to start

Key Features:

  • Uses custom Dockerfile for React application
  • Exposes the application on port 3000 for local development
  • Depends on backend API being available

2. Backend Service (movie-api)​

Configuration Details:

  • Container Name: movie-api
  • Image Name: movie-api:latest
  • Build Context: ./movie-api directory
  • Port Mapping: Host port 8080 → Container port 8080
  • Environment: SPRING_PROFILES_ACTIVE=local
  • Dependencies: Waits for mongo-local to start

Environment Variables:

  • SPRING_PROFILES_ACTIVE=local: Tells Spring Boot to use local configuration
    • Uses application-local.properties for database connection
    • Connects to MongoDB container named mongo-local

3. Database Service (mongo-local)​

Configuration Details:

  • Container Name: mongo-local
  • Image: mongo:latest (official MongoDB image)
  • Port Mapping: Host port 27017 → Container port 27017
  • Volume Mount: ./mongo-init:/docker-entrypoint-initdb.d

Initialization Process:

  • MongoDB automatically executes scripts in /docker-entrypoint-initdb.d
  • The mongo-init folder contains initialization scripts and data
  • Database is populated with movie data on first startup

Container Dependencies​

The services start in a specific order due to dependency configuration:

1. mongo-local (starts first)
2. movie-api (waits for mongo-local)
3. movie-list-front (waits for movie-api)

Why Dependencies Matter:

  • Database First: MongoDB must be ready before the API tries to connect
  • API Before Frontend: Frontend needs the API endpoints to be available
  • Health Checks: Docker Compose ensures dependent services are running

Port Configuration​

ServiceHost PortContainer PortPurpose
Frontend300080React application access
Backend80808080Spring Boot API endpoints
Database2701727017MongoDB connection

Port Mapping Explanation:

  • Host Port: Port accessible from your local machine
  • Container Port: Port the service listens on inside the container
  • Frontend: Maps to port 80 (standard HTTP) inside container, exposed as 3000 externally

Volume Configuration​

Database Initialization​

volumes:
- ./mongo-init:/docker-entrypoint-initdb.d

Purpose:

  • Mounts local mongo-init folder into MongoDB container
  • Contains init-mongo.js script and data.json file
  • Automatically populates database with movie data

Optional Data Persistence​

# Commented out by default
# volumes:
# - mongo-local-data:/data/db

When Enabled:

  • Creates a named Docker volume for MongoDB data
  • Data survives container restarts and rebuilds
  • Useful for development when you want to keep database changes

Build Process​

Development Build​

docker-compose up --build

What Happens:

  1. Builds Custom Images: Creates Docker images for frontend and backend
  2. Pulls Base Images: Downloads MongoDB image if not present
  3. Creates Network: Sets up internal Docker network for service communication
  4. Starts Services: Launches all containers in dependency order
  5. Initializes Database: Runs MongoDB initialization scripts

Production Considerations​

Environment Variables:

  • Production would use SPRING_PROFILES_ACTIVE=prod
  • Would connect to external MongoDB service (like MongoDB Atlas)
  • Frontend would be built with production optimizations

Security:

  • Remove development headers and CORS configurations
  • Use environment-specific database credentials
  • Implement proper authentication and authorization

Network Communication​

Internal Docker Network​

Docker Compose automatically creates a network where services can communicate using container names:

  • Frontend → Backend: http://movie-api:8080
  • Backend → Database: mongodb://mongo-local:27017

External Access​

From your local machine:

  • Frontend: http://localhost:3000
  • Backend API: http://localhost:8080
  • Database: mongodb://localhost:27017

Common Commands​

Start All Services​

docker-compose up --build

Start in Background​

docker-compose up -d --build

Stop All Services​

docker-compose down

View Logs​

docker-compose logs [service-name]

Rebuild Specific Service​

docker-compose build [service-name]

Benefits of This Docker Setup​

  1. Consistency: Same environment across development, testing, and production
  2. Isolation: Each service runs in its own container
  3. Easy Setup: Single command starts entire application stack
  4. Scalability: Services can be scaled independently
  5. Development Speed: No need to install dependencies locally
  6. Version Control: Infrastructure configuration is code-managed
  7. Clean Environment: Easy to reset to fresh state

Troubleshooting​

Common Issues​

  1. Port Conflicts: Ensure ports 3000, 8080, and 27017 are available
  2. Build Failures: Check Dockerfile syntax and dependencies
  3. Database Connection: Verify MongoDB is fully started before API
  4. CORS Issues: Ensure frontend and backend can communicate

Debugging Commands​

# Check running containers
docker ps

# View container logs
docker logs [container-name]

# Execute commands in container
docker exec -it [container-name] /bin/bash

# Check network connectivity
docker network ls