Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
.git/
target/
**/target/
.git/
*.log
data
dist
bin
docs
tools
*.md
!README.md
32 changes: 32 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copy to .env; unset values use docker-compose.yml defaults. Never set COMPOSE_* or DOCKER_* here.

# 30 mainnet, 31 testnet, anything else regtest.
RUSTOCK_NETWORK_ID=30
# Peers, tcp (rskj: 5050 mainnet, 50505 testnet).
RUSTOCK_P2P_PORT=5050
# Discovery, udp; must be the peer port + 1.
RUSTOCK_DISCOVERY_PORT=5051
# Public IPv4 address advertised to peers. Unset, networks 30 and 31 look it up;
# set it behind NAT or to skip the lookup.
RUSTOCK_EXTERNAL_IP=

# A named volume, or a host path (/ or ./) owned by uid 10001.
RUSTOCK_DATA_DIR=rustock-data

# trace, debug, info, warn, error, or per-module: info,rustock_sync=debug
RUSTOCK_LOG_LEVEL=info
# false writes rustock.log.<date> into the data directory instead.
RUSTOCK_LOG_TO_STDOUT=true
RUSTOCK_LOG_MAX_SIZE=50m
RUSTOCK_LOG_MAX_FILE=10

# The RPC has no authentication; do not bind it beyond a trusted network.
RUSTOCK_RPC_BIND=127.0.0.1
RUSTOCK_RPC_PORT=4444
RUSTOCK_METRICS_BIND=127.0.0.1
RUSTOCK_METRICS_PORT=9898

# Node binary to mount.
RUSTOCK_BINARY=./bin/rustock-cli
# Extra rustock-cli flags, e.g. "--snap-sync --rpc-admin".
RUSTOCK_EXTRA_ARGS=
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ tmp/
*.env
pegout-alerts.toml
!pegout-alerts.example.toml

# Built and installed node binaries (scripts/build-binary.sh)
/dist/
/bin/
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ docker run --rm rustock rustock-cli --help

The image has no entrypoint; the binary is `/usr/local/bin/rustock-cli`.

To run a node under Docker Compose from a prebuilt binary, see
[docs/docker.md](docs/docker.md).

### Running

Start the full node on RSK Mainnet (default):
Expand Down
82 changes: 82 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Settings come from .env; each has a default here.
services:
rustock:
image: rustock-runtime:local
build:
context: ./docker
dockerfile: runtime.Dockerfile
# Restart after a crash, not after a clean exit such as rsk_shutdown.
restart: on-failure

# The running node ignores SIGTERM; stop it with rsk_shutdown (docs/docker.md).
stop_grace_period: 30s

environment:
RUSTOCK_NETWORK_ID: ${RUSTOCK_NETWORK_ID:-30}
RUSTOCK_P2P_PORT: ${RUSTOCK_P2P_PORT:-5050}
RUSTOCK_DISCOVERY_PORT: ${RUSTOCK_DISCOVERY_PORT:-5051}
RUSTOCK_EXTERNAL_IP: ${RUSTOCK_EXTERNAL_IP:-}
RUSTOCK_LOG_LEVEL: ${RUSTOCK_LOG_LEVEL:-info}
RUSTOCK_LOG_TO_STDOUT: ${RUSTOCK_LOG_TO_STDOUT:-true}
RUSTOCK_EXTRA_ARGS: ${RUSTOCK_EXTRA_ARGS:-}

# json-file never rotates without limits.
logging:
driver: json-file
options:
max-size: ${RUSTOCK_LOG_MAX_SIZE:-50m}
max-file: "${RUSTOCK_LOG_MAX_FILE:-10}"

# P2P and discovery ports are identical on host and container: the node
# advertises its own port to peers.
ports:
- "${RUSTOCK_P2P_PORT:-5050}:${RUSTOCK_P2P_PORT:-5050}/tcp"
- "${RUSTOCK_DISCOVERY_PORT:-5051}:${RUSTOCK_DISCOVERY_PORT:-5051}/udp"
- "${RUSTOCK_RPC_BIND:-127.0.0.1}:${RUSTOCK_RPC_PORT:-4444}:4444"
- "${RUSTOCK_METRICS_BIND:-127.0.0.1}:${RUSTOCK_METRICS_PORT:-9898}:9898"

