Part 2: Installation & Cluster Setup
Spinning up a local CockroachDB cluster with Docker in under 5 minutes.
Theory is great, but let’s get our hands dirty. We’re going to spin up a 3-node CockroachDB cluster on your local machine using Docker.
Why 3 Nodes?
You can run a single node, but that’s boring. CockroachDB shines when it’s distributed. With 3 nodes, we can simulate failures and see how the cluster self-heals.
Step 1: Create a Network
First, let’s create a Docker network so our nodes can talk to each other.
docker network create -d bridge roachnet
Step 2: Start the Nodes
Run these commands to start three nodes:
# Node 1
docker run -d --name=roach1 --hostname=roach1 --net=roachnet -p 26257:26257 -p 8080:8080 cockroachdb/cockroach:latest start-single-node --insecure
# Wait... actually, let's do a proper 3-node cluster.
# Stop that single node first if you ran it.
# Node 1
docker run -d --name=roach1 --hostname=roach1 --net=roachnet -p 26257:26257 -p 8080:8080 cockroachdb/cockroach:latest start --insecure --join=roach1,roach2,roach3
# Node 2
docker run -d --name=roach2 --hostname=roach2 --net=roachnet cockroachdb/cockroach:latest start --insecure --join=roach1,roach2,roach3
# Node 3
docker run -d --name=roach3 --hostname=roach3 --net=roachnet cockroachdb/cockroach:latest start --insecure --join=roach1,roach2,roach3
Step 3: Initialize the Cluster
The nodes are running, but they aren’t a cluster yet. We need to tell them to join forces.
docker exec -it roach1 ./cockroach init --insecure
You should see: Cluster successfully initialized.
Step 4: Access the Dashboard
Open your browser and go to http://localhost:8080. You’ll see the beautiful DB Console showing 3 live nodes.
Conclusion
You now have a distributed database running on your laptop. In the next part, we’ll connect to it using SQL and see how it compares to Postgres.