From e4a09fad287e2593e5a0bc757c3143ccc1ded400 Mon Sep 17 00:00:00 2001 From: "Javier G. Montoya S" Date: Sun, 10 Nov 2024 19:47:05 -0300 Subject: [PATCH] feat(docker): unify mostro and relay compose also improve docker image and ergonomics --- .gitignore | 8 +-- Makefile | 44 ++++++++++++ docker/.env.example | 6 ++ docker/DOCKER.md | 70 ++++++++++++++----- docker/Dockerfile | 35 +++++++--- docker/compose.yml | 16 ++++- relay/config.toml => docker/relay_config.toml | 2 +- docker/settings.docker.toml | 8 +-- relay/.env.example | 2 - relay/docker-compose.yml | 12 ---- 10 files changed, 149 insertions(+), 54 deletions(-) create mode 100644 Makefile create mode 100644 docker/.env.example rename relay/config.toml => docker/relay_config.toml (99%) delete mode 100644 relay/.env.example delete mode 100644 relay/docker-compose.yml diff --git a/.gitignore b/.gitignore index 43d74bfd..0bd33df7 100644 --- a/.gitignore +++ b/.gitignore @@ -3,9 +3,6 @@ mostro.db* mostro.log -# Relay data -/relay/data/* - # IDE's .idea .vscode @@ -14,4 +11,7 @@ mostro.log settings.toml book/book/ -bin/ \ No newline at end of file +bin/ + +# Docker related config and data +docker/config diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..1d5d569d --- /dev/null +++ b/Makefile @@ -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 diff --git a/docker/.env.example b/docker/.env.example new file mode 100644 index 00000000..afe088f4 --- /dev/null +++ b/docker/.env.example @@ -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 diff --git a/docker/DOCKER.md b/docker/DOCKER.md index c7ac9ca5..0354d492 100644 --- a/docker/DOCKER.md +++ b/docker/DOCKER.md @@ -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 + ``` diff --git a/docker/Dockerfile b/docker/Dockerfile index 0f8e1bd7..94aff077 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -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"] diff --git a/docker/compose.yml b/docker/compose.yml index fd827d49..dddbf15a 100644 --- a/docker/compose.yml +++ b/docker/compose.yml @@ -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 diff --git a/relay/config.toml b/docker/relay_config.toml similarity index 99% rename from relay/config.toml rename to docker/relay_config.toml index 2709b868..8c8f9f63 100644 --- a/relay/config.toml +++ b/docker/relay_config.toml @@ -214,4 +214,4 @@ reject_future_seconds = 1800 # Whether or not new sign ups should be allowed #sign_ups = false -#secret_key = "" \ No newline at end of file +#secret_key = "" diff --git a/docker/settings.docker.toml b/docker/settings.docker.toml index fda3625e..1da3108a 100644 --- a/docker/settings.docker.toml +++ b/docker/settings.docker.toml @@ -1,8 +1,8 @@ [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 @@ -10,7 +10,7 @@ lnd_grpc_host = 'https://host.docker.internal:10001' 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 @@ -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 diff --git a/relay/.env.example b/relay/.env.example deleted file mode 100644 index 555c81e7..00000000 --- a/relay/.env.example +++ /dev/null @@ -1,2 +0,0 @@ -# Port for local relay -MOSTRO_RELAY_LOCAL_PORT=7000 \ No newline at end of file diff --git a/relay/docker-compose.yml b/relay/docker-compose.yml deleted file mode 100644 index 34601f7a..00000000 --- a/relay/docker-compose.yml +++ /dev/null @@ -1,12 +0,0 @@ -version: '3.8' - -services: - nostr-relay: - image: scsibug/nostr-rs-relay - container_name: nostr-relay - ports: - - '${MOSTRO_RELAY_LOCAL_PORT:-7000}:8080' - volumes: - - './data:/usr/src/app/db:Z' - - './config.toml:/usr/src/app/config.toml:ro,Z' -