Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(docker): unify mostro and relay compose #392

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines +23 to +30
Copy link
Contributor

@coderabbitai coderabbitai bot Nov 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add validation checks in docker-up target.

The target should validate the relay configuration file and check for running services before proceeding.

Suggested improvement:

 docker-up:
 	@set -o pipefail; \
 	cd docker && \
+	if [ ! -f "./relay_config.toml" ]; then \
+		echo "Error: relay_config.toml not found"; \
+		exit 1; \
+	fi && \
 	echo "Copying Nostr relay config" && \
 	mkdir -p config/relay && \
 	cp -v ./relay_config.toml config/relay/config.toml && \
+	if docker compose ps --quiet | grep -q .; then \
+		echo "Warning: Some services are already running"; \
+	fi && \
 	echo "Starting services" && \
 	docker compose up -d
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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-up:
@set -o pipefail; \
cd docker && \
if [ ! -f "./relay_config.toml" ]; then \
echo "Error: relay_config.toml not found"; \
exit 1; \
fi && \
echo "Copying Nostr relay config" && \
mkdir -p config/relay && \
cp -v ./relay_config.toml config/relay/config.toml && \
if docker compose ps --quiet | grep -q .; then \
echo "Warning: Some services are already running"; \
fi && \
echo "Starting services" && \
docker compose up -d

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is really needed (or actually wanted for that matter 🙄)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jgmontoya, understood. I'll disregard the suggestion. Let me know if there's anything else I can assist with.


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
Comment on lines +32 to +39
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Reduce code duplication with docker-up target.

The relay configuration copying logic is duplicated between docker-up and docker-relay-up. Consider extracting this into a separate target.

Suggested refactor:

+copy-relay-config:
+	@set -o pipefail; \
+	cd docker && \
+	if [ ! -f "./relay_config.toml" ]; then \
+		echo "Error: relay_config.toml not found"; \
+		exit 1; \
+	fi && \
+	echo "Copying Nostr relay config" && \
+	mkdir -p config/relay && \
+	cp -v ./relay_config.toml config/relay/config.toml
+
 docker-up: copy-relay-config
-	@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:
+docker-relay-up: copy-relay-config
 	@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
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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
copy-relay-config:
@set -o pipefail; \
cd docker && \
if [ ! -f "./relay_config.toml" ]; then \
echo "Error: relay_config.toml not found"; \
exit 1; \
fi && \
echo "Copying Nostr relay config" && \
mkdir -p config/relay && \
cp -v ./relay_config.toml config/relay/config.toml
docker-up: copy-relay-config
@set -o pipefail; \
cd docker && \
echo "Starting services" && \
docker compose up -d
docker-relay-up: copy-relay-config
@set -o pipefail; \
cd docker && \
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'
Comment on lines +12 to +19
Copy link
Contributor

@coderabbitai coderabbitai bot Nov 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider adding container reliability configurations.

While the basic setup is correct, consider the following improvements for production readiness:

  1. Add a restart policy
  2. Add a health check
  3. Consider using named volumes instead of relative paths

Apply these improvements:

  nostr-relay:
    image: scsibug/nostr-rs-relay
    container_name: nostr-relay
+   restart: unless-stopped
+   healthcheck:
+     test: ["CMD", "curl", "-f", "http://localhost:8080/"]
+     interval: 30s
+     timeout: 10s
+     retries: 3
    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'
+     - nostr_relay_data:/usr/src/app/db
+     - type: bind
+       source: ./config/relay/config.toml
+       target: /usr/src/app/config.toml
+       read_only: true

volumes:
  nostr_relay_data:
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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'
nostr-relay:
image: scsibug/nostr-rs-relay
container_name: nostr-relay
restart: unless-stopped
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/"]
interval: 30s
timeout: 10s
retries: 3
ports:
- '${MOSTRO_RELAY_LOCAL_PORT:-7000}:8080'
volumes:
- nostr_relay_data:/usr/src/app/db
- type: bind
source: ./config/relay/config.toml
target: /usr/src/app/config.toml
read_only: true
volumes:
nostr_relay_data:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was just moved, not really changed from what it previously was

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jgmontoya, understood. Thank you for the clarification.


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'
Copy link
Contributor

@coderabbitai coderabbitai bot Nov 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Docker volume configuration needs adjustment for LND certificates

The path /config/lnd/tls.cert in settings.docker.toml is not properly mounted in the Docker setup. While there is a volume mount for ./config:/config, there's no evidence of LND certificate files being copied or mounted in the correct location. This could lead to runtime errors.

  • The compose.yml mounts ./config:/config but doesn't ensure LND directory structure
  • start.sh only handles settings.toml and mostro.db
  • No setup for LND certificates in Dockerfile or start script

Required changes:

  • Create LND directory structure in start.sh
  • Document LND certificate requirements in DOCKER.md
  • Consider adding volume mount specifically for LND certificates
🔗 Analysis chain

Verify Docker volume mounting for LND certificates

The paths have been standardized to use /config/lnd/ directory. This change appears to be part of the Docker compose unification effort.

Let's verify the Docker compose configuration matches these paths:

Also applies to: 5-5

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify Docker volume mounting configuration for LND certificates
# Expected: Docker compose file should mount these paths correctly

# Check Docker compose files for volume mounting
rg -l "config/lnd" docker/

Length of output: 212


Script:

#!/bin/bash
# Let's check the actual volume mounting configuration in compose.yml
cat docker/compose.yml

Length of output: 544


Script:

#!/bin/bash
# Let's check the Dockerfile to see how the config directory is handled
cat docker/Dockerfile

# Also check the start script which might handle the LND cert setup
cat docker/start.sh

Length of output: 1745

Copy link
Contributor Author

@jgmontoya jgmontoya Nov 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the cert and macaroon files are copied in the docker-build Makefile target, the lnd directory is also created there

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jgmontoya, thank you for clarifying. I see now that the cert and macaroon files are copied in the docker-up Makefile target. My apologies for the oversight.


✏️ Learnings added
Learnt from: jgmontoya
PR: MostroP2P/mostro#392
File: docker/settings.docker.toml:3-3
Timestamp: 2024-11-11T11:32:14.343Z
Learning: In the `Makefile`, the `docker-up` target handles copying the LND certificate and macaroon files to the Docker configuration directory.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

# 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.

Loading