Skip to content

Commit a4acced

Browse files
authored
Merge pull request talaia-labs#227 from orbitalturtle/docker
Dockerfile and instructions
2 parents 658fcca + ddd4bd9 commit a4acced

File tree

4 files changed

+230
-0
lines changed

4 files changed

+230
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Refer to [INSTALL.md](INSTALL.md)
2323

2424
Make sure `bitcoind` is running before running `teosd` (it will fail at startup if it cannot connect to `bitcoind`). [Here](DEPENDENCIES.md#installing-bitcoind) you can find a sample bitcoin.conf.
2525

26+
Please see [Docker instructions](docker/README.md) for instructions on how to set up `teosd` in Docker.
27+
2628
### Starting the tower daemon ♖
2729

2830
Once installed, you can start the tower by running:

docker/Dockerfile

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Use the rust image as the base image for the build stage
2+
FROM rust:latest AS builder
3+
4+
# Copy the rust-teos source code
5+
COPY . /tmp/rust-teos
6+
7+
# Install the dependencies required for building rust-teos
8+
RUN apt-get update\
9+
&& apt-get -y --no-install-recommends install libffi-dev libssl-dev musl-tools pkg-config
10+
11+
RUN cd /tmp/rust-teos \
12+
&& rustup target add x86_64-unknown-linux-musl \
13+
# Rustfmt is needed to format the grpc stubs generated by tonic
14+
&& rustup component add rustfmt \
15+
# Cross compile with musl as the target, so teosd can run on alpine
16+
&& RUSTFLAGS='-C target-feature=+crt-static' cargo build --manifest-path=teos/Cargo.toml --locked --release --target x86_64-unknown-linux-musl
17+
18+
# Use a new stage with a smaller base image to reduce image size
19+
FROM alpine:latest
20+
21+
RUN apk update && apk upgrade
22+
23+
# UID and GID for the teosd user
24+
ENV TEOS_UID=1001 TEOS_GID=1001
25+
26+
# Copy the teos binaries from the build stage to the new stage
27+
COPY --from=builder \
28+
/tmp/rust-teos/target/x86_64-unknown-linux-musl/release/teosd \
29+
/tmp/rust-teos/target/x86_64-unknown-linux-musl/release/teos-cli /usr/local/bin/
30+
31+
# Copy the entrypoint script to the container
32+
COPY docker/entrypoint.sh /entrypoint.sh
33+
34+
# Set the entrypoint script as executable and add running user
35+
RUN chmod +x /entrypoint.sh \
36+
&& addgroup -g ${TEOS_GID} -S teos \
37+
&& adduser -S -G teos -u ${TEOS_UID} teos
38+
39+
# Expose the default port used by teosd
40+
EXPOSE 9814/tcp
41+
42+
# Switch user so that we don't run stuff as root
43+
USER teos
44+
45+
# Create the teos data directory
46+
RUN mkdir /home/teos/.teos
47+
48+
# Start teosd when the container starts
49+
ENTRYPOINT [ "/entrypoint.sh" ]

docker/README.md

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
## Running `teosd` in a docker container
2+
A `teos` image can be built from the Dockerfile located in `docker`. You can create the image by running:
3+
4+
cd rust-teos
5+
docker build -f docker/Dockerfile -t teos .
6+
7+
Then we can create a container by running:
8+
9+
docker run -it teos
10+
11+
One way to feed `teos` custom config options is to set environment variables:
12+
13+
docker run -it -e <ENV_VARIABLES> teos
14+
15+
Notice that the ENV variables are optional, if unset the corresponding default setting is used. The following ENVs are available:
16+
17+
```
18+
- API_BIND=<teos_api_hostname>
19+
- API_PORT=<teos_api_port>
20+
- RPC_BIND=<teos_rpc_hostname>
21+
- RPC_PORT=<teos_rpc_port>
22+
- BTC_NETWORK=<btc_network>
23+
- BTC_RPC_CONNECT=<btc_node_hostname>
24+
- BTC_RPC_PORT=<btc_node_port>
25+
- BTC_RPC_USER=<btc_rpc_username>
26+
- BTC_RPC_PASSWORD=<btc_rpc_password>
27+
# The following options can be set turned on by setting them to "true"
28+
- DEBUG=<debug_bool>
29+
- DEPS_DEBUG=<deps_debug_bool>
30+
- OVERWRITE_KEY=<overwrite_key_bool>
31+
- FORCE_UPDATE=<force_update_bool>
32+
```
33+
34+
### Volume persistence
35+
36+
You may also want to run docker with a volume, so you can have data persistence in `teosd` databases and keys.
37+
If so, run:
38+
39+
docker volume create teos-data
40+
41+
And add the the mount parameter to `docker run`:
42+
43+
-v teos-data:/home/teos/.teos
44+
45+
If you are running `teosd` and `bitcoind` in the same machine, continue reading for how to create the container based on your OS.
46+
47+
### `bitcoind` running on the same machine (UNIX)
48+
The easiest way to run both together in the same machine using UNIX is to set the container to use the host network.
49+
50+
For example, if both `teosd` and `bitcoind` are running on default settings, run:
51+
52+
```
53+
docker run \
54+
--network=host \
55+
--name teos \
56+
-v teos-data:/home/teos/.teos \
57+
-e BTC_RPC_USER=<btc_rpc_username> \
58+
-e BTC_RPC_PASSWORD=<btc_rpc_password> \
59+
-it teos
60+
```
61+
62+
Notice that you may still need to set your RPC authentication details, since, hopefully, your credentials won't match the `teosd` defaults.
63+
64+
### `bitcoind` running on the same machine (OSX or Windows)
65+
66+
Docker for OSX and Windows does not allow to use the host network (nor to use the `docker0` bridge interface). To work around this
67+
you can use the special `host.docker.internal` domain:
68+
69+
```
70+
docker run \
71+
-p 9814:9814 \
72+
-p 8814:8814 \
73+
--name teos \
74+
-v teos-data:/home/teos/.teos \
75+
-e BTC_RPC_CONNECT=host.docker.internal \
76+
-e BTC_RPC_USER=<btc_rpc_username> \
77+
-e BTC_RPC_PASSWORD=<btc_rpc_password> \
78+
-e API_BIND=0.0.0.0 \
79+
-e RPC_BIND=0.0.0.0 \
80+
-it teos
81+
```
82+
83+
Notice that we also needed to add `API_BIND=0.0.0.0` and `RPC_BIND=0.0.0.0` to bind the API to all interfaces of the container.
84+
Otherwise it will bind to `localhost` and we won't be able to send requests to the tower from the host.
85+
86+
### Interacting with a TEOS instance
87+
88+
Once our `teos` instance is running in the container, we can interact with it using `teos-cli`. We have two main ways of doing so:
89+
90+
1) You can open a shell to the Docker instance by calling:
91+
92+
`docker exec -it <CONTAINER_NAME> sh`
93+
94+
Then you can use the `teos-cli` binary from inside the container as you would use it from your host machine.
95+
96+
2) Using `teos-cli` remotely (assuming you have it installed in the source machine) and pointing to the container. To do so, you will need to copy over the necessary credentials to the host machine. To do so, you can follow the instructions in [the main README](https://github.com/talaia-labs/rust-teos/blob/master/README.md#running-teos-cli-remotely).
97+
98+
### Plugging in Tor
99+
100+
You may have noticed, in the above section where the environment variables are covered, that the Tor options are nowhere to be found. That's because these instructions assume that users will likely be setting up Tor in another container.
101+
102+
On the machine where you have Tor running, you can follow [these instructions](https://community.torproject.org/onion-services/setup/) for setting up a hidden service manually.
103+
104+
For instance, if you're running `teosd` in a Docker container on the same machine as where Tor is running, you can create a hidden service from the host machine to hide the IP of the `teosd` API (listening on port 9814 for example). If you're using Linux, you can do so by editing your `torrc` file on the host machine with the below option:
105+
106+
```
107+
HiddenServiceDir /var/lib/tor/teosd # Path for Linux. This may differ depending on your OS.
108+
HiddenServicePort 9814 127.0.0.1:9814
109+
```
110+
111+
Then restart Tor.
112+
113+
If all works correctly, the hidden service public key will be located in the `HiddenServiceDir` you set above, in the file called `hostname`.

docker/entrypoint.sh

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/bin/sh
2+
3+
# Define the start command
4+
START_COMMAND="teosd"
5+
6+
# Set the API bind address
7+
if [[ ! -z ${API_BIND} ]]; then
8+
START_COMMAND="$START_COMMAND --apibind $API_BIND"
9+
fi
10+
11+
# Set the API port
12+
if [[ ! -z ${API_PORT} ]]; then
13+
START_COMMAND="$START_COMMAND --apiport $API_PORT"
14+
fi
15+
16+
# Set the RPC bind address
17+
if [[ ! -z ${RPC_BIND} ]]; then
18+
START_COMMAND="$START_COMMAND --rpcbind $RPC_BIND"
19+
fi
20+
21+
# Set the RPC port
22+
if [[ ! -z ${RPC_PORT} ]]; then
23+
START_COMMAND="$START_COMMAND --rpcport $RPC_PORT"
24+
fi
25+
26+
# Set the Bitcoin network
27+
if [[ ! -z ${BTC_NETWORK} ]]; then
28+
START_COMMAND="$START_COMMAND --btcnetwork $BTC_NETWORK"
29+
fi
30+
31+
# Set the Bitcoin RPC credentials
32+
if [[ ! -z ${BTC_RPC_USER} ]]; then
33+
START_COMMAND="$START_COMMAND --btcrpcuser $BTC_RPC_USER"
34+
fi
35+
36+
if [[ ! -z ${BTC_RPC_PASSWORD} ]]; then
37+
START_COMMAND="$START_COMMAND --btcrpcpassword $BTC_RPC_PASSWORD"
38+
fi
39+
40+
# Set the Bitcoin RPC connection details
41+
if [[ ! -z ${BTC_RPC_CONNECT} ]]; then
42+
START_COMMAND="$START_COMMAND --btcrpcconnect $BTC_RPC_CONNECT"
43+
fi
44+
45+
if [[ ! -z ${BTC_RPC_PORT} ]]; then
46+
START_COMMAND="$START_COMMAND --btcrpcport $BTC_RPC_PORT"
47+
fi
48+
49+
if [ "${DEBUG}" == "true" ]; then
50+
START_COMMAND="$START_COMMAND --debug"
51+
fi
52+
53+
if [ "${DEPS_DEBUG}" == "true" ]; then
54+
START_COMMAND="$START_COMMAND --depsdebug"
55+
fi
56+
57+
if [ "${OVERWRITE_KEY}" == "true" ]; then
58+
START_COMMAND="$START_COMMAND --overwritekey"
59+
fi
60+
61+
if [ "${FORCE_UPDATE}" == "true" ]; then
62+
START_COMMAND="$START_COMMAND --forceupdate"
63+
fi
64+
65+
# Start the TEOS daemon
66+
$START_COMMAND

0 commit comments

Comments
 (0)