volumes:
# A path starting with / or ./ is a host directory; a bare name is the
# named volume declared below.
- ${RUSTOCK_DATA_DIR:-rustock-data}:/var/lib/rustock
- ./docker/node.toml:/etc/rustock/node.toml:ro
# create_host_path: false fails instead of creating a directory when the
# binary is missing.
- type: bind
source: ${RUSTOCK_BINARY:-./bin/rustock-cli}
target: /usr/local/bin/rustock-cli
read_only: true
bind:
create_host_path: false

ulimits:
nofile:
soft: 65536
hard: 65536

healthcheck:
test: ["CMD", "bash", "-c", "exec 3<>/dev/tcp/127.0.0.1/9898"]
interval: 30s
timeout: 5s
retries: 3
start_period: 120s

# Offline maintenance: docker compose run --rm tools <tool> /var/lib/rustock ...
tools:
image: rustock-runtime:local
profiles: [tools]
restart: "no"
entrypoint: []
environment:
PATH: /opt/rustock-tools:/usr/local/bin:/usr/bin:/bin
volumes:
- ${RUSTOCK_DATA_DIR:-rustock-data}:/var/lib/rustock
- type: bind
source: ./bin/tools
target: /opt/rustock-tools
read_only: true
bind:
create_host_path: false

volumes:
rustock-data:
38 changes: 38 additions & 0 deletions docker/build.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# syntax=docker/dockerfile:1

# Keep in step with rust-toolchain.toml.
ARG RUST_VERSION=1.93.1

# The binary links this image's glibc and libstdc++; docker/runtime.Dockerfile
# must stay on the same Debian release.
FROM rust:${RUST_VERSION}-bookworm AS build

