docker: add single command docker build+run for btcd+lnd

This commit adds two Dockerfiles, along with a docker-compose file
which links the two docker files together allowing for single-command
deployment.

Using the docker-compose file, two containers are deployed. One running
btcd, and the other running lnd. Both containers share the same shared
volume mounted to the file system in order to allow land to read btcd’s
certificates for the TLS RPC connections.

Additionally, the btcd instance comes will an automatic RPC
configuration generated allowing one to use btcctl out of the box via
calls to “docker-compose exec btcctl …”.
This commit is contained in:
Olaoluwa Osuntokun 2016-07-16 17:55:16 -07:00
parent 14aadc8c2e
commit 62fb3a9fee
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2
6 changed files with 126 additions and 1 deletions

2
.gitignore vendored
View File

@ -24,7 +24,7 @@ _testmain.go
*.test
*.prof
lnd
/lnd
cmd/cmd
**.key

1
docker/README.md Normal file
View File

@ -0,0 +1 @@

25
docker/btcd/Dockerfile Normal file
View File

@ -0,0 +1,25 @@
FROM golang:1.6.2
MAINTAINER Olaoluwa Osuntokun <laolu@lightning.network>
# Grab and install the latest version of roasbeef's fork of btcd and all
# related dependencies.
RUN go get -u -v github.com/roasbeef/btcd/...
# Expose the mainnet, testnet, simnet, and segnet listening ports.
EXPOSE 8333 18333 18335 28901
# Expose the mainnet, testnet, simnet, and segnet rpc ports.
EXPOSE 8333 18333 18336 28902
VOLUME ["/data"]
RUN mkdir /root/.btcd && mkdir /root/.btcctl
# Generate an automatic RPC conf.
ADD initrpc.go /root/
WORKDIR /root
RUN go build -o gen-config && ./gen-config
# TODO(roabeef): ENV or prog to parse --no-tls?
ENTRYPOINT ["/go/bin/btcd", "--datadir=/data", "--logdir=/data", "--segnet", "--rpccert=/data/rpc.cert", "--rpckey=/data/rpc.key"]

64
docker/btcd/initrpc.go Normal file
View File

@ -0,0 +1,64 @@
package main
import (
"bytes"
"crypto/rand"
"encoding/base64"
"flag"
"fmt"
"io/ioutil"
"log"
"text/template"
"github.com/roasbeef/btcutil"
)
var (
numRandBytes = flag.Int("num_rand_bytes", 32, "Number of random bytes to read for both the username and password")
)
const (
autoRpcTemplate = "[Application Options]\nrpcuser={{.Username}}\nrpcpass={{.Password}}"
)
type basicRpcOptions struct {
Username string
Password string
}
func randBase64string(numBytes int) string {
randBuf := make([]byte, numBytes)
if _, err := rand.Read(randBuf); err != nil {
log.Fatalf("unable to read random bytes: %v", err)
}
return base64.StdEncoding.EncodeToString(randBuf)
}
func main() {
fmt.Println("Creating random rpc config for btcd")
t := template.Must(template.New("rpcOptions").Parse(autoRpcTemplate))
randRpcOptions := basicRpcOptions{
Username: randBase64string(*numRandBytes),
Password: randBase64string(*numRandBytes),
}
var autoAuth bytes.Buffer
if err := t.Execute(&autoAuth, randRpcOptions); err != nil {
log.Fatalf("unable to generate random auth: %v")
}
btcdHomeDir := btcutil.AppDataDir("btcd", false)
btcctlHomeDir := btcutil.AppDataDir("btcctl", false)
btcdConfigPath := fmt.Sprintf("%s/btcd.conf", btcdHomeDir)
btcctlConfigPath := fmt.Sprintf("%s/btcctl.conf", btcctlHomeDir)
if err := ioutil.WriteFile(btcdConfigPath, autoAuth.Bytes(), 0644); err != nil {
log.Fatalf("unable to write config for btcd: %v", err)
}
if err := ioutil.WriteFile(btcctlConfigPath, autoAuth.Bytes(), 0644); err != nil {
log.Fatalf("unable to write config for btcctl: %v", err)
}
fmt.Println("fin.")
}

21
docker/docker-compose.yml Normal file
View File

@ -0,0 +1,21 @@
version: '2'
services:
btcd:
container_name: btcd
build: btcd/
ports:
# TODO(roasbeef): switch to testnet after fixing peer discovery.
- "28901:28901"
volumes:
- shared-volume:/data
lnd:
container_name: lnd
build: lnd/
ports:
- "10009:10009"
volumes:
- shared-volume:/data
links:
- btcd
volumes:
shared-volume: {}

14
docker/lnd/Dockerfile Normal file
View File

@ -0,0 +1,14 @@
FROM golang:1.6.2
MAINTAINER Olaoluwa Osuntokun <laolu@lightning.network>
# Grab and install the latest version of lnd and all related dependencies.
# TODO(roasbeef): replace with glide install
RUN go get -u -v github.com/lightningnetwork/lnd/...
VOLUME ["/data"]
# Expose the p2p listening port, and the current RPC port.
EXPOSE 10009 10011
ENTRYPOINT ["/go/bin/lnd", "--datadir=/data", "--logdir=/data", "--segnet", "--btcdhost=btcd", "--rpccert=/data/rpc.cert"]