From 96c3c3d1011f5976e1e5d3b2076f33028a93b9ef Mon Sep 17 00:00:00 2001 From: "Javier G. Montoya S." Date: Sat, 19 Oct 2024 14:04:27 -0300 Subject: [PATCH] fix(docker): add missing dependency (#374) also improve final image size and security --- docker/DOCKER.md | 8 ++++++-- docker/Dockerfile | 35 ++++++++++++++++++++--------------- docker/compose.yml | 4 +++- docker/start.sh | 2 +- 4 files changed, 30 insertions(+), 19 deletions(-) diff --git a/docker/DOCKER.md b/docker/DOCKER.md index 7e370161..c7ac9ca5 100644 --- a/docker/DOCKER.md +++ b/docker/DOCKER.md @@ -13,10 +13,14 @@ The `compose.yml` file is configured as follows: ```yaml services: mostro: - build: . + build: + context: .. + dockerfile: docker/Dockerfile volumes: - - ~/mostro:/config # settings.toml and mostro.db + - ./config:/config # settings.toml and mostro.db - ~/.polar/networks/1/volumes/lnd:/lnd # LND data + platform: linux/amd64 + ``` ## Building and Running the Docker Container diff --git a/docker/Dockerfile b/docker/Dockerfile index 23618139..0f8e1bd7 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,32 +1,37 @@ # Build stage -FROM rust:latest as builder - -# Install dependencies -RUN apt-get update && \ - apt-get install -y cmake build-essential libsqlite3-dev pkg-config libssl-dev - -# Clone repository -RUN git clone https://github.com/MostroP2P/mostro.git /mostro +FROM rust:1.81 AS builder # Set working directory WORKDIR /mostro +# 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 rust:slim-bookworm +FROM debian:bookworm-slim -# Copy builded binary from build stage +# Copy built binary from build stage COPY --from=builder /mostro/target/release/mostrod /usr/local/bin/mostrod +WORKDIR /mostro + # Copy settings and empty database -COPY ./settings.docker.toml /mostro/settings.docker.toml -COPY ./empty.mostro.db /mostro/empty.mostro.db +COPY --chown=mostrouser:mostrouser ./docker/settings.docker.toml ./docker/empty.mostro.db ./ # Copy start script -COPY start.sh /mostro/start.sh -RUN chmod +x /mostro/start.sh +COPY --chown=mostrouser:mostrouser ./docker/start.sh ./start.sh +RUN chmod +x ./start.sh + +# Add a non-root user and switch to it +RUN useradd -m mostrouser +USER mostrouser # Start mostro (copy settings and database if it's not created yet) -CMD ["/mostro/start.sh"] +ENTRYPOINT ["./start.sh"] diff --git a/docker/compose.yml b/docker/compose.yml index c0e04faa..fd827d49 100644 --- a/docker/compose.yml +++ b/docker/compose.yml @@ -1,6 +1,8 @@ services: mostro: - build: . + build: + context: .. + dockerfile: docker/Dockerfile volumes: - ./config:/config # settings.toml and mostro.db - ~/.polar/networks/1/volumes/lnd:/lnd # LND data diff --git a/docker/start.sh b/docker/start.sh index 459d2def..6aedb576 100644 --- a/docker/start.sh +++ b/docker/start.sh @@ -13,4 +13,4 @@ if [ ! -f /config/mostro.db ]; then fi # Run application -/usr/local/bin/mostrod -d /config \ No newline at end of file +/usr/local/bin/mostrod -d /config