diff --git a/.cargo/config.toml b/.cargo/config.toml index 9e1afd01..9fee0622 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,5 +1,5 @@ [alias] -build-backend="build -p backend --features swagger --no-default-features" +build-backend="build -p bob-management --features swagger --no-default-features" build-frontend="build -p frontend --no-default-features" -run-backend="run -p backend --features swagger --no-default-features" +run-backend="run -p bob-management --features swagger --no-default-features" diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..742903cf --- /dev/null +++ b/.dockerignore @@ -0,0 +1,11 @@ +.env* +.DS_Store + +target/ +frontend/node_modules +frontend/dist + +npm-debug.log* +yarn-debug.log* +yarn-error.log* +build.log diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b5b2afd..2d2bf5af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,3 +8,4 @@ Bob Management GUI changelog - Initial project structure, backend only (#9) - Initial project stricture, frontend (#10) +- Dockerfile and Docker-Compose to simplify deployment (#5) diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 5d939ae1..ecb7a738 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "backend" +name = "bob-management" description = "Bob Management GUI: Backend" publish = false keywords = [ "BOB", "Management", "GUI" ] diff --git a/backend/src/main.rs b/backend/src/main.rs index 43b5847a..5ec70d1c 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -1,7 +1,7 @@ #![allow(clippy::multiple_crate_versions)] use axum::Router; -use backend::{ +use bob_management::{ config::ConfigExt, prelude::*, root, @@ -39,7 +39,7 @@ async fn main() -> Result<(), AppError> { let app = router(cors); #[cfg(all(feature = "swagger", debug_assertions))] - let app = app.merge(backend::openapi_doc()); + let app = app.merge(bob_management::openapi_doc()); axum::Server::bind(&addr) .serve(app.into_make_service()) @@ -82,7 +82,7 @@ fn router(cors: CorsLayer) -> Router { #[cfg(test)] mod tests { #![allow(clippy::expect_used)] - use backend::services::api_router_v1; + use bob_management::services::api_router_v1; #[test] fn register_routes() { diff --git a/config.yaml b/config.yaml index 5c7cc0cb..db7be989 100644 --- a/config.yaml +++ b/config.yaml @@ -1,3 +1,3 @@ -address: 0.0.0.0:7000 +address: 0.0.0.0:9000 logger: trace-level: INFO diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 00000000..17fa10a1 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,21 @@ +--- +version: "3.8" +services: + bob-gui: + build: + context: ./ + dockerfile: dockerfiles/alpine/Dockerfile + volumes: + - "./config.yaml:/config.yaml" + networks: + bobnet: + ipv4_address: 192.168.17.11 + ports: + - "9000:9000" + command: "--config-file /config.yaml" +networks: + bobnet: + driver: bridge + ipam: + config: + - subnet: 192.168.17.0/24 diff --git a/dockerfiles/alpine/Dockerfile b/dockerfiles/alpine/Dockerfile new file mode 100644 index 00000000..8e7a0e6b --- /dev/null +++ b/dockerfiles/alpine/Dockerfile @@ -0,0 +1,76 @@ +FROM rust:1.72 as backend + +ARG GIT_HASH_VAR +ENV BOBGUI_GIT_HASH $GIT_HASH_VAR +ARG BRANCH_TAG_VAR +ENV BOBGUI_BUILD_BRANCH_TAG $BRANCH_TAG_VAR + +ENV HOME=/home/root +WORKDIR $HOME/app +# rust toolchain +ARG RUST_TC_VER=stable +ARG BUILD_TARGET=x86_64-unknown-linux-musl +ARG BUILD_PROFILE=release-lto + +RUN apt-get update \ + && apt-get install -y --no-install-recommends musl-tools \ + && rustup install $RUST_TC_VER \ + && rustup default $RUST_TC_VER \ + && rustup target add $BUILD_TARGET + +# estimate build directory +RUN echo "$(case "$BUILD_PROFILE" in\ + ("dev") echo "debug";;\ + ("test") echo "debug";;\ + ("bench") echo "release";;\ + (*) echo "$BUILD_PROFILE";;\ + esac)" >> ./build_profile_dir + +RUN mkdir -p backend/src frontend cli/src +RUN mkdir target +COPY Cargo.toml Cargo.toml +COPY cli/Cargo.toml cli/Cargo.toml +COPY backend/Cargo.toml backend/Cargo.toml +COPY frontend/Cargo.toml frontend/Cargo.toml +COPY .cargo .cargo +RUN echo "fn main() {println!(\"if you see this, the build broke\")}" > backend/src/lib.rs \ + && echo "fn main() {println!(\"if you see this, the build broke\")}" > backend/src/main.rs \ + && echo "fn main() {println!(\"if you see this, the build broke\")}" > frontend/build.rs \ + && echo "fn main() {println!(\"if you see this, the build broke\")}" > cli/src/lib.rs \ + && echo "fn main() {println!(\"if you see this, the build broke\")}" > build.rs \ + && cargo build-backend --profile=$BUILD_PROFILE --target=$BUILD_TARGET + +COPY . ./ + +RUN cargo build-backend --profile=$BUILD_PROFILE --target=$BUILD_TARGET \ + && mkdir /build_output \ + && cp -f target/$BUILD_TARGET/$(cat ./build_profile_dir)/bob-management /build_output/bob-management + +FROM node:20.6 as frontend + +COPY ./frontend ./frontend + +RUN cd frontend && yarn && yarn build && mkdir /build_output && cp -r ./frontend /build_output/frontend + +FROM alpine:3.18 +ARG APP=/home/bob-management +ENV TZ=Etc/UTC \ + APP_USER=bobm +ENV PATH="$PATH:${APP}" +RUN addgroup -S $APP_USER \ + && adduser -S -g $APP_USER $APP_USER \ + && apk update \ + && apk add --no-cache ca-certificates tzdata \ + && rm -rf /var/cache/apk/* + +EXPOSE 9000 + +COPY --from=backend --chown=$APP_USER:$APP_USER /build_output/bob-management ${APP}/bob-management +COPY --from=frontend --chown=$APP_USER:$APP_USER /build_output/frontend ${APP}/frontend + +USER $APP_USER +WORKDIR ${APP} + +ENTRYPOINT ["./bob-management"] +CMD ["--default"] +