Skip to main content
Docker Compose provides a simple way to run the entire Blackjack API stack with all dependencies in a single command.

docker-compose.yml

The project includes a complete Docker Compose configuration:
services:
  mysql:
    image: mysql:8.0
    container_name: blackjack-mysql
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: blackjack
      MYSQL_USER: blackjack
      MYSQL_PASSWORD: blackjack
    ports:
      - "3307:3306"
    volumes:
      - mysql_data:/var/lib/mysql
    healthcheck:
      test: [ "CMD", "mysqladmin", "ping", "-h", "localhost", "-uroot", "-proot" ]
      interval: 5s
      timeout: 3s
      retries: 20


  mongo:
    image: mongo:7
    container_name: blackjack-mongo
    ports:
      - "27017:27017"
    volumes:
      - mongo_data:/data/db
    healthcheck:
      test: [ "CMD", "mongosh", "--eval", "db.adminCommand('ping')" ]
      interval: 5s
      timeout: 3s
      retries: 20

  api:
    image: ccasr/blackjack-api:latest
    container_name: blackjack-api
    ports:
      - "8080:8080"
    environment:
      SPRING_PROFILES_ACTIVE: docker
    depends_on:
      mysql:
        condition: service_healthy
      mongo:
        condition: service_healthy

volumes:
  mysql_data:
  mongo_data:

Services Explained

MySQL Service

Stores player statistics and ranking data. Configuration:
  • Image: mysql:8.0
  • Port Mapping: Host port 3307 → Container port 3306
    • Uses 3307 on host to avoid conflicts with local MySQL installations
  • Database: Automatically creates blackjack database
  • Credentials:
    • Root password: root
    • User: blackjack
    • Password: blackjack
  • Persistence: Data is stored in mysql_data named volume
  • Health Check: Waits up to 100 seconds (20 retries × 5s interval) for MySQL to be ready

MongoDB Service

Stores game state, deck, and hands. Configuration:
  • Image: mongo:7
  • Port Mapping: Host port 27017 → Container port 27017
  • Persistence: Data is stored in mongo_data named volume
  • Health Check: Uses mongosh to verify the database is responding

API Service

The main Spring Boot application. Configuration:
  • Image: ccasr/blackjack-api:latest (pre-built from Docker Hub)
  • Port Mapping: Host port 8080 → Container port 8080
  • Environment: Uses docker profile which configures:
    • MongoDB URI: mongodb://mongo:27017/blackjack
    • MySQL connection: r2dbc:mysql://mysql:3306/blackjack
  • Dependencies: Waits for both MySQL and MongoDB health checks before starting

Starting the Stack

Start all services in detached mode:
docker compose up -d
With build (if you modified the code):
docker compose up -d --build
View logs:
docker compose logs -f
View logs for specific service:
docker compose logs -f api

Stopping the Stack

Stop all services:
docker compose down
Stop and remove volumes (deletes all data):
docker compose down -v

Managing Services

View Running Services

docker compose ps

Restart a Service

docker compose restart api

View Service Logs

# All services
docker compose logs

# Specific service
docker compose logs mysql
docker compose logs mongo
docker compose logs api

# Follow logs
docker compose logs -f api

Execute Commands in Services

# MySQL
docker compose exec mysql mysql -u blackjack -pblackjack blackjack

# MongoDB
docker compose exec mongo mongosh blackjack

# API
docker compose exec api /bin/bash

Environment Variables

Docker Profile Configuration

When SPRING_PROFILES_ACTIVE=docker, the API uses these settings (from application-docker.yml):
spring:
  r2dbc:
    url: r2dbc:mysql://mysql:3306/blackjack
    username: blackjack
    password: blackjack

  flyway:
    enabled: true
    url: jdbc:mysql://mysql:3306/blackjack
    user: blackjack
    password: blackjack
    locations: classpath:db/migration

  data:
    mongodb:
      uri: mongodb://mongo:27017/blackjack

Custom Environment Variables

You can override settings by adding environment variables to the api service:
api:
  image: ccasr/blackjack-api:latest
  environment:
    SPRING_PROFILES_ACTIVE: docker
    SERVER_PORT: 8080
    LOGGING_LEVEL_ROOT: INFO

Data Persistence

Named Volumes

Docker Compose creates two named volumes:
  • mysql_data - MySQL database files
  • mongo_data - MongoDB database files
These volumes persist data even when containers are stopped or removed.

Inspect Volumes

docker volume ls
docker volume inspect blackjack-api_mysql_data
docker volume inspect blackjack-api_mongo_data

Backup Data

MySQL Backup

docker compose exec mysql mysqldump -u blackjack -pblackjack blackjack > backup.sql

MongoDB Backup

docker compose exec mongo mongodump --db blackjack --out /tmp/backup
docker compose cp mongo:/tmp/backup ./mongo-backup

Restore Data

MySQL Restore

docker compose exec -T mysql mysql -u blackjack -pblackjack blackjack < backup.sql

MongoDB Restore

docker compose cp ./mongo-backup mongo:/tmp/restore
docker compose exec mongo mongorestore --db blackjack /tmp/restore/blackjack

Health Checks

The compose file includes health checks for databases:

MySQL Health Check

healthcheck:
  test: [ "CMD", "mysqladmin", "ping", "-h", "localhost", "-uroot", "-proot" ]
  interval: 5s
  timeout: 3s
  retries: 20
This ensures MySQL is fully ready before the API starts.

MongoDB Health Check

healthcheck:
  test: [ "CMD", "mongosh", "--eval", "db.adminCommand('ping')" ]
  interval: 5s
  timeout: 3s
  retries: 20
This verifies MongoDB is accepting connections.

Testing the Stack

Once all services are running:

1. Check Service Health

docker compose ps
All services should show status “Up (healthy)“.

2. Test API Endpoints

# Health check
curl http://localhost:8080/actuator/health

# Create a new game
curl -X POST http://localhost:8080/game/new

# View ranking
curl http://localhost:8080/ranking

3. Access Swagger UI

Open in your browser:
http://localhost:8080/swagger-ui.html

4. Verify Database Connections

# Check MySQL tables
docker compose exec mysql mysql -u blackjack -pblackjack -e "USE blackjack; SHOW TABLES;"

# Check MongoDB collections
docker compose exec mongo mongosh --eval "use blackjack; db.getCollectionNames();"

Troubleshooting

Container Won’t Start

Check logs:
docker compose logs api

Database Connection Issues

Verify databases are healthy:
docker compose ps
Test connectivity:
# Test MySQL
docker compose exec api ping mysql

# Test MongoDB
docker compose exec api ping mongo

Port Already in Use

Change host port in docker-compose.yml:
api:
  ports:
    - "9090:8080"  # Use port 9090 instead

Reset Everything

Stop and remove all containers, volumes, and networks:
docker compose down -v
docker compose up -d

Production Considerations

Security

For production deployments:
  1. Change Default Passwords: Don’t use default credentials
  2. Use Secrets: Store credentials in Docker secrets or environment files
  3. Network Isolation: Use custom networks to isolate services
  4. Remove Port Bindings: Don’t expose database ports externally

Example Production Configuration

services:
  mysql:
    environment:
      MYSQL_ROOT_PASSWORD_FILE: /run/secrets/mysql_root_password
      MYSQL_PASSWORD_FILE: /run/secrets/mysql_password
    secrets:
      - mysql_root_password
      - mysql_password
    ports: []  # Don't expose externally

secrets:
  mysql_root_password:
    external: true
  mysql_password:
    external: true

Next Steps

Build docs developers (and LLMs) love