From 62fb3a9fee8e39871f4372d4bc148e51f01dbc02 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Sat, 16 Jul 2016 17:55:16 -0700 Subject: [PATCH] docker: add single command docker build+run for btcd+lnd MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 …”. --- .gitignore | 2 +- docker/README.md | 1 + docker/btcd/Dockerfile | 25 +++++++++++++++ docker/btcd/initrpc.go | 64 +++++++++++++++++++++++++++++++++++++++ docker/docker-compose.yml | 21 +++++++++++++ docker/lnd/Dockerfile | 14 +++++++++ 6 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 docker/README.md create mode 100644 docker/btcd/Dockerfile create mode 100644 docker/btcd/initrpc.go create mode 100644 docker/docker-compose.yml create mode 100644 docker/lnd/Dockerfile diff --git a/.gitignore b/.gitignore index 2d4d343a..39b75505 100644 --- a/.gitignore +++ b/.gitignore @@ -24,7 +24,7 @@ _testmain.go *.test *.prof -lnd +/lnd cmd/cmd **.key diff --git a/docker/README.md b/docker/README.md new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/docker/README.md @@ -0,0 +1 @@ + diff --git a/docker/btcd/Dockerfile b/docker/btcd/Dockerfile new file mode 100644 index 00000000..088d474e --- /dev/null +++ b/docker/btcd/Dockerfile @@ -0,0 +1,25 @@ +FROM golang:1.6.2 + +MAINTAINER Olaoluwa Osuntokun + +# 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"] diff --git a/docker/btcd/initrpc.go b/docker/btcd/initrpc.go new file mode 100644 index 00000000..331da39b --- /dev/null +++ b/docker/btcd/initrpc.go @@ -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.") +} diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml new file mode 100644 index 00000000..e790fb36 --- /dev/null +++ b/docker/docker-compose.yml @@ -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: {} diff --git a/docker/lnd/Dockerfile b/docker/lnd/Dockerfile new file mode 100644 index 00000000..4d2de759 --- /dev/null +++ b/docker/lnd/Dockerfile @@ -0,0 +1,14 @@ +FROM golang:1.6.2 + +MAINTAINER Olaoluwa Osuntokun + +# 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"]