build(docker): add Dockerfile

This commit is contained in:
Matthieu Bessat 2024-12-03 23:58:20 +01:00
parent 0243535469
commit 23f12904cc
7 changed files with 77 additions and 12 deletions

7
.dockerignore Normal file
View file

@ -0,0 +1,7 @@
target
.env
tmp
Dockerfile
.dockerignore
justfile
config.toml

View file

@ -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"

37
Dockerfile Normal file
View file

@ -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"]

10
init_db.sh Executable file
View file

@ -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

View file

@ -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

View file

@ -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";

View file

@ -50,9 +50,10 @@ pub async fn get_kernel_context(start_config: StartKernelConfig) -> Result<Kerne
let config: Config = get_config(config_path)
.expect("Cannot get config.");
dotenvy::dotenv().context("loading .env")?;
let _ = dotenvy::dotenv();
let secrets = AppSecrets {
jwt_secret: env::var("APP_JWT_SECRET").context("Expecting APP_JWT_SECRET env var.")?
jwt_secret: env::var("APP_JWT_SECRET")
.context("Expected APP_JWT_SECRET environment variable to exists.")?
};
Ok(KernelContext {