Skip to content

Commit

Permalink
feat(docker): unify mostro and relay compose (#392)
Browse files Browse the repository at this point in the history
also improve docker image and ergonomics
  • Loading branch information
jgmontoya authored Nov 11, 2024
1 parent 579abbb commit 8542920
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 54 deletions.
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
mostro.db*
mostro.log

# Relay data
/relay/data/*

# IDE's
.idea
.vscode
Expand All @@ -14,4 +11,7 @@ mostro.log
settings.toml

book/book/
bin/
bin/

# Docker related config and data
docker/config
44 changes: 44 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
docker-build:
@set -o pipefail; \
cd docker && \
set -a && source .env && set +a && \
mkdir -p config/lnd && \
echo "Checking LND files..." && \
echo "LND_CERT_FILE=$${LND_CERT_FILE}" && \
echo "LND_MACAROON_FILE=$${LND_MACAROON_FILE}" && \
if [ ! -f "$${LND_CERT_FILE}" ]; then \
echo "Error: LND cert file not found at: $${LND_CERT_FILE}"; \
exit 1; \
fi && \
if [ ! -f "$${LND_MACAROON_FILE}" ]; then \
echo "Error: LND macaroon file not found at: $${LND_MACAROON_FILE}"; \
exit 1; \
fi && \
echo "Copying LND cert and macaroon to docker config" && \
cp -v $${LND_CERT_FILE} config/lnd/tls.cert && \
cp -v $${LND_MACAROON_FILE} config/lnd/admin.macaroon && \
echo "Building docker image" && \
docker compose build

docker-up:
@set -o pipefail; \
cd docker && \
echo "Copying Nostr relay config" && \
mkdir -p config/relay && \
cp -v ./relay_config.toml config/relay/config.toml && \
echo "Starting services" && \
docker compose up -d

docker-relay-up:
@set -o pipefail; \
cd docker && \
echo "Copying Nostr relay config" && \
mkdir -p config/relay && \
cp -v ./relay_config.toml config/relay/config.toml && \
echo "Starting Nostr relay" && \
docker compose up -d nostr-relay

docker-down:
@set -o pipefail; \
cd docker && \
docker compose down
6 changes: 6 additions & 0 deletions docker/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# LND TLS certificate and macaroon files (required)
LND_CERT_FILE=
LND_MACAROON_FILE=

# Port for local relay
MOSTRO_RELAY_LOCAL_PORT=7000
70 changes: 51 additions & 19 deletions docker/DOCKER.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,75 @@ This guide provides instructions for building and running the MostroP2P applicat

Ensure you have Docker and Docker Compose installed on your machine. You can download Docker from [here](https://www.docker.com/get-started) and Docker Compose from [here](https://docs.docker.com/compose/install/).

## Docker Compose Configuration
You need to have a LND node running locally. We recommend using [Polar](https://lightningpolar.com/) for this.

The `compose.yml` file is configured as follows:
## Docker Compose Configuration

```yaml
services:
mostro:
build:
context: ..
dockerfile: docker/Dockerfile
volumes:
- ./config:/config # settings.toml and mostro.db
- ~/.polar/networks/1/volumes/lnd:/lnd # LND data
platform: linux/amd64
The `compose.yml` sets up the following services:

```
- `mostro`: the MostroP2P service
- `nostr-relay`: the Nostr relay

## Building and Running the Docker Container

To build and run the Docker container using Docker Compose, follow these steps:

### Steps for running the MostroP2P service and Nostr relay

1. Clone the repository:

```sh
git clone https://github.com/MostroP2P/mostro.git
cd mostro/docker
```

2. Ensure you have the `settings.toml` configuration file and the `mostro.db` SQLite database in a `config` directory (acording to the `volumes` section). If you don't have those files from a previous installation, then the first time they will be created as follows:

- `settings.toml` from the settings.docker.toml template
- `mostro.db` from (empty) database mostro.empty.db
- `docker/config/settings.toml` from the `docker/settings.docker.toml` template
- `docker/config/mostro.db` from the `docker/empty.mostro.db` database

3. Set the `LND_CERT_FILE` and `LND_MACAROON_FILE` to the paths of the LND TLS certificate and macaroon files on the `docker/.env` file. These files will be copied to the `docker/config/lnd` directory. For example:

```sh
LND_CERT_FILE=~/.polar/networks/1/volumes/lnd/alice/tls.cert
LND_MACAROON_FILE=~/.polar/networks/1/volumes/lnd/alice/data/chain/bitcoin/regtest/admin.macaroon
```

4. [Optional] Set the `MOSTRO_RELAY_LOCAL_PORT` to the port you want to use for the local relay on the `docker/.env` file. For example:

```sh
MOSTRO_RELAY_LOCAL_PORT=7000
```

5. Build the docker image:

```sh
make docker-build
```

6. Run the docker compose file:

```sh
make docker-up
```

## Stopping the Docker Container

3. Run Docker Compose:
To stop the Docker container, run:

```sh
make docker-down
```

## Steps for running just the Nostr relay

1. Run the following command to start the Nostr relay:

```sh
docker compose up --build -d
make docker-relay-up
```

This command will build the Docker image and run the container in detached mode.
2. Stop the Nostr relay:

```sh
make docker-down
```
35 changes: 24 additions & 11 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,37 +1,50 @@
# Build stage
FROM rust:1.81 AS builder
FROM rust:1.82 AS builder

# Install build dependencies
RUN apt-get update && \
apt-get install -y --no-install-recommends cmake build-essential libsqlite3-dev pkg-config libssl-dev protobuf-compiler && \
rm -rf /var/lib/apt/lists/*


# Set working directory
WORKDIR /mostro

# Copy Cargo.toml and Cargo.lock to leverage Docker cache
COPY Cargo.toml Cargo.lock ./
RUN cargo fetch

# Copy source code
COPY . .

# Install build dependencies
RUN apt-get update && \
apt-get install -y cmake build-essential libsqlite3-dev pkg-config libssl-dev protobuf-compiler

# Build the project in release mode
RUN cargo build --release

# Production stage
FROM debian:bookworm-slim

# Install dependencies
RUN apt-get update && apt-get install -y --reinstall ca-certificates

# Add a non-root user
RUN useradd -m mostrouser

# Copy built binary from build stage
COPY --from=builder /mostro/target/release/mostrod /usr/local/bin/mostrod

WORKDIR /mostro
WORKDIR /home/mostrouser

# Copy settings and empty database
COPY --chown=mostrouser:mostrouser ./docker/settings.docker.toml ./docker/empty.mostro.db ./
COPY ./docker/settings.docker.toml ./docker/empty.mostro.db ./

# Copy start script
COPY --chown=mostrouser:mostrouser ./docker/start.sh ./start.sh
COPY ./docker/start.sh ./start.sh
RUN chmod +x ./start.sh

# Add a non-root user and switch to it
RUN useradd -m mostrouser
RUN chown -R mostrouser:mostrouser /home/mostrouser

# Switch to non-root user
USER mostrouser

# Start mostro (copy settings and database if it's not created yet)
ENTRYPOINT ["./start.sh"]
CMD ["./start.sh"]
16 changes: 15 additions & 1 deletion docker/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,19 @@ services:
dockerfile: docker/Dockerfile
volumes:
- ./config:/config # settings.toml and mostro.db
- ~/.polar/networks/1/volumes/lnd:/lnd # LND data
platform: linux/amd64
networks:
- default

nostr-relay:
image: scsibug/nostr-rs-relay
container_name: nostr-relay
ports:
- '${MOSTRO_RELAY_LOCAL_PORT:-7000}:8080'
volumes:
- './config/relay/data:/usr/src/app/db:Z'
- './config/relay/config.toml:/usr/src/app/config.toml:ro,Z'

networks:
default:
driver: bridge
2 changes: 1 addition & 1 deletion relay/config.toml → docker/relay_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,4 @@ reject_future_seconds = 1800

# Whether or not new sign ups should be allowed
#sign_ups = false
#secret_key = "<nostr nsec>"
#secret_key = "<nostr nsec>"
8 changes: 4 additions & 4 deletions docker/settings.docker.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
[lightning]
# path to tls.cert file
lnd_cert_file = '/lnd/alice/tls.cert'
lnd_cert_file = '/config/lnd/tls.cert'
# path to macaroon file
lnd_macaroon_file = '/lnd/alice/data/chain/bitcoin/regtest/admin.macaroon'
lnd_macaroon_file = '/config/lnd/admin.macaroon'
# lnd grpc host and port
lnd_grpc_host = 'https://host.docker.internal:10001'
# lightning invoices sent by the buyer to Mostro should have at least
# this expiration time in seconds
invoice_expiration_window = 3600
# Hold invoice cltv delta (expiration time in blocks)
hold_invoice_cltv_delta = 144
# This is the time that a taker has to pay the invoice (seller) or
# This is the time that a taker has to pay the invoice (seller) or
# to add a new invoice (buyer), in seconds
hold_invoice_expiration_window = 300
# Retries for failed payments
Expand All @@ -20,7 +20,7 @@ payment_retries_interval = 60

[nostr]
nsec_privkey = 'nsec1...'
relays = ['ws://localhost:7000']
relays = ['ws://host.docker.internal:7000', 'ws://localhost:7000']

[mostro]
# Mostro Fee
Expand Down
2 changes: 0 additions & 2 deletions relay/.env.example

This file was deleted.

12 changes: 0 additions & 12 deletions relay/docker-compose.yml

This file was deleted.

0 comments on commit 8542920

Please sign in to comment.