Skip to main content

Overview

Showdown Trivia includes a complete Docker Compose setup that runs:
  • Application: Go web server
  • MongoDB: Database for game data and user information
  • Prometheus: Metrics collection and monitoring
  • Grafana: Metrics visualization dashboard

Quick Start

1

Prerequisites

Ensure you have installed:
  • Docker (version 20.10 or higher)
  • Docker Compose (version 2.0 or higher)
2

Build and run

From the project root directory:
docker compose up --build
This command will:
  • Build the application Docker image
  • Pull required images (MongoDB, Prometheus, Grafana)
  • Start all services
  • Create necessary networks and volumes
3

Access the application

Once all services are running, access:

Service Configuration

Application Service

The main application is built using a multi-stage Dockerfile for optimized image size. Image: Built from Dockerfile
Port: 8080
Container Name: app
Environment Variables:
PORT=8080
DB_URL=mongodb://admin:admin11@mongo/trivia?authSource=admin
LOG_LEVEL=info
ENV=dev
The application depends on MongoDB and will wait for it to be ready before starting.

MongoDB Service

Image: mongo:7.0.5
Port: 27017
Database: trivia
Default Credentials:
  • Username: admin
  • Password: admin11
These are development credentials. Change them for production deployments by modifying the environment variables in compose.yaml.
Environment Variables:
MONGO_INITDB_DATABASE=trivia
MONGO_INITDB_ROOT_USERNAME=admin
MONGO_INITDB_ROOT_PASSWORD=admin11

Prometheus Service

Image: prom/prometheus:v2.40.4
Port: 9090
Configuration:
Prometheus is configured via /etc/prometheus/prometheus.yml, mounted from:
./deployments/prometheus/prometheus.yml
This service scrapes metrics from the application for monitoring performance and game statistics.

Grafana Service

Image: grafana/grafana:9.3.0
Port: 3000
Default Credentials:
  • Username: admin
  • Password: devops123
Volumes:
  • Configuration: ./deployments/grafana/datasources.yaml/etc/grafana/provisioning/datasources/datasources.yaml
  • Data persistence: Named volume grafana
Grafana is pre-configured with Prometheus as a data source through the provisioned datasources configuration.

Docker Compose File Structure

version: '3.8'
services:
  app:
    build: 
      context: ./.
      dockerfile: Dockerfile
    container_name: app
    depends_on:
      - mongo
    ports:
      - 8080:8080
    environment:
      - PORT=8080
      - DB_URL=mongodb://admin:admin11@mongo/trivia?authSource=admin
      - LOG_LEVEL=info
      - ENV=dev  
  mongo:
    image: 'mongo:7.0.5'
    ports:
      - 27017:27017
    environment:
      - MONGO_INITDB_DATABASE=trivia
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=admin11
  prometheus:
    image: prom/prometheus:v2.40.4
    ports:
      - 9090:9090
    volumes:
      - ./deployments/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
  grafana:
    image: grafana/grafana:9.3.0
    ports:
      - 3000:3000
    environment:
      - GF_SECURITY_ADMIN_USER=admin
      - GF_SECURITY_ADMIN_PASSWORD=devops123
    volumes:
      - ./deployments/grafana/datasources.yaml:/etc/grafana/provisioning/datasources/datasources.yaml
      - grafana:/var/lib/grafana
volumes:
  grafana:

Dockerfile Overview

The application uses a multi-stage build for efficiency: Stage 1: Builder (golang:1.22.3-alpine3.18)
  • Downloads and verifies Go modules
  • Copies source code
  • Builds optimized binary with -ldflags="-s" (strips debug info)
Stage 2: Runtime (alpine:latest)
  • Minimal Alpine Linux base
  • Copies only the compiled binary
  • Exposes port 8080
  • Runs the application
The multi-stage build reduces the final image size significantly by excluding build tools and source code.

Port Mappings

ServiceHost PortContainer PortPurpose
Application80808080Web application
MongoDB2701727017Database access
Prometheus90909090Metrics collection
Grafana30003000Dashboard UI

Common Commands

docker compose up

Data Persistence

The Grafana service uses a named volume (grafana) to persist:
  • Dashboard configurations
  • User preferences
  • Custom queries and annotations
MongoDB does not use a persistent volume in the default configuration. Data will be lost when the container is removed. Add a volume mapping for production use.

Customization

Change Application Port

Modify both the environment variable and port mapping:
app:
  ports:
    - 3000:3000  # Change host port
  environment:
    - PORT=3000  # Change container port

Use External MongoDB

Remove the mongo service and update the application’s DB_URL:
app:
  environment:
    - DB_URL=mongodb://user:pass@external-host:27017/trivia?authSource=admin

Change Log Level

Modify the LOG_LEVEL environment variable:
environment:
  - LOG_LEVEL=debug  # or info, warn, error

Monitoring Setup

After starting the services:
1

Access Grafana

Navigate to http://localhost:3000 and log in with:
  • Username: admin
  • Password: devops123
2

Verify Prometheus connection

Go to Configuration → Data Sources to confirm Prometheus is connected.
3

Create dashboards

Import or create dashboards to visualize application metrics, game statistics, and performance data.

Troubleshooting

Container fails to start

Check logs for the specific service:
docker compose logs app

Port already in use

Find and stop the conflicting process:
lsof -ti:8080
kill -9 <PID>
Or change the port mapping in compose.yaml.

MongoDB connection issues

Ensure the MongoDB container is healthy:
docker compose ps
Verify the connection string matches the MongoDB service configuration.

Production Considerations

The default configuration is for development. For production:
  • Change all default passwords
  • Add MongoDB persistence volume
  • Set ENV=prod for the application
  • Use LOG_LEVEL=info or warn
  • Configure proper network security
  • Set up SSL/TLS certificates
  • Use secrets management for credentials

Next Steps

Local Setup

Set up the development environment without Docker

Configuration

Understand all configuration options and environment variables

Build docs developers (and LLMs) love