Skip to content

Commit

Permalink
5 create dockerfile and docker compose to simplify deployment (#12)
Browse files Browse the repository at this point in the history
* add dockerfile and docker-compose

* fix swagger-ui

* nodejs fix

* some changes

* review fixes

* update changelog

* add git args

* update dockerfile

* post review fixes

* change app dir
  • Loading branch information
archeoss authored Nov 15, 2023
1 parent 6ab8e31 commit f2f5af5
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -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"

11 changes: 11 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.env*
.DS_Store

target/
frontend/node_modules
frontend/dist

npm-debug.log*
yarn-debug.log*
yarn-error.log*
build.log
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion backend/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "backend"
name = "bob-management"
description = "Bob Management GUI: Backend"
publish = false
keywords = [ "BOB", "Management", "GUI" ]
Expand Down
6 changes: 3 additions & 3 deletions backend/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(clippy::multiple_crate_versions)]

use axum::Router;
use backend::{
use bob_management::{
config::ConfigExt,
prelude::*,
root,
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
address: 0.0.0.0:7000
address: 0.0.0.0:9000
logger:
trace-level: INFO
21 changes: 21 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -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
76 changes: 76 additions & 0 deletions dockerfiles/alpine/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]

0 comments on commit f2f5af5

Please sign in to comment.