From 53f415714222358b9799a45fc634d8718ac34e79 Mon Sep 17 00:00:00 2001 From: Sebastien Iooss Date: Thu, 11 Jan 2024 18:41:03 +0100 Subject: [PATCH] feat: add working docker --- .dockerignore | 17 +++++++++++++---- Dockerfile | 15 +++++++-------- Readme.md | 6 +++++- cli.Dockerfile | 13 +++++++++++++ src/main.rs | 4 ++-- 5 files changed, 40 insertions(+), 15 deletions(-) create mode 100644 cli.Dockerfile diff --git a/.dockerignore b/.dockerignore index 7ec286c..36c1ea3 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,6 +1,15 @@ -target -old -.cargo -tarpaulin-report.html millennium.log* +.cargo .env +.git +.github +.gitignore +deny.toml +examples +old +Readme.md +rustfmt.toml +target +tarpaulin-report.html +tests +Dockerfile diff --git a/Dockerfile b/Dockerfile index d418027..8674acd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,13 @@ FROM rust:1.75.0-buster as builder - -RUN rustup target add x86_64-unknown-linux-musl -RUN apt update && apt install -y clang - WORKDIR /app -COPY . /app/ - +COPY Cargo.lock /app/ +COPY Cargo.toml /app/ +COPY src /app/src/ +COPY front /app/front/ +COPY .sqlx /app/.sqlx RUN cargo build --release -FROM scratch -USER 1000 +FROM ubuntu:24.04 +WORKDIR /app COPY --from=builder /app/target/release/millennium_falcon . ENTRYPOINT [ "./millennium_falcon" ] diff --git a/Readme.md b/Readme.md index a17a7da..cf81fde 100644 --- a/Readme.md +++ b/Readme.md @@ -4,7 +4,11 @@ Solution of the [developer-test](https://github.com/lioncowlionant/developer-tes With a recent version of rust (tested with 1.75.0), you can build the project with `cargo build --release`. Then you can run the cli with `./target/release/give-me-the-odds examples/millennium-falcon.json examples/example2/empire.json` and the webserver with `./target/release/millennium_falcon examples/millennium-falcon.json`. -Note that when starting, the webserver will create a file `millennium.log.{date}` containing the log of the server. stdio will be pretty silent if everything goes well. The server will be listening on `127.0.0.1:8000`. +Note that when starting, the webserver will create a folder `logs` containing a file `millennium.log.{date}` with the log of the server. stdio will be pretty silent if everything goes well. The server will be listening on `127.0.0.1:8000`. + +It is also possible to start the server with docker by running `docker build -t millennium-falcon .` then `docker run -it --rm -v ./logs:/app/logs -v ./examples:/app/examples -p 0.0.0.0:8000:8000 millennium-falcon examples/millennium-falcon.json` + +and the cli by running `docker build -t give-me-the-odds -f cli.Dockerfile .` then `docker run -it --rm -v ./examples:/app/examples give-me-the-odds examples/millennium-falcon.json examples/example1/empire.json` ## Architecture diff --git a/cli.Dockerfile b/cli.Dockerfile new file mode 100644 index 0000000..8f7bf4c --- /dev/null +++ b/cli.Dockerfile @@ -0,0 +1,13 @@ +FROM rust:1.75.0-buster as builder +WORKDIR /app +COPY Cargo.lock /app/ +COPY Cargo.toml /app/ +COPY src /app/src/ +COPY front /app/front/ +COPY .sqlx /app/.sqlx +RUN cargo build --release + +FROM ubuntu:24.04 +WORKDIR /app +COPY --from=builder /app/target/release/give-me-the-odds . +ENTRYPOINT [ "./give-me-the-odds" ] diff --git a/src/main.rs b/src/main.rs index cb3df7f..2f79ad4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,11 +16,11 @@ pub async fn setup_webserver(address: &str) -> Result { #[actix_web::main] async fn main() -> std::io::Result<()> { - let file_appender = tracing_appender::rolling::daily(".", "millennium.log"); + let file_appender = tracing_appender::rolling::daily("logs", "millennium.log"); let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender); tracing_subscriber::fmt().with_writer(non_blocking).init(); - match setup_webserver("127.0.0.1:8000").await { + match setup_webserver("0.0.0.0:8000").await { Ok(server) => server.await, Err(e) => { let e = e.context("unable to start the server");