Skip to content

Commit 5d8c281

Browse files
authored
Merge pull request #1415 from opentensor/feat/roman/add-localnet-docker-image
Add Docker Image with localnet nodes
2 parents 733353b + f940aeb commit 5d8c281

File tree

4 files changed

+164
-2
lines changed

4 files changed

+164
-2
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Build Localnet Docker Image
2+
3+
on:
4+
pull_request:
5+
6+
jobs:
7+
build:
8+
runs-on: SubtensorCI
9+
10+
steps:
11+
- name: Checkout code
12+
uses: actions/checkout@v4
13+
14+
- name: Set up QEMU
15+
uses: docker/setup-qemu-action@v2
16+
17+
- name: Set up Docker Buildx
18+
uses: docker/setup-buildx-action@v2
19+
20+
- name: Build Docker Image
21+
run: docker build -f Dockerfile-localnet -t localnet .
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
name: Publish Localnet Docker Image
2+
3+
on:
4+
release:
5+
types: [published]
6+
workflow_dispatch:
7+
inputs:
8+
branch-or-tag:
9+
description: "Branch or tag to use for the Docker image tag and ref to checkout (optional)"
10+
required: false
11+
default: ""
12+
push:
13+
branches:
14+
- devnet-ready
15+
16+
permissions:
17+
contents: read
18+
packages: write
19+
actions: read
20+
security-events: write
21+
22+
jobs:
23+
publish:
24+
runs-on: SubtensorCI
25+
26+
steps:
27+
- name: Determine Docker tag and ref
28+
id: tag
29+
run: |
30+
branch_or_tag="${{ github.event.inputs.branch-or-tag || github.ref_name }}"
31+
echo "Determined branch or tag: $branch_or_tag"
32+
echo "tag=$branch_or_tag" >> $GITHUB_ENV
33+
echo "ref=$branch_or_tag" >> $GITHUB_ENV
34+
35+
# Check if this is a tagged release (not devnet-ready/devnet/testnet)
36+
if [[ "$branch_or_tag" != "devnet-ready" ]]; then
37+
echo "latest_tag=true" >> $GITHUB_ENV
38+
else
39+
echo "latest_tag=false" >> $GITHUB_ENV
40+
fi
41+
42+
- name: Checkout code
43+
uses: actions/checkout@v4
44+
with:
45+
ref: ${{ env.ref }}
46+
47+
- name: Set up QEMU
48+
uses: docker/setup-qemu-action@v2
49+
50+
- name: Set up Docker Buildx
51+
uses: docker/setup-buildx-action@v2
52+
53+
- name: Login to GHCR
54+
uses: docker/login-action@v2
55+
with:
56+
registry: ghcr.io
57+
username: ${{ github.actor }}
58+
password: ${{ secrets.GITHUB_TOKEN }}
59+
60+
- name: Build and push Docker image
61+
uses: docker/build-push-action@v4
62+
with:
63+
context: .
64+
file: Dockerfile-localnet
65+
push: true
66+
platforms: linux/amd64,linux/arm64
67+
tags: |
68+
ghcr.io/${{ github.repository }}-localnet:${{ env.tag }}
69+
${{ env.latest_tag == 'true' && format('ghcr.io/{0}-localnet:latest', github.repository) || '' }}

