Start a Cluster in Docker (Insecure)

Once you've installed the official CockroachDB Docker image, it's simple to run an insecure multi-node cluster across multiple Docker containers on a single host, using Docker volumes to persist node data.

Warning:

Running a stateful application like CockroachDB in Docker is more complex and error-prone than most uses of Docker and is not recommended for production deployments. To run a physically distributed cluster in containers, use an orchestration tool like Kubernetes or Docker Swarm. See Orchestration for more details.

Before you begin

If you have not already installed the official CockroachDB Docker image, go to Install CockroachDB and follow the instructions under Use Docker.

Step 1. Create a bridge network

Since you'll be running multiple Docker containers on a single host, with one CockroachDB node per container, you need to create what Docker refers to as a bridge network. The bridge network will enable the containers to communicate as a single cluster while keeping them isolated from external networks.

  1. $ docker network create -d bridge roachnet

We've used roachnet as the network name here and in subsequent steps, but feel free to give your network any name you like.

Step 2. Start the first node

  1. $ docker run -d \
  2. --name=roach1 \
  3. --hostname=roach1 \
  4. --net=roachnet \
  5. -p 26257:26257 -p 8080:8080 \
  6. -v "${PWD}/cockroach-data/roach1:/cockroach/cockroach-data" \
  7. cockroachdb/cockroach:v19.1.0 start --insecure

This command creates a container and starts the first CockroachDB node inside it. Let's look at each part:

  • docker run: The Docker command to start a new container.
  • -d: This flag runs the container in the background so you can continue the next steps in the same shell.
  • —name: The name for the container. This is optional, but a custom name makes it significantly easier to reference the container in other commands, for example, when opening a Bash session in the container or stopping the container.
  • —hostname: The hostname for the container. You will use this to join other containers/nodes to the cluster.
  • —net: The bridge network for the container to join. See step 1 for more details.
  • -p 26257:26257 -p 8080:8080: These flags map the default port for inter-node and client-node communication (26257) and the default port for HTTP requests to the Admin UI (8080) from the container to the host. This enables inter-container communication and makes it possible to call up the Admin UI from a browser.
  • -v "${PWD}/cockroach-data/roach1:/cockroach/cockroach-data": This flag mounts a host directory as a data volume. This means that data and logs for this node will be stored in ${PWD}/cockroach-data/roach1 on the host and will persist after the container is stopped or deleted. For more details, see Docker's Bind Mounts topic.
  • cockroachdb/cockroach:v19.1.0 start —insecure: The CockroachDB command to start a node in the container in insecure mode.

Step 3. Add nodes to the cluster

At this point, your cluster is live and operational. With just one node, you can already connect a SQL client and start building out your database. In real deployments, however, you'll always want 3 or more nodes to take advantage of CockroachDB's automatic replication, rebalancing, and fault tolerance capabilities.

To simulate a real deployment, scale your cluster by adding two more nodes:

  1. $ docker run -d \
  2. --name=roach2 \
  3. --hostname=roach2 \
  4. --net=roachnet \
  5. -v "${PWD}/cockroach-data/roach2:/cockroach/cockroach-data" \
  6. cockroachdb/cockroach:v19.1.0 start --insecure --join=roach1
  1. $ docker run -d \
  2. --name=roach3 \
  3. --hostname=roach3 \
  4. --net=roachnet \
  5. -v "${PWD}/cockroach-data/roach3:/cockroach/cockroach-data" \
  6. cockroachdb/cockroach:v19.1.0 start --insecure --join=roach1

These commands add two more containers and start CockroachDB nodes inside them, joining them to the first node. There are only a few differences to note from step 2:

  • -v: This flag mounts a host directory as a data volume. Data and logs for these nodes will be stored in ${PWD}/cockroach-data/roach2 and ${PWD}/cockroach-data/roach3 on the host and will persist after the containers are stopped or deleted.
  • —join: This flag joins the new nodes to the cluster, using the first container's hostname. Otherwise, all cockroach start defaults are accepted. Note that since each node is in a unique container, using identical default ports won’t cause conflicts.

Step 4. Test the cluster

Now that you've scaled to 3 nodes, you can use any node as a SQL gateway to the cluster. To demonstrate this, use the docker exec command to start the built-in SQL shell in the first container:

  1. $ docker exec -it roach1 ./cockroach sql --insecure

Run some basic CockroachDB SQL statements:

  1. > CREATE DATABASE bank;
  1. > CREATE TABLE bank.accounts (id INT PRIMARY KEY, balance DECIMAL);
  1. > INSERT INTO bank.accounts VALUES (1, 1000.50);
  1. > SELECT * FROM bank.accounts;
  1. +----+---------+
  2. | id | balance |
  3. +----+---------+
  4. | 1 | 1000.5 |
  5. +----+---------+
  6. (1 row)

