CockroachDB Cluster
Distributed SQL database with automatic replication and strong consistency.
Overview
CockroachDB is a distributed SQL database designed to survive datacenter failures while maintaining ACID transactions and strong consistency guarantees. Built by former Google engineers who worked on Spanner, CockroachDB automatically replicates data across multiple nodes, rebalances data as the cluster grows or shrinks, and provides PostgreSQL wire protocol compatibility for easy application migration. The database gets its name from the cockroach's legendary survivability - the system is designed to keep running even when individual nodes fail catastrophically.
This three-node CockroachDB cluster configuration creates a production-ready distributed database with automatic failover capabilities and horizontal scaling. Each node runs independently while participating in the Raft consensus protocol to maintain data consistency across the cluster. The cluster can survive the loss of one node without service interruption, automatically promoting replicas and continuing to serve read and write requests. The built-in load balancer distributes queries across healthy nodes, while the gossip protocol ensures cluster membership and routing information stays current.
This setup is ideal for teams transitioning from traditional PostgreSQL deployments who need global scalability, automatic failover, and geo-distributed capabilities without changing their application code. Financial services, SaaS platforms, and e-commerce applications benefit from CockroachDB's ability to maintain strict consistency while scaling across multiple regions. The PostgreSQL compatibility means existing ORMs, drivers, and database tools work without modification, making it an attractive option for organizations needing distributed database capabilities without a complete application rewrite.
Key Features
- Automatic data replication with configurable replication factor across cluster nodes
- PostgreSQL wire protocol compatibility supporting existing drivers and ORMs
- Raft consensus algorithm ensuring strong consistency and ACID transaction guarantees
- Built-in Admin UI at port 8080 for cluster monitoring, query analysis, and performance metrics
- Automatic range splitting and rebalancing as data grows across the cluster
- Geo-partitioning capabilities for data locality and compliance requirements
- Time Travel queries using AS OF SYSTEM TIME for historical data access
- Distributed SQL execution engine with cost-based query optimization
Common Use Cases
- 1Multi-region SaaS applications requiring low-latency reads with global consistency
- 2Financial trading platforms needing ACID transactions across geographically distributed data
- 3E-commerce systems handling high-volume transactions with automatic scaling requirements
- 4IoT data ingestion pipelines requiring horizontal write scalability and real-time analytics
- 5Migration path from single-node PostgreSQL to distributed architecture without code changes
- 6Disaster recovery scenarios where automatic failover and data replication are critical
- 7Development and testing environments replicating production distributed database behavior
Prerequisites
- Minimum 8GB RAM available as CockroachDB requires 2GB per node plus overhead
- Docker Engine 20.10+ with support for multi-container networking and volume management
- Ports 26257 (SQL) and 8080 (Admin UI) available on the host system
- Basic understanding of distributed systems concepts and SQL database administration
- Familiarity with PostgreSQL query syntax and connection management
- At least 10GB available disk space per node for data storage and transaction logs
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 roach1: 3 image: cockroachdb/cockroach:latest4 container_name: roach15 command: start --insecure --join=roach1,roach2,roach36 volumes: 7 - roach1_data:/cockroach/cockroach-data8 ports: 9 - "26257:26257"10 - "8080:8080"11 networks: 12 - roach-network1314 roach2: 15 image: cockroachdb/cockroach:latest16 container_name: roach217 command: start --insecure --join=roach1,roach2,roach318 volumes: 19 - roach2_data:/cockroach/cockroach-data20 networks: 21 - roach-network2223 roach3: 24 image: cockroachdb/cockroach:latest25 container_name: roach326 command: start --insecure --join=roach1,roach2,roach327 volumes: 28 - roach3_data:/cockroach/cockroach-data29 networks: 30 - roach-network3132 init: 33 image: cockroachdb/cockroach:latest34 command: init --insecure --host=roach135 depends_on: 36 - roach137 - roach238 - roach339 networks: 40 - roach-network4142volumes: 43 roach1_data: 44 roach2_data: 45 roach3_data: 4647networks: 48 roach-network: 49 driver: bridge.env Template
.env
1# CockroachDB runs in insecure mode for development2# For production, configure certificatesUsage Notes
- 1Docs: https://www.cockroachlabs.com/docs/
- 2Admin UI at http://localhost:8080 | SQL on port 26257
- 3Connect: docker exec -it roach1 cockroach sql --insecure
- 4PostgreSQL-compatible - use any PG client or ORM
- 5Automatic data replication - survives node failures
- 6For production: configure TLS certificates and remove --insecure flag
Individual Services(4 services)
Copy individual services to mix and match with your existing compose files.
roach1
roach1:
image: cockroachdb/cockroach:latest
container_name: roach1
command: start --insecure --join=roach1,roach2,roach3
volumes:
- roach1_data:/cockroach/cockroach-data
ports:
- "26257:26257"
- "8080:8080"
networks:
- roach-network
roach2
roach2:
image: cockroachdb/cockroach:latest
container_name: roach2
command: start --insecure --join=roach1,roach2,roach3
volumes:
- roach2_data:/cockroach/cockroach-data
networks:
- roach-network
roach3
roach3:
image: cockroachdb/cockroach:latest
container_name: roach3
command: start --insecure --join=roach1,roach2,roach3
volumes:
- roach3_data:/cockroach/cockroach-data
networks:
- roach-network
init
init:
image: cockroachdb/cockroach:latest
command: init --insecure --host=roach1
depends_on:
- roach1
- roach2
- roach3
networks:
- roach-network
Quick Start
terminal
1# 1. Create the compose file2cat > docker-compose.yml << 'EOF'3services:4 roach1:5 image: cockroachdb/cockroach:latest6 container_name: roach17 command: start --insecure --join=roach1,roach2,roach38 volumes:9 - roach1_data:/cockroach/cockroach-data10 ports:11 - "26257:26257"12 - "8080:8080"13 networks:14 - roach-network1516 roach2:17 image: cockroachdb/cockroach:latest18 container_name: roach219 command: start --insecure --join=roach1,roach2,roach320 volumes:21 - roach2_data:/cockroach/cockroach-data22 networks:23 - roach-network2425 roach3:26 image: cockroachdb/cockroach:latest27 container_name: roach328 command: start --insecure --join=roach1,roach2,roach329 volumes:30 - roach3_data:/cockroach/cockroach-data31 networks:32 - roach-network3334 init:35 image: cockroachdb/cockroach:latest36 command: init --insecure --host=roach137 depends_on:38 - roach139 - roach240 - roach341 networks:42 - roach-network4344volumes:45 roach1_data:46 roach2_data:47 roach3_data:4849networks:50 roach-network:51 driver: bridge52EOF5354# 2. Create the .env file55cat > .env << 'EOF'56# CockroachDB runs in insecure mode for development57# For production, configure certificates58EOF5960# 3. Start the services61docker compose up -d6263# 4. View logs64docker compose logs -fOne-Liner
Run this command to download and set up the recipe in one step:
terminal
1curl -fsSL https://docker.recipes/api/recipes/cockroachdb/run | bashTroubleshooting
- Cluster initialization fails: Ensure all three nodes are running before the init container executes, add health checks if needed
- Node cannot join cluster: Verify the --join parameter includes all node hostnames and the roach-network allows inter-container communication
- Admin UI shows unavailable replicas: Check that all nodes have sufficient disk space and restart any failed containers
- Connection refused on port 26257: Wait for cluster initialization to complete, verify the init container ran successfully
- Performance issues with queries: Use EXPLAIN ANALYZE to identify distributed query bottlenecks and consider adding indexes
- Memory errors in container logs: Increase Docker memory limits or reduce concurrent connection limits in client applications
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
Shortcuts: C CopyF FavoriteD Download