1. Pull the mirror
docker pull cockroachdb/cockroach:v1.0.2 docker run --rm cockroachdb/cockroach:v1.0.2 version // Confirm that the image is working properly
2. New bridge
When running multiple containers in a single-machine environment, you need to use docker's bridge mode to enable mutual access between containers, while ensuring that containers are isolated from each other in the external network environment
docker network create -d bridge roachnet
3. Start the first node
docker run -d \ --name=roach1 \ --hostname=roach1 \ --net=roachnet \ -p 26257:26257 -p 8080:8080 \ -v "${PWD}/cockroach-data/roach1:/cockroach/cockroach-data" \ cockroachdb/cockroach:v1.0.2 start --insecure
-- hostname: Containers join the cluster through hostname
26257: The default port for cockroach
8080: port for monitoring UI
-p: map the port of the container host and container
-v: mount the host directory
start --insecure start the node inside the container
4. Add another node to the cluster
docker run -d \ --name=roach2 \ --hostname=roach2 \ --net=roachnet \ -v "${PWD}/cockroach-data/roach2:/cockroach/cockroach-data" \ cockroachdb/cockroach:v1.0.2 start --insecure --join=roach1 docker run -d \ --name=roach3 \ --hostname=roach3 \ --net=roachnet \ -v "${PWD}/cockroach-data/roach3:/cockroach/cockroach-data" \ cockroachdb/cockroach:v1.0.2 start --insecure --join=roach1
--join: The container node joins the cluster through the hostname specified earlier
5. Cluster test
docker exec -it roach1 ./cockroach sql --insecure
output:
# Welcome to the cockroach SQL interface.
# All statements must be terminated by a semicolon.
# To exit: CTRL + D.
Execute sql:
CREATE DATABASE bank; CREATE TABLE bank.accounts (id INT PRIMARY KEY, balance DECIMAL); INSERT INTO bank.accounts VALUES (1, 1000.50); SELECT * FROM bank.accounts;
Node 1 result
+----+---------+ | id | balance | +----+---------+ | 1 | 1000.50 | +----+---------+ (1 row) \q quit
Node 2, Node 3:
SELECT * FROM bank.accounts;
search result:
+----+---------+ | id | balance | +----+---------+ | 1 | 1000.50 | +----+---------+ (1 row) \q exit node
6. Cluster monitoring
The 8080 port of the host has been managed when the cluster is started, you can view the container
docker ps
55e09b83cdba cockroachdb/cockroach:v1.0.2 "/cockroach/cockroach" 40 minutes ago Up 40 minutes 0.0.0.0:8080->8080/tcp, 0.0.0.0:26257->26257/tcp roach1
在浏览器中打开地址,http://localhost:8080,本例是在云服务器中执行,所以通过外网查看
至此已成功docker中运行cockroach集群