Exit the SQL shell on node 1:

  1. > \q

Then start the SQL shell in the second container:

  1. $ docker exec -it roach2 ./cockroach sql --insecure

Now run the same SELECT query:

  1. > SELECT * FROM bank.accounts;
  1. +----+---------+
  2. | id | balance |
  3. +----+---------+
  4. | 1 | 1000.5 |
  5. +----+---------+
  6. (1 row)

As you can see, node 1 and node 2 behaved identically as SQL gateways.

When you're done, exit the SQL shell on node 2:

  1. > \q

Step 5. Monitor the cluster

When you started the first container/node, you mapped the node's default HTTP port 8080 to port 8080 on the host. To check out the Admin UI metrics for your cluster, point your browser to that port on localhost, i.e., http://localhost:8080, and click Metrics on the left-hand navigation bar.

As mentioned earlier, CockroachDB automatically replicates your data behind-the-scenes. To verify that data written in the previous step was replicated successfully, scroll down to the Replicas per Node graph and hover over the line:

CockroachDB Admin UI

The replica count on each node is identical, indicating that all data in the cluster was replicated 3 times (the default).

Tip:
For more insight into how CockroachDB automatically replicates and rebalances data, and tolerates and recovers from failures, see our replication, rebalancing, fault tolerance demos.

Step 6. Stop the cluster

Use the docker stop and docker rm commands to stop and remove the containers (and therefore the cluster):

  1. $ docker stop roach1 roach2 roach3
  1. $ docker rm roach1 roach2 roach3

If you do not plan to restart the cluster, you may want to remove the nodes' data stores:

  1. $ rm -rf cockroach-data

Before you begin

If you have not already installed the official CockroachDB Docker image, go to Install CockroachDB and follow the instructions under Use Docker.

Step 1. Create a bridge network

Since you'll be running multiple Docker containers on a single host, with one CockroachDB node per container, you need to create what Docker refers to as a bridge network. The bridge network will enable the containers to communicate as a single cluster while keeping them isolated from external networks.

  1. $ docker network create -d bridge roachnet

We've used roachnet as the network name here and in subsequent steps, but feel free to give your network any name you like.

Step 2. Start the first node

  1. $ docker run -d \
  2. --name=roach1 \
  3. --hostname=roach1 \
  4. --net=roachnet \
  5. -p 26257:26257 -p 8080:8080 \
  6. -v "${PWD}/cockroach-data/roach1:/cockroach/cockroach-data" \
  7. cockroachdb/cockroach:v19.1.0 start --insecure

This command creates a container and starts the first CockroachDB node inside it. Let's look at each part:

  • docker run: The Docker command to start a new container.
  • -d: This flag runs the container in the background so you can continue the next steps in the same shell.
  • —name: The name for the container. This is optional, but a custom name makes it significantly easier to reference the container in other commands, for example, when opening a Bash session in the container or stopping the container.
  • —hostname: The hostname for the container. You will use this to join other containers/nodes to the cluster.
  • —net: The bridge network for the container to join. See step 1 for more details.
  • -p 26257:26257 -p 8080:8080: These flags map the default port for inter-node and client-node communication (26257) and the default port for HTTP requests to the Admin UI (8080) from the container to the host. This enables inter-container communication and makes it possible to call up the Admin UI from a browser.
  • -v "${PWD}/cockroach-data/roach1:/cockroach/cockroach-data": This flag mounts a host directory as a data volume. This means that data and logs for this node will be stored in ${PWD}/cockroach-data/roach1 on the host and will persist after the container is stopped or deleted. For more details, see Docker's Bind Mounts topic.
  • cockroachdb/cockroach:v19.1.0 start —insecure: The CockroachDB command to start a node in the container in insecure mode.

Step 3. Add nodes to the cluster

At this point, your cluster is live and operational. With just one node, you can already connect a SQL client and start building out your database. In real deployments, however, you'll always want 3 or more nodes to take advantage of CockroachDB's automatic replication, rebalancing, and fault tolerance capabilities.

To simulate a real deployment, scale your cluster by adding two more nodes:

  1. $ docker run -d \
  2. --name=roach2 \
  3. --hostname=roach2 \
  4. --net=roachnet \
  5. -v "${PWD}/cockroach-data/roach2:/cockroach/cockroach-data" \
  6. cockroachdb/cockroach:v19.1.0 start --insecure --join=roach1
  1. $ docker run -d \
  2. --name=roach3 \
  3. --hostname=roach3 \
  4. --net=roachnet \
  5. -v "${PWD}/cockroach-data/roach3:/cockroach/cockroach-data" \
  6. cockroachdb/cockroach:v19.1.0 start --insecure --join=roach1

