Skip to content

Commit a7914af

Browse files
docker: setup bitcoind docker image in the docker compose yaml file
1 parent 2f04ce7 commit a7914af

File tree

7 files changed

+226
-25
lines changed

7 files changed

+226
-25
lines changed

docker/README.md

+46-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ docker | 1.13.0
1313
### Table of content
1414
* [Create lightning network cluster](#create-lightning-network-cluster)
1515
* [Connect to faucet lightning node](#connect-to-faucet-lightning-node)
16+
* [Building standalone docker images](#building-standalone-docker-images)
17+
* [Using bitcoind version](#using-bitcoind-version)
18+
* [Start Bitcoin Node with bitcoind using Docker Compose](#start-bitcoin-node-with-bitcoind-using-docker-compose)
19+
* [Generating RPCAUTH](#generating-rpcauth)
20+
* [Mining in regtest using bitcoin-cli](#mining-in-regtest-using-bitcoin-cli)
1621
* [Questions](#questions)
1722

1823
### Create lightning network cluster
@@ -327,10 +332,49 @@ Instructions on how to build standalone docker images (for development or
327332
production), outside of `docker-compose`, see the
328333
[docker docs](../docs/DOCKER.md).
329334

335+
### Using bitcoind version
336+
If you are using the bitcoind version of the compose file i.e `docker-compose-bitcoind.yml`, follow these additional instructions:
337+
338+
#### Start Bitcoin Node with bitcoind using Docker Compose
339+
To launch the Bitcoin node using bitcoind in the regtest network using Docker Compose, use the following command:
340+
```shell
341+
$ NETWORK="regtest" docker-compose -f docker-compose-bitcoind.yml up
342+
```
343+
344+
#### Generating RPCAUTH
345+
In bitcoind, the usage of `rpcuser` and `rpcpassword` for server-side authentication has been deprecated. To address this, we now use `rpcauth` instead. You can generate the necessary rpcauth credentials using the [rpcauth.py script](https://github.com/bitcoin/bitcoin/blob/master/share/rpcauth/rpcauth.py) from the Bitcoin Core repository.
346+
347+
Note: When using any RPC client, such as `lnd` or `bitcoin-cli`, It is crucial to either provide a clear text password with username or employ cookie authentication.
348+
349+
#### Mining in regtest using bitcoin-cli
350+
1. Log into the `lnd` container:
351+
```shell
352+
$ docker exec -it lnd bash
353+
```
354+
2. Generate a new backward compatible nested p2sh address:
355+
```shell
356+
lnd$ lncli --network=regtest newaddress np2wkh
357+
```
358+
3. Log into the `bitcoind` container:
359+
```shell
360+
$ docker exec -it bitcoind bash
361+
```
362+
4. Generate 101 blocks:
363+
```shell
364+
# Note: We need at least "100 >=" blocks because of coinbase block maturity.
365+
bitcoind$ bitcoin-cli -chain=regtest -rpcuser=devuser -rpcpassword=devpass generatetoaddress 101 2N1NQzFjCy1NnpAH3cT4h4GoByrAAkiH7zu
366+
```
367+
5. Check your lnd wallet balance in regtest network:
368+
```shell
369+
lnd$ lncli --network=regtest walletbalance
370+
```
371+
372+
Note: The address `2N1NQzFjCy1NnpAH3cT4h4GoByrAAkiH7zu` is just a random example. Feel free to use an address generated from your `lnd` wallet to send coins to yourself.
373+
330374
### Questions
331375
[![Irc](https://img.shields.io/badge/chat-on%20libera-brightgreen.svg)](https://web.libera.chat/#lnd)
332376

333-
* How to see `alice` | `bob` | `btcd` logs?
377+
* How to see `alice` | `bob` | `btcd` | `lnd` | `bitcoind` logs?
334378
```shell
335-
$ docker-compose logs <alice|bob|btcd>
379+
$ docker-compose logs <alice|bob|btcd|lnd|bitcoind>
336380
```

docker/bitcoind/Dockerfile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM lightninglabs/bitcoin-core:${BITCOIN_VERSION:-25}
2+
3+
# Copy the start script into the container
4+
COPY start-bitcoind.sh .
5+
6+
# Set execute permissions for the script
7+
RUN chmod +x ./start-bitcoind.sh

docker/bitcoind/start-bitcoind.sh

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env bash
2+
3+
# exit from script if error was raised.
4+
set -e
5+
6+
# error function is used within a bash function in order to send the error
7+
# message directly to the stderr output and exit.
8+
error() {
9+
echo "$1" > /dev/stderr
10+
exit 0
11+
}
12+
13+
# return is used within the bash function in order to return the value.
14+
return() {
15+
echo "$1"
16+
}
17+
18+
# set_default function gives the ability to move the setting of default
19+
# env variable from docker file to the script thereby giving the ability to the
20+
# user to override it during container start.
21+
set_default() {
22+
# docker initialized env variables with blank string and we can't just
23+
# use -z flag as usually.
24+
BLANK_STRING='""'
25+
26+
VARIABLE="$1"
27+
DEFAULT="$2"
28+
29+
if [[ -z "$VARIABLE" || "$VARIABLE" == "$BLANK_STRING" ]]; then
30+
31+
if [ -z "$DEFAULT" ]; then
32+
error "You should specify default variable"
33+
else
34+
VARIABLE="$DEFAULT"
35+
fi
36+
fi
37+
38+
return "$VARIABLE"
39+
}
40+
41+
# Set default variables if needed.
42+
43+
# This password is for testing and it is equivalent to `devpass`.
44+
# It is generated from (Note: It is fully deterministic, yet the random salt
45+
# generated by the script ensures that the resulting hash is always unique.):
46+
# https://github.com/bitcoin/bitcoin/blob/master/share/rpcauth/rpcauth.py
47+
DEFAULT_PASSWORD='22d3dd348a42d7f040487887b0ea6cc7$79ce831819539c78537884f85b65a09e15b079d79eb8f99447ea9b0a58fa66a6'
48+
49+
RPCAUTH=$(set_default "$RPCAUTH" "devuser:$DEFAULT_PASSWORD")
50+
NETWORK=$(set_default "$NETWORK" "regtest")
51+
DEBUG=$(set_default "$BITCOIND_DEBUG" "1")
52+
53+
PARAMS=""
54+
if [ "$NETWORK" != "mainnet" ]; then
55+
PARAMS="-$NETWORK"
56+
fi
57+
58+
PARAMS=$(echo $PARAMS \
59+
"-debug"="$DEBUG" \
60+
"-rpcauth"="$RPCAUTH" \
61+
"-datadir"="/data" \
62+
"-debuglogfile"="/data/debug.log" \
63+
"-rpcbind"="0.0.0.0" \
64+
"-rpcallowip"="0.0.0.0/0" \
65+
"-zmqpubrawblock"="tcp://0.0.0.0:28332" \
66+
"-zmqpubrawtx"="tcp://0.0.0.0:28333" \
67+
"-txindex"
68+
)
69+
70+
# Add user parameters to the command.
71+
PARAMS="$PARAMS $@"
72+
73+
# Print command and start bitcoin node.
74+
echo "Command: bitcoind $PARAMS"
75+
exec bitcoind $PARAMS

docker/btcd/start-btcd.sh

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ error() {
1010
exit 0
1111
}
1212

13-
# return is used within bash function in order to return the value.
13+
# return is used within the bash function in order to return the value.
1414
return() {
1515
echo "$1"
1616
}
1717

1818
# set_default function gives the ability to move the setting of default
1919
# env variable from docker file to the script thereby giving the ability to the
20-
# user override it durin container start.
20+
# user to override it during container start.
2121
set_default() {
2222
# docker initialized env variables with blank string and we can't just
2323
# use -z flag as usually.
@@ -41,7 +41,7 @@ set_default() {
4141
# Set default variables if needed.
4242
RPCUSER=$(set_default "$RPCUSER" "devuser")
4343
RPCPASS=$(set_default "$RPCPASS" "devpass")
44-
DEBUG=$(set_default "$DEBUG" "info")
44+
DEBUG=$(set_default "$BTCD_DEBUG" "info")
4545
NETWORK=$(set_default "$NETWORK" "simnet")
4646

4747
PARAMS=""
@@ -66,7 +66,7 @@ if [[ -n "$MINING_ADDRESS" ]]; then
6666
PARAMS="$PARAMS --miningaddr=$MINING_ADDRESS"
6767
fi
6868

69-
# Add user parameters to command.
69+
# Add user parameters to the command.
7070
PARAMS="$PARAMS $@"
7171

7272
# Print command and start bitcoin node.

docker/docker-compose-bitcoind.yml

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
version: '3'
2+
services:
3+
# lightninglabs/bitcoin-core is the base image for bitcoind.
4+
# The environment variables default values determined on stage of container
5+
# start within starting script.
6+
bitcoind:
7+
image: bitcoind
8+
container_name: bitcoind
9+
build:
10+
context: bitcoind/
11+
volumes:
12+
- bitcoin:/data
13+
environment:
14+
- RPCAUTH
15+
- NETWORK
16+
- BITCOIND_DEBUG
17+
entrypoint: ["./start-bitcoind.sh"]
18+
19+
lnd:
20+
image: lnd
21+
container_name: lnd
22+
build:
23+
context: ../
24+
dockerfile: dev.Dockerfile
25+
environment:
26+
- RPCUSER
27+
- RPCPASS
28+
- NETWORK
29+
- CHAIN
30+
- LND_DEBUG
31+
- BACKEND=bitcoind
32+
volumes:
33+
- lnd:/root/.lnd
34+
entrypoint: ["./start-lnd.sh"]
35+
links:
36+
- "bitcoind:blockchain"
37+
38+
volumes:
39+
# bitcoin volume is needed for maintaining blockchain persistence
40+
# during btcd container recreation.
41+
bitcoin:
42+
driver: local
43+
44+
# lnd volume is used for persisting lnd application data and chain state
45+
# during container lifecycle.
46+
lnd:
47+
driver: local

docker/docker-compose.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ services:
1515
- RPCUSER
1616
- RPCPASS
1717
- NETWORK
18-
- DEBUG
18+
- BTCD_DEBUG
1919
- MINING_ADDRESS
2020
entrypoint: ["./start-btcd.sh"]
2121

@@ -30,7 +30,8 @@ services:
3030
- RPCPASS
3131
- NETWORK
3232
- CHAIN
33-
- DEBUG
33+
- LND_DEBUG
34+
- BACKEND=btcd
3435
volumes:
3536
- shared:/rpc
3637
- lnd:/root/.lnd

docker/lnd/start-lnd.sh

+44-17
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,59 @@ set_default() {
3838
return "$VARIABLE"
3939
}
4040

41+
42+
# Set the default network and default RPC path (if any).
43+
DEFAULT_NETWORK="regtest"
44+
if [ "$BACKEND" == "btcd" ]; then
45+
DEFAULT_NETWORK="simnet"
46+
DEFAULT_RPCCRTPATH="/rpc/rpc.cert"
47+
fi
48+
4149
# Set default variables if needed.
42-
RPCCRTPATH=$(set_default "$RPCCRTPATH" "/rpc/rpc.cert")
50+
NETWORK=$(set_default "$NETWORK" "$DEFAULT_NETWORK")
51+
RPCCRTPATH=$(set_default "$RPCCRTPATH" "$DEFAULT_RPCCRTPATH")
4352
RPCHOST=$(set_default "$RPCHOST" "blockchain")
4453
RPCUSER=$(set_default "$RPCUSER" "devuser")
4554
RPCPASS=$(set_default "$RPCPASS" "devpass")
46-
DEBUG=$(set_default "$DEBUG" "debug")
47-
NETWORK=$(set_default "$NETWORK" "simnet")
55+
DEBUG=$(set_default "$LND_DEBUG" "debug")
4856
CHAIN=$(set_default "$CHAIN" "bitcoin")
49-
BACKEND="btcd"
5057
HOSTNAME=$(hostname)
5158

5259
# CAUTION: DO NOT use the --noseedback for production/mainnet setups, ever!
5360
# Also, setting --rpclisten to $HOSTNAME will cause it to listen on an IP
5461
# address that is reachable on the internal network. If you do this outside of
5562
# docker, this might be a security concern!
5663

57-
exec lnd \
58-
--noseedbackup \
59-
"--$CHAIN.active" \
60-
"--$CHAIN.$NETWORK" \
61-
"--$CHAIN.node"="$BACKEND" \
62-
"--$BACKEND.rpccert"="$RPCCRTPATH" \
63-
"--$BACKEND.rpchost"="$RPCHOST" \
64-
"--$BACKEND.rpcuser"="$RPCUSER" \
65-
"--$BACKEND.rpcpass"="$RPCPASS" \
66-
"--rpclisten=$HOSTNAME:10009" \
67-
"--rpclisten=localhost:10009" \
68-
--debuglevel="$DEBUG" \
69-
"$@"
64+
if [ "$BACKEND" == "bitcoind" ]; then
65+
exec lnd \
66+
--noseedbackup \
67+
"--$CHAIN.active" \
68+
"--$CHAIN.$NETWORK" \
69+
"--$CHAIN.node"="$BACKEND" \
70+
"--$BACKEND.rpchost"="$RPCHOST" \
71+
"--$BACKEND.rpcuser"="$RPCUSER" \
72+
"--$BACKEND.rpcpass"="$RPCPASS" \
73+
"--$BACKEND.zmqpubrawblock"="tcp://$RPCHOST:28332" \
74+
"--$BACKEND.zmqpubrawtx"="tcp://$RPCHOST:28333" \
75+
"--rpclisten=$HOSTNAME:10009" \
76+
"--rpclisten=localhost:10009" \
77+
--debuglevel="$DEBUG" \
78+
"$@"
79+
elif [ "$BACKEND" == "btcd" ]; then
80+
exec lnd \
81+
--noseedbackup \
82+
"--$CHAIN.active" \
83+
"--$CHAIN.$NETWORK" \
84+
"--$CHAIN.node"="$BACKEND" \
85+
"--$BACKEND.rpccert"="$RPCCRTPATH" \
86+
"--$BACKEND.rpchost"="$RPCHOST" \
87+
"--$BACKEND.rpcuser"="$RPCUSER" \
88+
"--$BACKEND.rpcpass"="$RPCPASS" \
89+
"--rpclisten=$HOSTNAME:10009" \
90+
"--rpclisten=localhost:10009" \
91+
--debuglevel="$DEBUG" \
92+
"$@"
93+
else
94+
echo "Unknown backend: $BACKEND"
95+
exit 1
96+
fi

0 commit comments

Comments
 (0)