Dockerfile-localnet

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
ARG BASE_IMAGE=ubuntu:latest
2+
3+
FROM $BASE_IMAGE AS builder
4+
SHELL ["/bin/bash", "-c"]
5+
6+
# Set noninteractive mode for apt-get
7+
ARG DEBIAN_FRONTEND=noninteractive
8+
9+
LABEL ai.opentensor.image.authors="[email protected]" \
10+
ai.opentensor.image.vendor="Opentensor Foundation" \
11+
ai.opentensor.image.title="opentensor/subtensor-localnet" \
12+
ai.opentensor.image.description="Opentensor Subtensor Blockchain" \
13+
ai.opentensor.image.documentation="https://docs.bittensor.com"
14+
15+
# Set up Rust environment
16+
ENV RUST_BACKTRACE=1
17+
18+
RUN apt-get update
19+
RUN apt-get install -y curl build-essential protobuf-compiler clang git pkg-config libssl-dev llvm libudev-dev
20+
21+
# Copy entire repository
22+
COPY . /build
23+
WORKDIR /build
24+
25+
# Install Rust
26+
RUN set -o pipefail && curl https://sh.rustup.rs -sSf | sh -s -- -y
27+
ENV PATH="/root/.cargo/bin:${PATH}"
28+
RUN rustup toolchain install
29+
RUN rustup target add wasm32-unknown-unknown
30+
31+
## Build fast-blocks node
32+
RUN ./scripts/localnet.sh --build-only
33+
# Build non-fast-blocks
34+
RUN ./scripts/localnet.sh False --build-only
35+
36+
# Verify the binaries was produced
37+
RUN test -e /build/target/fast-blocks/release/node-subtensor
38+
RUN test -e /build/target/non-fast-blocks/release/node-subtensor
39+
40+
FROM $BASE_IMAGE AS subtensor-localnet
41+
42+
# Copy binaries
43+
COPY --from=builder /build/target/fast-blocks/release/node-subtensor target/fast-blocks/release/node-subtensor
44+
RUN chmod +x target/fast-blocks/release/node-subtensor
45+
46+
COPY --from=builder /build/target/non-fast-blocks/release/node-subtensor target/non-fast-blocks/release/node-subtensor
47+
RUN chmod +x target/non-fast-blocks/release/node-subtensor
48+
49+
COPY --from=builder /build/snapshot.json /snapshot.json
50+
51+
COPY --from=builder /build/scripts/localnet.sh scripts/localnet.sh
52+
RUN chmod +x /scripts/localnet.sh
53+
54+
## Ubdate certificates
55+
RUN apt-get update && apt-get install -y ca-certificates
56+
57+
# Do not build (just run)
58+
ENV BUILD_BINARY=0
59+
# Switch to local run with IP 0.0.0.0 within docker image
60+
ENV RUN_IN_DOCKER=1
61+
# Expose ports
62+
EXPOSE 30334 30335 9944 9945
63+
64+
ENTRYPOINT ["/scripts/localnet.sh"]
65+
CMD ["True"]

scripts/localnet.sh

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ if [[ $BUILD_BINARY == "1" ]]; then
6161
fi
6262

6363
echo "*** Building chainspec..."
64-
"$BUILD_DIR/release/node-subtensor" build-spec --disable-default-bootnode --raw --chain $CHAIN >$FULL_PATH
64+
"$BUILD_DIR/release/node-subtensor" build-spec --disable-default-bootnode --raw --chain "$CHAIN" >"$FULL_PATH"
6565
echo "*** Chainspec built and output to file"
6666

6767
# Generate node keys
@@ -79,6 +79,7 @@ fi
7979

8080
if [ $BUILD_ONLY -eq 0 ]; then
8181
echo "*** Starting localnet nodes..."
82+
8283
alice_start=(
8384
"$BUILD_DIR/release/node-subtensor"
8485
--base-path /tmp/alice
@@ -107,11 +108,17 @@ if [ $BUILD_ONLY -eq 0 ]; then
107108
--unsafe-force-node-key-generation
108109
)
109110

111+
# Provide RUN_IN_DOCKER local environment variable if run script in the docker image
112+
if [ "${RUN_IN_DOCKER}" == "1" ]; then
113+
alice_start+=(--unsafe-rpc-external)
114+
bob_start+=(--unsafe-rpc-external)
115+
fi
116+
110117
trap 'pkill -P $$' EXIT SIGINT SIGTERM
111118

112119
(
113120
("${alice_start[@]}" 2>&1) &
114121
("${bob_start[@]}" 2>&1)
115122
wait
116123
)
117-
fi
124+
fi

0 commit comments

Comments
 (0)