These commands add two more containers and start CockroachDB nodes inside them, joining them to the first node. There are only a few differences to note from step 2:

  • -v: This flag mounts a host directory as a data volume. Data and logs for these nodes will be stored in ${PWD}/cockroach-data/roach2 and ${PWD}/cockroach-data/roach3 on the host and will persist after the containers are stopped or deleted.
  • —join: This flag joins the new nodes to the cluster, using the first container's hostname. Otherwise, all cockroach start defaults are accepted. Note that since each node is in a unique container, using identical default ports won’t cause conflicts.

Step 4. Test the cluster

Now that you've scaled to 3 nodes, you can use any node as a SQL gateway to the cluster. To demonstrate this, use the docker exec command to start the built-in SQL shell in the first container:

  1. $ docker exec -it roach1 ./cockroach sql --insecure

Run some basic CockroachDB SQL statements:

  1. > CREATE DATABASE bank;
  1. > CREATE TABLE bank.accounts (id INT PRIMARY KEY, balance DECIMAL);
  1. > INSERT INTO bank.accounts VALUES (1, 1000.50);
  1. > SELECT * FROM bank.accounts;
  1. +----+---------+
  2. | id | balance |
  3. +----+---------+
  4. | 1 | 1000.5 |
  5. +----+---------+
  6. (1 row)

Exit the SQL shell on node 1:

  1. > \q

Then start the SQL shell in the second container:

  1. $ docker exec -it roach2 ./cockroach sql --insecure

Now run the same SELECT query:

  1. > SELECT * FROM bank.accounts;
  1. +----+---------+
  2. | id | balance |
  3. +----+---------+
  4. | 1 | 1000.5 |
  5. +----+---------+
  6. (1 row)

As you can see, node 1 and node 2 behaved identically as SQL gateways.

When you're done, exit the SQL shell on node 2:

  1. > \q

Step 5. Monitor the cluster

When you started the first container/node, you mapped the node's default HTTP port 8080 to port 8080 on the host. To check out the Admin UI metrics for your cluster, point your browser to that port on localhost, i.e., http://localhost:8080 and click Metrics on the left.

As mentioned earlier, CockroachDB automatically replicates your data behind-the-scenes. To verify that data written in the previous step was replicated successfully, scroll down to the Replicas per Node graph and hover over the line:

CockroachDB Admin UI

The replica count on each node is identical, indicating that all data in the cluster was replicated 3 times (the default).

Tip:

For more insight into how CockroachDB automatically replicates and rebalances data, and tolerates and recovers from failures, see our replication, rebalancing, fault tolerance demos.

Step 6. Stop the cluster

Use the docker stop and docker rm commands to stop and remove the containers (and therefore the cluster):

  1. $ docker stop roach1 roach2 roach3
  1. $ docker rm roach1 roach2 roach3

If you do not plan to restart the cluster, you may want to remove the nodes' data stores:

  1. $ rm -rf cockroach-data

Before you begin

If you have not already installed the official CockroachDB Docker image, go to Install CockroachDB and follow the instructions under Use Docker.

Step 1. Create a bridge network

Since you'll be running multiple Docker containers on a single host, with one CockroachDB node per container, you need to create what Docker refers to as a bridge network. The bridge network will enable the containers to communicate as a single cluster while keeping them isolated from external networks.

  1. PS C:\Users\username> docker network create -d bridge roachnet

We've used roachnet as the network name here and in subsequent steps, but feel free to give your network any name you like.

Step 2. Start the first node

Note:
Be sure to replace <username> in the -v flag with your actual username.

  1. PS C:\Users\username> docker run -d `
  2. --name=roach1 `
  3. --hostname=roach1 `
  4. --net=roachnet `
  5. -p 26257:26257 -p 8080:8080 `
  6. -v "//c/Users/<username>/cockroach-data/roach1:/cockroach/cockroach-data" `
  7. cockroachdb/cockroach:v19.1.0 start --insecure

This command creates a container and starts the first CockroachDB node inside it. Let's look at each part:

  • docker run: The Docker command to start a new container.
  • -d: This flag runs the container in the background so you can continue the next steps in the same shell.
  • —name: The name for the container. This is optional, but a custom name makes it significantly easier to reference the container in other commands, for example, when opening a Bash session in the container or stopping the container.
  • —hostname: The hostname for the container. You will use this to join other containers/nodes to the cluster.
  • —net: The bridge network for the container to join. See step 1 for more details.
  • -p 26257:26257 -p 8080:8080: These flags map the default port for inter-node and client-node communication (26257) and the default port for HTTP requests to the Admin UI (8080) from the container to the host. This enables inter-container communication and makes it possible to call up the Admin UI from a browser.
  • -v "//c/Users/<username>/cockroach-data/roach1:/cockroach/cockroach-data": This flag mounts a host directory as a data volume. This means that data and logs for this node will be stored in Users/<username>/cockroach-data/roach1 on the host and will persist after the container is stopped or deleted. For more details, see Docker's Bind Mounts topic.
  • cockroachdb/cockroach:v19.1.0 start —insecure: The CockroachDB command to start a node in the container in insecure mode.

