From 23f12904ccce3abebd3e6f04749853c3f857bc6d Mon Sep 17 00:00:00 2001 From: Matthieu Bessat Date: Tue, 3 Dec 2024 23:58:20 +0100 Subject: [PATCH] build(docker): add Dockerfile --- .dockerignore | 7 +++++++ Cargo.toml | 10 ++++++---- Dockerfile | 37 +++++++++++++++++++++++++++++++++++++ init_db.sh | 10 ++++++++++ justfile | 18 +++++++++++++----- lib/kernel/src/consts.rs | 2 +- lib/kernel/src/context.rs | 5 +++-- 7 files changed, 77 insertions(+), 12 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100755 init_db.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..bff112f --- /dev/null +++ b/.dockerignore @@ -0,0 +1,7 @@ +target +.env +tmp +Dockerfile +.dockerignore +justfile +config.toml diff --git a/Cargo.toml b/Cargo.toml index edef17d..70a7822 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,11 @@ -cargo-features = ["codegen-backend"] +# cargo-features = ["codegen-backend"] -[profile.dev] -codegen-backend = "cranelift" +# [profile.dev] +# codegen-backend = "cranelift" +# # END OF [workspace] +resolver = "2" members = [ "lib/kernel", "lib/utils", @@ -26,7 +28,7 @@ url = "2.5.3" argh = "0.1" # Async -tokio = { version = "1.40.0", features = ["rt-multi-thread"] } +tokio = { version = "1.40.0", features = ["rt-multi-thread", "macros"] } # Logging log = "0.4" diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a5fd9d9 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,37 @@ +FROM rust:1.83-alpine3.20 AS builder + +WORKDIR /usr/src/minauthator +COPY . . + +RUN apk add musl-dev + +RUN cargo install --bin minauthator-admin --locked --path lib/admin_cli +RUN cargo install --bin minauthator-server --locked --path lib/http_server + +FROM alpine:3.20 AS base + +RUN apk add sqlite +COPY --from=builder /usr/local/cargo/bin/minauthator-server /usr/local/bin/minauthator-server +COPY --from=builder /usr/local/cargo/bin/minauthator-admin /usr/local/bin/minauthator-admin +RUN mkdir -p \ + /usr/local/src/minauthator/migrations \ + /usr/local/lib/minauthator/assets \ + /var/lib/minauthator \ + /etc/minauthator +COPY --from=builder /usr/src/minauthator/migrations/all.sql /usr/local/src/minauthator/migrations +COPY --from=builder /usr/src/minauthator/init_db.sh /usr/local/bin/minauthator_init_db.sh +COPY --from=builder /usr/src/minauthator/assets /usr/local/lib/minauthator/assets + +RUN addgroup -g 1000 app && \ + adduser -S -u 1000 -G app -s /bin/sh app && \ + chown -R app:app /var/lib/minauthator && \ + chmod -R u=rwx,g=rwx,o= /var/lib/minauthator + +USER app:app +ENV RUST_LOG=info +ENV RUST_BACKTRACE=1 +ENV APP_JWT_SECRET="DummyAppSecret20241029" + +CMD ["minauthator-server", "--listen-host", "0.0.0.0", "--listen-port", "8080"] + + diff --git a/init_db.sh b/init_db.sh new file mode 100755 index 0000000..eee8c9f --- /dev/null +++ b/init_db.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +DEFAULT_DB_PATH="/var/lib/minauthator/minauthator.db" +DEFAULT_MIGRATION_PATH="/usr/local/src/minauthator/migrations/all.sql" + +DB_PATH="${DB_PATH:-$DEFAULT_DB_PATH}" +MIGRATION_PATH="${MIGRATION_PATH:-$DEFAULT_MIGRATION_PATH}" + +sqlite3 $DB_PATH < $MIGRATION_PATH + diff --git a/justfile b/justfile index fc10ea4..de21365 100644 --- a/justfile +++ b/justfile @@ -11,14 +11,22 @@ server: admin: cargo run --bin minauthator-admin -- $CONTEXT_ARGS -docker-run: - docker run -p 3085:8080 -v ./tmp/docker/config:/etc/minauthator -v ./tmp/docker/db:/var/lib/minauthator minauthator +docker-build: + docker build -t lefuturiste/minauthator . docker-init-db: - docker run -v ./tmp/docker/config:/etc/minauthator -v ./tmp/docker/db:/var/lib/minauthator minauthator /usr/local/bin/minauthator_init_db.sh + docker run \ + -v ./tmp/docker/config:/etc/minauthator \ + -v minauthator-db:/var/lib/minauthator \ + lefuturiste/minauthator \ + /usr/local/bin/minauthator_init_db.sh -docker-build: - docker build -t minauthator . +docker-run: + docker run \ + -p 127.0.0.1:3085:8080 \ + -v ./tmp/docker/config:/etc/minauthator \ + -v minauthator-db:/var/lib/minauthator \ + lefuturiste/minauthator init-db: sqlite3 -echo tmp/dbs/minauthator.db < migrations/all.sql diff --git a/lib/kernel/src/consts.rs b/lib/kernel/src/consts.rs index bd3bef1..31fca20 100644 --- a/lib/kernel/src/consts.rs +++ b/lib/kernel/src/consts.rs @@ -1,4 +1,4 @@ pub const DEFAULT_DB_PATH: &str = "/var/lib/minauthator/minauthator.db"; pub const DEFAULT_ASSETS_PATH: &str = "/usr/local/lib/minauthator/assets"; -pub const DEFAULT_CONFIG_PATH: &str = "/etc/minauthator/config.yaml"; +pub const DEFAULT_CONFIG_PATH: &str = "/etc/minauthator/config.toml"; diff --git a/lib/kernel/src/context.rs b/lib/kernel/src/context.rs index bbbf0df..0c6e3b1 100644 --- a/lib/kernel/src/context.rs +++ b/lib/kernel/src/context.rs @@ -50,9 +50,10 @@ pub async fn get_kernel_context(start_config: StartKernelConfig) -> Result