docker.recipes

Cortex

advanced

Horizontally scalable, multi-tenant Prometheus service.

Overview

Cortex is a horizontally scalable, multi-tenant Prometheus implementation designed to provide long-term storage and high availability for metrics data. Originally developed by Grafana Labs (now in maintenance mode with development shifted to Grafana Mimir), Cortex addresses Prometheus's limitations around scalability and multi-tenancy by providing a distributed architecture that can handle massive metric ingestion rates while maintaining complete Prometheus API compatibility. This deployment creates a minimal Cortex cluster using two essential services: the Cortex service itself configured for distributed operation, and HashiCorp Consul providing service discovery and coordination between Cortex components. The Cortex container runs multiple internal microservices (distributor, ingester, querier, query-frontend) within a single process, while Consul handles the cluster membership and health checking required for proper distributed operation. This configuration is ideal for organizations running multiple Prometheus instances who need centralized, scalable metrics storage with tenant isolation. Platform teams managing metrics infrastructure across multiple environments will find this setup valuable for consolidating monitoring data while maintaining organizational boundaries through Cortex's multi-tenant capabilities.

Key Features

  • Multi-tenant metrics ingestion with X-Scope-OrgID header-based tenant isolation
  • Prometheus remote_write compatibility for direct integration with existing Prometheus deployments
  • Consul-based service discovery enabling automatic cluster member coordination and health monitoring
  • Horizontal scalability through Cortex's microservices architecture running in single-process mode
  • Long-term metrics storage with configurable retention policies and downsampling
  • Complete Prometheus Query API compatibility for integration with Grafana and other visualization tools
  • Built-in load balancing and high availability through Consul's distributed consensus
  • Configurable ingestion limits and rate limiting per tenant for resource protection

Common Use Cases

  • 1Centralized metrics storage for multiple Kubernetes clusters each running Prometheus
  • 2Multi-tenant SaaS platforms requiring isolated metrics collection per customer organization
  • 3Enterprise monitoring consolidation where different teams need separate metric namespaces
  • 4Long-term metrics retention beyond single Prometheus instance storage limitations
  • 5High-availability Prometheus replacement for mission-critical monitoring infrastructure
  • 6Development and staging environment metrics aggregation with production data separation
  • 7Managed service providers offering Prometheus-as-a-Service to multiple clients

Prerequisites

  • Minimum 1GB RAM available (512MB for Cortex + 256MB for Consul + system overhead)
  • Docker host with ports 8500 and 9009 available for Consul UI and Cortex API access
  • Valid cortex/config.yaml file configured with storage backend and cluster settings
  • Understanding of Prometheus remote_write configuration for metric forwarding
  • Familiarity with multi-tenant concepts and HTTP header-based tenant routing
  • Basic knowledge of YAML configuration for Cortex microservices tuning

For development & testing. Review security settings, change default credentials, and test thoroughly before production use. See Terms

docker-compose.yml

docker-compose.yml
1services:
2 consul:
3 image: consul:latest
4 container_name: consul
5 ports:
6 - "8500:8500"
7 networks:
8 - cortex-network
9
10 cortex:
11 image: cortexproject/cortex:latest
12 container_name: cortex
13 command:
14 - -config.file=/etc/cortex/config.yaml
15 volumes:
16 - ./cortex/config.yaml:/etc/cortex/config.yaml:ro
17 - cortex_data:/data
18 ports:
19 - "9009:9009"
20 depends_on:
21 - consul
22 networks:
23 - cortex-network
24
25volumes:
26 cortex_data:
27
28networks:
29 cortex-network:
30 driver: bridge

.env Template

.env
1# Cortex configuration file required

Usage Notes

  1. 1Docs: https://cortexmetrics.io/docs/
  2. 2Prometheus remote_write endpoint: http://localhost:9009/api/v1/push
  3. 3Create cortex/config.yaml before starting
  4. 4Multi-tenant: set X-Scope-OrgID header for tenant isolation
  5. 5Consul provides service discovery for cluster coordination
  6. 6Note: Project is in maintenance mode, consider Mimir for new deployments

Individual Services(2 services)

Copy individual services to mix and match with your existing compose files.

consul
consul:
  image: consul:latest
  container_name: consul
  ports:
    - "8500:8500"
  networks:
    - cortex-network
cortex
cortex:
  image: cortexproject/cortex:latest
  container_name: cortex
  command:
    - "-config.file=/etc/cortex/config.yaml"
  volumes:
    - ./cortex/config.yaml:/etc/cortex/config.yaml:ro
    - cortex_data:/data
  ports:
    - "9009:9009"
  depends_on:
    - consul
  networks:
    - cortex-network

Quick Start

terminal
1# 1. Create the compose file
2cat > docker-compose.yml << 'EOF'
3services:
4 consul:
5 image: consul:latest
6 container_name: consul
7 ports:
8 - "8500:8500"
9 networks:
10 - cortex-network
11
12 cortex:
13 image: cortexproject/cortex:latest
14 container_name: cortex
15 command:
16 - -config.file=/etc/cortex/config.yaml
17 volumes:
18 - ./cortex/config.yaml:/etc/cortex/config.yaml:ro
19 - cortex_data:/data
20 ports:
21 - "9009:9009"
22 depends_on:
23 - consul
24 networks:
25 - cortex-network
26
27volumes:
28 cortex_data:
29
30networks:
31 cortex-network:
32 driver: bridge
33EOF
34
35# 2. Create the .env file
36cat > .env << 'EOF'
37# Cortex configuration file required
38EOF
39
40# 3. Start the services
41docker compose up -d
42
43# 4. View logs
44docker compose logs -f

One-Liner

Run this command to download and set up the recipe in one step:

terminal
1curl -fsSL https://docker.recipes/api/recipes/cortex/run | bash

Troubleshooting

  • Cortex fails to start with config error: Verify cortex/config.yaml exists and contains valid YAML with required storage_config section
  • Consul shows unhealthy service checks: Check cortex container logs for HTTP endpoint availability and ensure 9009 port binding is successful
  • Remote write from Prometheus fails with 400 error: Confirm X-Scope-OrgID header is set in Prometheus remote_write configuration
  • Cortex reports 'no healthy instances' errors: Verify Consul agent is running and accessible, check network connectivity between cortex and consul containers
  • Query API returns empty results: Check tenant ID consistency between ingestion and query requests, verify metrics are being successfully written
  • High memory usage in cortex container: Review ingestion rate limits and consider adjusting max_concurrent queries in config.yaml

Community Notes

Loading...
Loading notes...

Download Recipe Kit

Get all files in a ready-to-deploy package

Includes docker-compose.yml, .env template, README, and license

Ad Space