借助 Docker Compose,你可以用一条命令启动本地测试网。

要求

  1. 安装 CometBFT
  2. 安装 docker
  3. 安装 docker-compose

构建

构建 cometbft 二进制文件,并可选构建 cometbft/localnode Docker 镜像。 注意,二进制文件会挂载到容器中,因此无需重新构建镜像即可更新它。
# 在 ./build 中构建 linux 二进制文件
make build-linux

# (可选)构建 cometbft/localnode 镜像
make build-docker-localnode

运行测试网

要启动一个 4 节点测试网,请运行:
make localnet-start
这些节点会将它们的 RPC 服务器绑定到主机上的 26657、26660、26662 和 26664 端口。 此命令会使用 localnode 镜像创建一个 4 节点网络。 该网络中的节点会将其 P2P 和 RPC 端点分别暴露给主机,端口为 26656-26657、26659-26660、26661-26662 和 26663-26664。 要更新二进制文件,只需重新构建并重启节点:
make build-linux
make localnet-start

配置

make localnet-start 命令会通过调用 cometbft testnet 命令,在 ./build 中为一个 4 节点测试网创建文件。 ./build 目录会挂载到 /cometbft 挂载点,以便将二进制文件和配置文件附加到容器中。 要更改验证者 / 非验证者的数量,请修改 这里 的 localnet-start Makefile 目标:
localnet-start: localnet-stop
  @if ! [ -f build/node0/config/genesis.json ]; then docker run --rm -v $(CURDIR)/build:/cometbft:Z cometbft/localnode testnet --v 5 --n 3 --o . --populate-persistent-peers --starting-ip-address 192.167.10.2 ; fi
  docker compose up -d
现在该命令会为 5 个验证者和 3 个非验证者生成配置文件。除了生成新的配置文件外,还需要编辑 docker-compose 文件。 还需要再添加 4 个节点,才能充分使用已生成的配置文件。
  node3: # 每增加一个节点,这里加 1
    container_name: node3 # 每增加一个节点,这里加 1
    image: "cometbft/localnode"
    environment:
      - ID=3
      - LOG=${LOG:-cometbft.log}
    ports:
      - "26663-26664:26656-26657" # 每增加一个节点,这里把 26663-26664 加 1
    volumes:
      - ./build:/cometbft:Z
    networks:
      localnet:
        ipv4_address: 192.167.10.5 # 每增加一个节点,最后一位加 1
在运行之前,别忘了清理旧文件:
# 清空 build 目录
rm -rf ./build/node*

配置 ABCI 容器

要在 4 节点配置中使用你自己的 ABCI 应用,请编辑 docker-compose.yaml 文件,并为你的 ABCI 应用添加镜像。
 abci0:
    container_name: abci0
    image: "abci-image"
    build:
      context: .
      dockerfile: abci.Dockerfile
    command: <insert command to run your abci application>
    networks:
      localnet:
        ipv4_address: 192.167.10.6

  abci1:
    container_name: abci1
    image: "abci-image"
    build:
      context: .
      dockerfile: abci.Dockerfile
    command: <insert command to run your abci application>
    networks:
      localnet:
        ipv4_address: 192.167.10.7

  abci2:
    container_name: abci2
    image: "abci-image"
    build:
      context: .
      dockerfile: abci.Dockerfile
    command: <insert command to run your abci application>
    networks:
      localnet:
        ipv4_address: 192.167.10.8

  abci3:
    container_name: abci3
    image: "abci-image"
    build:
      context: .
      dockerfile: abci.Dockerfile
    command: <insert command to run your abci application>
    networks:
      localnet:
        ipv4_address: 192.167.10.9

覆盖每个节点中的 command,以连接到各自的 ABCI。
  node0:
    container_name: node0
    image: "cometbft/localnode"
    ports:
      - "26656-26657:26656-26657"
    environment:
      - ID=0
      - LOG=$${LOG:-cometbft.log}
    volumes:
      - ./build:/cometbft:Z
    command: node --proxy_app=tcp://abci0:26658
    networks:
      localnet:
        ipv4_address: 192.167.10.2
同样配置 node1、node2 和 node3,然后运行测试网。

日志

日志会保存在挂载的卷中,即 cometbft.log 文件内。如果启动时将 LOG 环境变量设为 stdout,日志就不会保存,而是直接输出到屏幕。