Step 3. Add nodes to the cluster

At this point, your cluster is live and operational. With just one node, you can already connect a SQL client and start building out your database. In real deployments, however, you'll always want 3 or more nodes to take advantage of CockroachDB's automatic replication, rebalancing, and fault tolerance capabilities.

To simulate a real deployment, scale your cluster by adding two more nodes:

Note:
Again, be sure to replace <username> in the -v flag with your actual username.

  1. # Start the second container/node:
  2. PS C:\Users\username> docker run -d `
  3. --name=roach2 `
  4. --hostname=roach2 `
  5. --net=roachnet `
  6. -v "//c/Users/<username>/cockroach-data/roach2:/cockroach/cockroach-data" `
  7. cockroachdb/cockroach:v19.1.0 start --insecure --join=roach1
  8. # Start the third container/node:
  9. PS C:\Users\username> docker run -d `
  10. --name=roach3 `
  11. --hostname=roach3 `
  12. --net=roachnet `
  13. -v "//c/Users/<username>/cockroach-data/roach3:/cockroach/cockroach-data" `
  14. cockroachdb/cockroach:v19.1.0 start --insecure --join=roach1

These commands add two more containers and start CockroachDB nodes inside them, joining them to the first node. There are only a few differences to note from step 2:

  • -v: This flag mounts a host directory as a data volume. Data and logs for these nodes will be stored in Users/<username>/cockroach-data/roach2 and Users/<username>/cockroach-data/roach3 on the host and will persist after the containers are stopped or deleted.
  • —join: This flag joins the new nodes to the cluster, using the first container's hostname. Note that since each node is in a unique container, using identical default ports will not cause conflicts.

Step 4. Test the cluster

Now that you've scaled to 3 nodes, you can use any node as a SQL gateway to the cluster. To demonstrate this, use the docker exec command to start the built-in SQL shell in the first container:

  1. PS C:\Users\username> docker exec -it roach1 ./cockroach sql --insecure
  2. # Welcome to the cockroach SQL interface.
  3. # All statements must be terminated by a semicolon.
  4. # To exit: CTRL + D.

Run some basic CockroachDB SQL statements:

  1. > CREATE DATABASE bank;
  1. > CREATE TABLE bank.accounts (id INT PRIMARY KEY, balance DECIMAL);
  1. > INSERT INTO bank.accounts VALUES (1, 1000.50);
  1. > SELECT * FROM bank.accounts;
  1. +----+---------+
  2. | id | balance |
  3. +----+---------+
  4. | 1 | 1000.5 |
  5. +----+---------+
  6. (1 row)

Exit the SQL shell on node 1:

  1. > \q

Then start the SQL shell in the second container:

  1. PS C:\Users\username> docker exec -it roach2 ./cockroach sql --insecure
  2. # Welcome to the cockroach SQL interface.
  3. # All statements must be terminated by a semicolon.
  4. # To exit: CTRL + D.

Now run the same SELECT query:

  1. > SELECT * FROM bank.accounts;
  1. +----+---------+
  2. | id | balance |
  3. +----+---------+
  4. | 1 | 1000.5 |
  5. +----+---------+
  6. (1 row)

As you can see, node 1 and node 2 behaved identically as SQL gateways.

When you're done, exit the SQL shell on node 2:

  1. > \q

Step 5. Monitor the cluster

When you started the first container/node, you mapped the node's default HTTP port 8080 to port 8080 on the host. To check out the Admin UI metrics for your cluster, point your browser to that port on localhost, i.e., http://localhost:8080 and click Metrics on the left.

As mentioned earlier, CockroachDB automatically replicates your data behind-the-scenes. To verify that data written in the previous step was replicated successfully, scroll down to the Replicas per Node graph and hover over the line:

CockroachDB Admin UI

The replica count on each node is identical, indicating that all data in the cluster was replicated 3 times (the default).

Tip:

For more insight into how CockroachDB automatically replicates and rebalances data, and tolerates and recovers from failures, see our replication, rebalancing, fault tolerance demos.

Step 6. Stop the cluster

Use the docker stop and docker rm commands to stop and remove the containers (and therefore the cluster):

  1. # Stop the containers:
  2. PS C:\Users\username> docker stop roach1 roach2 roach3
  3. # Remove the containers:
  4. PS C:\Users\username> docker rm roach1 roach2 roach3

If you do not plan to restart the cluster, you may want to remove the nodes' data stores:

  1. PS C:\Users\username> rm -rf cockroach-data

What's next?

Was this page helpful?
YesNo