# librocksdb-sys compiles C++, and zstd-sys runs bindgen, which needs libclang.
RUN apt-get update \
&& apt-get install -y --no-install-recommends build-essential clang libclang-dev \
&& rm -rf /var/lib/apt/lists/*

# Stops rustup from fetching the components listed in rust-toolchain.toml.
ARG RUST_VERSION
ENV RUSTUP_TOOLCHAIN=${RUST_VERSION}

WORKDIR /src
COPY . .

# Comma-separated cargo features for rustock-cli, e.g. "smtp".
ARG FEATURES=""
# Maintenance tools: crates/cli/examples/<name>.rs, shipped beside the binary.
ARG TOOLS="rewind_to set_exec_head reset_exec_head block_info height_peek"
RUN --mount=type=cache,target=/usr/local/cargo/registry \
--mount=type=cache,target=/src/target \
examples=""; for t in $TOOLS; do examples="$examples --example $t"; done; \
cargo build --release --locked -p rustock-cli ${FEATURES:+--features "$FEATURES"} $examples \
&& install -D target/release/rustock-cli /out/rustock-cli \
&& for t in $TOOLS; do install -D "target/release/examples/$t" "/out/tools/$t"; done


FROM scratch AS binary
COPY --from=build /out/rustock-cli /rustock-cli

FROM scratch AS tools
COPY --from=build /out/tools/ /
76 changes: 76 additions & 0 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/bin/bash
# Turns the RUSTOCK_* environment into rustock-cli flags, then execs the node
# so it stays PID 1.

set -euo pipefail

fail() {
echo "entrypoint: $*" >&2
exit 1
}

is_port() {
[[ "$1" =~ ^[0-9]+$ ]] && [ "$1" -ge 1 ] && [ "$1" -le 65535 ]
}

is_ipv4() {
[[ "$1" =~ ^(0|[1-9][0-9]{0,2})(\.(0|[1-9][0-9]{0,2})){3}$ ]] || return 1
local octet
for octet in ${1//./ }; do
[ "$octet" -le 255 ] || return 1
done
}

data_dir=/var/lib/rustock
p2p_port="${RUSTOCK_P2P_PORT:-5050}"
discovery_port="${RUSTOCK_DISCOVERY_PORT:-5051}"
network_id="${RUSTOCK_NETWORK_ID:-30}"
external_ip="${RUSTOCK_EXTERNAL_IP:-}"
log_level="${RUSTOCK_LOG_LEVEL:-info}"
log_to_stdout="${RUSTOCK_LOG_TO_STDOUT:-true}"
extra_args="${RUSTOCK_EXTRA_ARGS:-}"

is_port "$p2p_port" || fail "RUSTOCK_P2P_PORT must be a port number, got '$p2p_port'"
is_port "$discovery_port" || fail "RUSTOCK_DISCOVERY_PORT must be a port number, got '$discovery_port'"
[ -z "$external_ip" ] || is_ipv4 "$external_ip" || fail "RUSTOCK_EXTERNAL_IP must be an IPv4 address, got '$external_ip'"
[[ "$network_id" =~ ^[0-9]+$ ]] || fail "RUSTOCK_NETWORK_ID must be a number, got '$network_id'"
[ -n "$log_level" ] || fail "RUSTOCK_LOG_LEVEL must not be empty"
case "$log_to_stdout" in
true|false) ;;
*) fail "RUSTOCK_LOG_TO_STDOUT must be true or false, got '$log_to_stdout'" ;;
esac

# The node binds discovery to the P2P port + 1 and cannot be told otherwise.
if [ "$discovery_port" -ne $((p2p_port + 1)) ]; then
fail "RUSTOCK_DISCOVERY_PORT is $discovery_port but must be RUSTOCK_P2P_PORT + 1 = $((p2p_port + 1))"
fi

# Docker creates a missing bind-mount source owned by root.
if [ ! -w "$data_dir" ]; then
fail "$data_dir is not writable by uid $(id -u); run: chown $(id -u):$(id -g) <RUSTOCK_DATA_DIR path>"
fi

args=(--config /etc/rustock/node.toml --port "$p2p_port" --network-id "$network_id" --log-level "$log_level")

if [ "$log_to_stdout" = true ]; then
args+=(--log-to-stdout)
fi

if [ -n "$external_ip" ]; then
args+=(--external-ip "$external_ip")
else
echo "entrypoint: RUSTOCK_EXTERNAL_IP is not set; networks 30 and 31 look it up, others advertise 127.0.0.1" >&2
fi

# The node gives RUST_LOG precedence over --log-level.
if [ -n "${RUST_LOG:-}" ]; then
echo "entrypoint: RUST_LOG is set and overrides RUSTOCK_LOG_LEVEL" >&2
fi

if [ -n "$extra_args" ]; then
read -r -a extra <<< "$extra_args"
args+=("${extra[@]}")
fi

echo "entrypoint: network $network_id, p2p tcp $p2p_port, discovery udp $discovery_port, log level $log_level" >&2
exec /usr/local/bin/rustock-cli "${args[@]}" "$@"
20 changes: 20 additions & 0 deletions docker/node.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Network, ports, advertised address and logging come from .env through
# entrypoint.sh as flags, which override this file. node.example.toml documents
# every setting.

[node]
data_dir = "/var/lib/rustock"

[trie]
# "single" aborts on an empty data directory; "epoch" keeps only recent state,
# so historical queries below its retention window fail. An imported rskj
# database needs "single" (see docs/docker.md).
backend = "epoch"

# Listen on 0.0.0.0: a published port cannot reach a loopback listener.
[rpc]
host = "0.0.0.0"

[metrics]
host = "0.0.0.0"
port = 9898
21 changes: 21 additions & 0 deletions docker/runtime.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Runs the prebuilt rustock-cli that docker-compose.yml mounts at /usr/local/bin.
FROM debian:bookworm-slim

RUN apt-get update \
&& apt-get install -y --no-install-recommends ca-certificates libstdc++6 \
&& rm -rf /var/lib/apt/lists/* \
&& groupadd --system --gid 10001 rustock \
&& useradd --system --uid 10001 --gid rustock --home-dir /var/lib/rustock --shell /usr/sbin/nologin rustock \
&& install -d -o rustock -g rustock /var/lib/rustock \
&& install -d /etc/rustock

COPY --chmod=755 entrypoint.sh /usr/local/bin/entrypoint.sh

USER rustock
WORKDIR /var/lib/rustock
VOLUME /var/lib/rustock

# P2P tcp, discovery udp (P2P + 1), RPC, metrics. Defaults; see .env.example.
EXPOSE 5050/tcp 5051/udp 4444/tcp 9898/tcp

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
Loading
Loading