特殊二进制文件

如果你有多个名称不同的二进制文件,可以通过 BINARY 环境变量指定要运行的那个。二进制文件的路径相对于挂载的卷。
With Docker Compose, you can spin up local testnets with a single command.

Requirements

  1. Install CometBFT
  2. Install docker
  3. Install docker-compose

Build

Build the cometbft binary and, optionally, the cometbft/localnode docker image. Note the binary will be mounted into the container so it can be updated without rebuilding the image.
# Build the linux binary in ./build
make build-linux

# (optionally) Build cometbft/localnode image
make build-docker-localnode

Run a testnet

To start a 4-node testnet, run:
make localnet-start
The nodes bind their RPC servers to ports 26657, 26660, 26662, and 26664 on the host. This command creates a 4-node network using the localnode image. The nodes of the network expose their P2P and RPC endpoints to the host machine on ports 26656-26657, 26659-26660, 26661-26662, and 26663-26664 respectively. To update the binary, just rebuild it and restart the nodes:
make build-linux
make localnet-start

Configuration

The make localnet-start command creates files for a 4-node testnet in ./build by calling the cometbft testnet command. The ./build directory is mounted to the /cometbft mount point to attach the binary and config files to the container. To change the number of validators / non-validators, change the localnet-start Makefile target here:
localnet-start: localnet-stop
  @if ! [ -f build/node0/config/genesis.json ]; then docker run --rm -v $(CURDIR)/build:/cometbft:Z cometbft/localnode testnet --v 5 --n 3 --o . --populate-persistent-peers --starting-ip-address 192.167.10.2 ; fi
  docker compose up -d
The command will now generate config files for 5 validators and 3 non-validators. Along with generating new config files, the docker-compose file needs to be edited. Adding 4 more nodes is required in order to fully utilize the config files that were generated.
  node3: # bump by 1 for every node
    container_name: node3 # bump by 1 for every node
    image: "cometbft/localnode"
    environment:
      - ID=3
      - LOG=${LOG:-cometbft.log}
    ports:
      - "26663-26664:26656-26657" # Bump 26663-26664 by one for every node
    volumes:
      - ./build:/cometbft:Z
    networks:
      localnet:
        ipv4_address: 192.167.10.5 # bump the final digit by 1 for every node
Before running it, don’t forget to clean up the old files:
# Clear the build folder
rm -rf ./build/node*

Configuring ABCI containers

To use your own ABCI applications with the 4-node setup, edit the docker-compose.yaml file and add images for your ABCI application.
 abci0:
    container_name: abci0
    image: "abci-image"
    build:
      context: .
      dockerfile: abci.Dockerfile
    command: <insert command to run your abci application>
    networks:
      localnet:
        ipv4_address: 192.167.10.6

  abci1:
    container_name: abci1
    image: "abci-image"
    build:
      context: .
      dockerfile: abci.Dockerfile
    command: <insert command to run your abci application>
    networks:
      localnet:
        ipv4_address: 192.167.10.7

  abci2:
    container_name: abci2
    image: "abci-image"
    build:
      context: .
      dockerfile: abci.Dockerfile
    command: <insert command to run your abci application>
    networks:
      localnet:
        ipv4_address: 192.167.10.8

  abci3:
    container_name: abci3
    image: "abci-image"
    build:
      context: .
      dockerfile: abci.Dockerfile
    command: <insert command to run your abci application>
    networks:
      localnet:
        ipv4_address: 192.167.10.9

Override the command in each node to connect to its ABCI.
  node0:
    container_name: node0
    image: "cometbft/localnode"
    ports:
      - "26656-26657:26656-26657"
    environment:
      - ID=0
      - LOG=$${LOG:-cometbft.log}
    volumes:
      - ./build:/cometbft:Z
    command: node --proxy_app=tcp://abci0:26658
    networks:
      localnet:
        ipv4_address: 192.167.10.2
Similarly configure node1, node2, and node3, then run the testnet.

Logging

The log is saved under the attached volume, in the cometbft.log file. If the LOG environment variable is set to stdout at start, the log is not saved, but printed to the screen.

Special binaries

If you have multiple binaries with different names, you can specify which one to run with the BINARY environment variable. The path of the binary is relative to the attached volume.