From 7df00050e3db9c65986ea12fddbaf464675cd86d Mon Sep 17 00:00:00 2001 From: Simeon Romanov Date: Sun, 3 Sep 2023 12:36:06 +0300 Subject: [PATCH 01/10] add dockerfile and docker-compose --- .dockerignore | 11 +++++ docker-compose.yaml | 27 +++++++++++ dockerfiles/alpine/Dockerfile | 73 ++++++++++++++++++++++++++++ dockerfiles/alpine/Dockerfile.dev | 79 +++++++++++++++++++++++++++++++ 4 files changed, 190 insertions(+) create mode 100644 .dockerignore create mode 100644 docker-compose.yaml create mode 100644 dockerfiles/alpine/Dockerfile create mode 100644 dockerfiles/alpine/Dockerfile.dev 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/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 00000000..443135ef --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,27 @@ +--- +version: "3.8" +services: + bob-gui: + build: + context: ./ + dockerfile: dockerfiles/alpine/Dockerfile + networks: + bobnet: + ipv4_address: 192.168.17.11 + ports: + - "9000:9000" + bob-gui-dev: + build: + context: ./ + dockerfile: dockerfiles/alpine/Dockerfile.dev + networks: + bobnet: + ipv4_address: 192.168.17.12 + ports: + - "8000:9000" +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..5b124f19 --- /dev/null +++ b/dockerfiles/alpine/Dockerfile @@ -0,0 +1,73 @@ +FROM rust:1.72 as builder + +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 + +# import the Nodesource GPG key +RUN apt-get update \ + && apt-get install -y ca-certificates curl gnupg \ + && mkdir -p /etc/apt/keyrings \ + && curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg + +# Create deb repository +ENV NODE_MAJOR=20 +RUN echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list + +# Install yarn keys +RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \ + && echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list + +RUN apt-get update \ + && apt-get install -y --no-install-recommends musl-tools yarn nodejs \ + && rustup install $RUST_TC_VER \ + && rustup default $RUST_TC_VER \ + && rustup target add $BUILD_TARGET + + +# RUN USER=root cargo new --bin bob-management +COPY ./Cargo.toml ./Cargo.toml +COPY ./build.rs ./build.rs +COPY ./cli ./cli +COPY ./backend ./backend +COPY ./frontend ./frontend +RUN cargo build --profile=$BUILD_PROFILE --target=$BUILD_TARGET --bin bob_management +RUN find . -name '*.rs' -delete + +ADD . ./ + +RUN rm ./target/$BUILD_TARGET/$BUILD_PROFILE/deps/bob_management* + +RUN --mount=type=cache,target=/usr/local/cargo/registry \ + --mount=type=cache,target=/home/root/app/target \ + cargo build --profile=$BUILD_PROFILE --target=$BUILD_TARGET --bin bob_management \ + && mkdir /build_output \ + && mkdir /build_output/backend \ + && mkdir /build_output/frontend \ + && cp -f target/$BUILD_TARGET/$BUILD_PROFILE/bob_management /build_output/backend/bob_management \ + && cp -r -f target/frontend/dist /build_output/frontend/dist + +FROM alpine:3.18 +ARG APP=/usr/src/app +ENV TZ=Etc/UTC \ + APP_USER=appuser + +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=builder --chown=$APP_USER:$APP_USER /build_output/backend ${APP}/backend +COPY --from=builder --chown=$APP_USER:$APP_USER /build_output/frontend ${APP}/frontend + +USER $APP_USER +WORKDIR ${APP} + +CMD ["./backend/bob_management", "--default"] + diff --git a/dockerfiles/alpine/Dockerfile.dev b/dockerfiles/alpine/Dockerfile.dev new file mode 100644 index 00000000..d6179ed0 --- /dev/null +++ b/dockerfiles/alpine/Dockerfile.dev @@ -0,0 +1,79 @@ +FROM rust:1.72 as builder + +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=dev + +# import the Nodesource GPG key +RUN apt-get update \ + && apt-get install -y ca-certificates curl gnupg \ + && mkdir -p /etc/apt/keyrings \ + && curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg + +# Create deb repository +ENV NODE_MAJOR=20 +RUN echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list + +# Install yarn keys +RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \ + && echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list + +RUN apt-get update \ + && apt-get install -y --no-install-recommends musl-tools yarn nodejs \ + && rustup install $RUST_TC_VER \ + && rustup default $RUST_TC_VER \ + && rustup target add $BUILD_TARGET + + +# RUN USER=root cargo new --bin bob-management +COPY ./Cargo.toml ./Cargo.toml +COPY ./build.rs ./build.rs +COPY ./cli ./cli +COPY ./backend ./backend +COPY ./frontend ./frontend +RUN cargo build --profile=$BUILD_PROFILE --target=$BUILD_TARGET --bin bob_management +RUN find . -name '*.rs' -delete + +ADD . ./ + +RUN rm ./target/$BUILD_TARGET/debug/deps/bob_management* + +RUN --mount=type=cache,target=/usr/local/cargo/registry \ + --mount=type=cache,target=/home/root/app/target \ + cargo build --profile=$BUILD_PROFILE --target=$BUILD_TARGET --bin bob_management \ + && mkdir /build_output \ + && mkdir /build_output/backend \ + && mkdir /build_output/frontend \ + && cp -f target/$BUILD_TARGET/debug/bob_management /build_output/backend/bob_management \ + # FIXME: Swagger-Ui doesn't work in docker for some reason - RapiDoc and Redoc are OK + # && mkdir /build_output/target/ \ + # && mkdir /build_output/target/debug \ + # && find target/$BUILD_TARGET/debug/build -maxdepth 1 -name "utoipa-swagger-ui*" \ + # && mv -v $(find target/$BUILD_TARGET/debug/build -maxdepth 1 -name "utoipa-swagger-ui*") /build_output/build \ + && cp -r -f target/frontend/dist /build_output/frontend/dist + +FROM alpine:3.18 +ARG APP=/usr/src/app +ENV TZ=Etc/UTC \ + APP_USER=appuser + +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=builder --chown=$APP_USER:$APP_USER /build_output/backend ${APP}/backend +COPY --from=builder --chown=$APP_USER:$APP_USER /build_output/frontend ${APP}/frontend +# COPY --from=builder --chown=$APP_USER:$APP_USER /build_output/build ${APP}/build + +USER $APP_USER +WORKDIR ${APP} + +CMD ["./backend/bob_management", "--default"] + From 7f8dc82fdc0cd8ebda66dcbce975843e4874c6e3 Mon Sep 17 00:00:00 2001 From: Simeon Romanov Date: Mon, 4 Sep 2023 14:48:07 +0300 Subject: [PATCH 02/10] fix swagger-ui --- dockerfiles/alpine/Dockerfile.dev | 7 ------- 1 file changed, 7 deletions(-) diff --git a/dockerfiles/alpine/Dockerfile.dev b/dockerfiles/alpine/Dockerfile.dev index d6179ed0..b593da33 100644 --- a/dockerfiles/alpine/Dockerfile.dev +++ b/dockerfiles/alpine/Dockerfile.dev @@ -28,7 +28,6 @@ RUN apt-get update \ && rustup target add $BUILD_TARGET -# RUN USER=root cargo new --bin bob-management COPY ./Cargo.toml ./Cargo.toml COPY ./build.rs ./build.rs COPY ./cli ./cli @@ -48,11 +47,6 @@ RUN --mount=type=cache,target=/usr/local/cargo/registry \ && mkdir /build_output/backend \ && mkdir /build_output/frontend \ && cp -f target/$BUILD_TARGET/debug/bob_management /build_output/backend/bob_management \ - # FIXME: Swagger-Ui doesn't work in docker for some reason - RapiDoc and Redoc are OK - # && mkdir /build_output/target/ \ - # && mkdir /build_output/target/debug \ - # && find target/$BUILD_TARGET/debug/build -maxdepth 1 -name "utoipa-swagger-ui*" \ - # && mv -v $(find target/$BUILD_TARGET/debug/build -maxdepth 1 -name "utoipa-swagger-ui*") /build_output/build \ && cp -r -f target/frontend/dist /build_output/frontend/dist FROM alpine:3.18 @@ -70,7 +64,6 @@ EXPOSE 9000 COPY --from=builder --chown=$APP_USER:$APP_USER /build_output/backend ${APP}/backend COPY --from=builder --chown=$APP_USER:$APP_USER /build_output/frontend ${APP}/frontend -# COPY --from=builder --chown=$APP_USER:$APP_USER /build_output/build ${APP}/build USER $APP_USER WORKDIR ${APP} From 165a483d68fd51df56cb648a16d7d19969e680e0 Mon Sep 17 00:00:00 2001 From: Simeon Romanov Date: Thu, 7 Sep 2023 12:11:01 +0300 Subject: [PATCH 03/10] nodejs fix --- dockerfiles/alpine/Dockerfile | 3 ++- dockerfiles/alpine/Dockerfile.dev | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/dockerfiles/alpine/Dockerfile b/dockerfiles/alpine/Dockerfile index 5b124f19..5479d525 100644 --- a/dockerfiles/alpine/Dockerfile +++ b/dockerfiles/alpine/Dockerfile @@ -14,7 +14,8 @@ RUN apt-get update \ && curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg # Create deb repository -ENV NODE_MAJOR=20 +# FIXME: Replace it with Nodejs 20.x when it will be fixed +ENV NODE_MAJOR=18 RUN echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list # Install yarn keys diff --git a/dockerfiles/alpine/Dockerfile.dev b/dockerfiles/alpine/Dockerfile.dev index b593da33..61ed177a 100644 --- a/dockerfiles/alpine/Dockerfile.dev +++ b/dockerfiles/alpine/Dockerfile.dev @@ -14,7 +14,8 @@ RUN apt-get update \ && curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg # Create deb repository -ENV NODE_MAJOR=20 +# FIXME: Replace it with Nodejs 20.x when it will be fixed +ENV NODE_MAJOR=18 RUN echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list # Install yarn keys From 9589978ca4f98da059574aeeed84101224220e66 Mon Sep 17 00:00:00 2001 From: Simeon Romanov Date: Sun, 17 Sep 2023 13:01:05 +0300 Subject: [PATCH 04/10] some changes --- docker-compose.yaml | 1 + dockerfiles/alpine/Dockerfile | 45 ++++++++++++----------------------- 2 files changed, 16 insertions(+), 30 deletions(-) diff --git a/docker-compose.yaml b/docker-compose.yaml index 443135ef..90f3f967 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -10,6 +10,7 @@ services: ipv4_address: 192.168.17.11 ports: - "9000:9000" + command: "backend/backend --config-file config" bob-gui-dev: build: context: ./ diff --git a/dockerfiles/alpine/Dockerfile b/dockerfiles/alpine/Dockerfile index 5479d525..82b9ffa0 100644 --- a/dockerfiles/alpine/Dockerfile +++ b/dockerfiles/alpine/Dockerfile @@ -1,4 +1,4 @@ -FROM rust:1.72 as builder +FROM rust:1.72 as backend ENV HOME=/home/root WORKDIR $HOME/app @@ -7,23 +7,8 @@ ARG RUST_TC_VER=stable ARG BUILD_TARGET=x86_64-unknown-linux-musl ARG BUILD_PROFILE=release-lto -# import the Nodesource GPG key RUN apt-get update \ - && apt-get install -y ca-certificates curl gnupg \ - && mkdir -p /etc/apt/keyrings \ - && curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg - -# Create deb repository -# FIXME: Replace it with Nodejs 20.x when it will be fixed -ENV NODE_MAJOR=18 -RUN echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list - -# Install yarn keys -RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \ - && echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list - -RUN apt-get update \ - && apt-get install -y --no-install-recommends musl-tools yarn nodejs \ + && apt-get install -y --no-install-recommends musl-tools \ && rustup install $RUST_TC_VER \ && rustup default $RUST_TC_VER \ && rustup target add $BUILD_TARGET @@ -34,22 +19,22 @@ COPY ./Cargo.toml ./Cargo.toml COPY ./build.rs ./build.rs COPY ./cli ./cli COPY ./backend ./backend -COPY ./frontend ./frontend -RUN cargo build --profile=$BUILD_PROFILE --target=$BUILD_TARGET --bin bob_management -RUN find . -name '*.rs' -delete +COPY ./.cargo ./.cargo +RUN cargo backend --profile=$BUILD_PROFILE --target=$BUILD_TARGET ADD . ./ -RUN rm ./target/$BUILD_TARGET/$BUILD_PROFILE/deps/bob_management* - RUN --mount=type=cache,target=/usr/local/cargo/registry \ --mount=type=cache,target=/home/root/app/target \ - cargo build --profile=$BUILD_PROFILE --target=$BUILD_TARGET --bin bob_management \ + cargo backend --profile=$BUILD_PROFILE --target=$BUILD_TARGET \ && mkdir /build_output \ - && mkdir /build_output/backend \ - && mkdir /build_output/frontend \ - && cp -f target/$BUILD_TARGET/$BUILD_PROFILE/bob_management /build_output/backend/bob_management \ - && cp -r -f target/frontend/dist /build_output/frontend/dist + && cp -f target/$BUILD_TARGET/$BUILD_PROFILE/backend /build_output/backend + +FROM node:20.6 as frontend + +COPY ./frontend ./frontend + +RUN cd frontend && yarn && yarn build FROM alpine:3.18 ARG APP=/usr/src/app @@ -64,11 +49,11 @@ RUN addgroup -S $APP_USER \ EXPOSE 9000 -COPY --from=builder --chown=$APP_USER:$APP_USER /build_output/backend ${APP}/backend -COPY --from=builder --chown=$APP_USER:$APP_USER /build_output/frontend ${APP}/frontend +COPY --from=backend --chown=$APP_USER:$APP_USER /build_output/backend ${APP}/backend/backend +COPY --from=frontend --chown=$APP_USER:$APP_USER /frontend/dist ${APP}/frontend USER $APP_USER WORKDIR ${APP} -CMD ["./backend/bob_management", "--default"] +CMD ["./backend/backend"] From 2322695ffd8c635f7acfa4d9457b785fd28049f8 Mon Sep 17 00:00:00 2001 From: Simeon Romanov Date: Tue, 19 Sep 2023 15:46:33 +0300 Subject: [PATCH 05/10] review fixes --- config.yaml | 2 +- docker-compose.yaml | 13 ++---- dockerfiles/alpine/Dockerfile | 34 ++++++++------ dockerfiles/alpine/Dockerfile.dev | 73 ------------------------------- 4 files changed, 25 insertions(+), 97 deletions(-) delete mode 100644 dockerfiles/alpine/Dockerfile.dev 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 index 90f3f967..17fa10a1 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -5,21 +5,14 @@ services: build: context: ./ dockerfile: dockerfiles/alpine/Dockerfile + volumes: + - "./config.yaml:/config.yaml" networks: bobnet: ipv4_address: 192.168.17.11 ports: - "9000:9000" - command: "backend/backend --config-file config" - bob-gui-dev: - build: - context: ./ - dockerfile: dockerfiles/alpine/Dockerfile.dev - networks: - bobnet: - ipv4_address: 192.168.17.12 - ports: - - "8000:9000" + command: "--config-file /config.yaml" networks: bobnet: driver: bridge diff --git a/dockerfiles/alpine/Dockerfile b/dockerfiles/alpine/Dockerfile index 82b9ffa0..c18552ad 100644 --- a/dockerfiles/alpine/Dockerfile +++ b/dockerfiles/alpine/Dockerfile @@ -15,18 +15,26 @@ RUN apt-get update \ # RUN USER=root cargo new --bin bob-management -COPY ./Cargo.toml ./Cargo.toml -COPY ./build.rs ./build.rs -COPY ./cli ./cli -COPY ./backend ./backend -COPY ./.cargo ./.cargo -RUN cargo backend --profile=$BUILD_PROFILE --target=$BUILD_TARGET +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 -p bob_management --features backend --no-default-features --profile=$BUILD_PROFILE --target=$BUILD_TARGET ADD . ./ -RUN --mount=type=cache,target=/usr/local/cargo/registry \ - --mount=type=cache,target=/home/root/app/target \ - cargo backend --profile=$BUILD_PROFILE --target=$BUILD_TARGET \ +RUN \ + # --mount=type=cache,target=/usr/local/cargo/registry \ + # --mount=type=cache,target=/home/root/app/target \ + cargo build -p bob_management --features backend --no-default-features --profile=$BUILD_PROFILE --target=$BUILD_TARGET \ && mkdir /build_output \ && cp -f target/$BUILD_TARGET/$BUILD_PROFILE/backend /build_output/backend @@ -40,7 +48,7 @@ FROM alpine:3.18 ARG APP=/usr/src/app ENV TZ=Etc/UTC \ APP_USER=appuser - +ENV PATH="$PATH:${APP}" RUN addgroup -S $APP_USER \ && adduser -S -g $APP_USER $APP_USER \ && apk update \ @@ -49,11 +57,11 @@ RUN addgroup -S $APP_USER \ EXPOSE 9000 -COPY --from=backend --chown=$APP_USER:$APP_USER /build_output/backend ${APP}/backend/backend -COPY --from=frontend --chown=$APP_USER:$APP_USER /frontend/dist ${APP}/frontend +COPY --from=backend --chown=$APP_USER:$APP_USER /build_output/backend ${APP}/backend +COPY --from=frontend --chown=$APP_USER:$APP_USER /frontend/dist ${APP}/dist USER $APP_USER WORKDIR ${APP} -CMD ["./backend/backend"] +ENTRYPOINT ["./backend"] diff --git a/dockerfiles/alpine/Dockerfile.dev b/dockerfiles/alpine/Dockerfile.dev deleted file mode 100644 index 61ed177a..00000000 --- a/dockerfiles/alpine/Dockerfile.dev +++ /dev/null @@ -1,73 +0,0 @@ -FROM rust:1.72 as builder - -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=dev - -# import the Nodesource GPG key -RUN apt-get update \ - && apt-get install -y ca-certificates curl gnupg \ - && mkdir -p /etc/apt/keyrings \ - && curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg - -# Create deb repository -# FIXME: Replace it with Nodejs 20.x when it will be fixed -ENV NODE_MAJOR=18 -RUN echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list - -# Install yarn keys -RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \ - && echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list - -RUN apt-get update \ - && apt-get install -y --no-install-recommends musl-tools yarn nodejs \ - && rustup install $RUST_TC_VER \ - && rustup default $RUST_TC_VER \ - && rustup target add $BUILD_TARGET - - -COPY ./Cargo.toml ./Cargo.toml -COPY ./build.rs ./build.rs -COPY ./cli ./cli -COPY ./backend ./backend -COPY ./frontend ./frontend -RUN cargo build --profile=$BUILD_PROFILE --target=$BUILD_TARGET --bin bob_management -RUN find . -name '*.rs' -delete - -ADD . ./ - -RUN rm ./target/$BUILD_TARGET/debug/deps/bob_management* - -RUN --mount=type=cache,target=/usr/local/cargo/registry \ - --mount=type=cache,target=/home/root/app/target \ - cargo build --profile=$BUILD_PROFILE --target=$BUILD_TARGET --bin bob_management \ - && mkdir /build_output \ - && mkdir /build_output/backend \ - && mkdir /build_output/frontend \ - && cp -f target/$BUILD_TARGET/debug/bob_management /build_output/backend/bob_management \ - && cp -r -f target/frontend/dist /build_output/frontend/dist - -FROM alpine:3.18 -ARG APP=/usr/src/app -ENV TZ=Etc/UTC \ - APP_USER=appuser - -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=builder --chown=$APP_USER:$APP_USER /build_output/backend ${APP}/backend -COPY --from=builder --chown=$APP_USER:$APP_USER /build_output/frontend ${APP}/frontend - -USER $APP_USER -WORKDIR ${APP} - -CMD ["./backend/bob_management", "--default"] - From 1e02497894ebad9e507fa349abdf781cd4a3fda7 Mon Sep 17 00:00:00 2001 From: Simeon Romanov Date: Tue, 19 Sep 2023 19:15:31 +0300 Subject: [PATCH 06/10] update changelog --- CHANGELOG.md | 1 + dockerfiles/alpine/Dockerfile | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) 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/dockerfiles/alpine/Dockerfile b/dockerfiles/alpine/Dockerfile index c18552ad..f9dd1e65 100644 --- a/dockerfiles/alpine/Dockerfile +++ b/dockerfiles/alpine/Dockerfile @@ -27,14 +27,14 @@ RUN echo "fn main() {println!(\"if you see this, the build broke\")}" > backend/ && 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 -p bob_management --features backend --no-default-features --profile=$BUILD_PROFILE --target=$BUILD_TARGET + && cargo build-backend --profile=$BUILD_PROFILE --target=$BUILD_TARGET ADD . ./ RUN \ # --mount=type=cache,target=/usr/local/cargo/registry \ # --mount=type=cache,target=/home/root/app/target \ - cargo build -p bob_management --features backend --no-default-features --profile=$BUILD_PROFILE --target=$BUILD_TARGET \ + cargo build-backend --profile=$BUILD_PROFILE --target=$BUILD_TARGET \ && mkdir /build_output \ && cp -f target/$BUILD_TARGET/$BUILD_PROFILE/backend /build_output/backend From ef6b998aee4bb979f506733a46439cb9e30992e3 Mon Sep 17 00:00:00 2001 From: Simeon Romanov Date: Wed, 4 Oct 2023 00:59:07 +0300 Subject: [PATCH 07/10] add git args --- dockerfiles/alpine/Dockerfile | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/dockerfiles/alpine/Dockerfile b/dockerfiles/alpine/Dockerfile index f9dd1e65..625df5e2 100644 --- a/dockerfiles/alpine/Dockerfile +++ b/dockerfiles/alpine/Dockerfile @@ -1,5 +1,10 @@ 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 From 798b7dad5a066eac4bcbfd9441b252219df065cd Mon Sep 17 00:00:00 2001 From: Simeon Romanov Date: Fri, 6 Oct 2023 20:06:56 +0300 Subject: [PATCH 08/10] update dockerfile --- dockerfiles/alpine/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dockerfiles/alpine/Dockerfile b/dockerfiles/alpine/Dockerfile index 625df5e2..6a3e8de3 100644 --- a/dockerfiles/alpine/Dockerfile +++ b/dockerfiles/alpine/Dockerfile @@ -63,7 +63,7 @@ RUN addgroup -S $APP_USER \ EXPOSE 9000 COPY --from=backend --chown=$APP_USER:$APP_USER /build_output/backend ${APP}/backend -COPY --from=frontend --chown=$APP_USER:$APP_USER /frontend/dist ${APP}/dist +COPY --from=frontend --chown=$APP_USER:$APP_USER /frontend/frontend ${APP}/frontend USER $APP_USER WORKDIR ${APP} From 7d99087e428c736cc6b309dd217b503504ead536 Mon Sep 17 00:00:00 2001 From: Simeon Romanov Date: Mon, 13 Nov 2023 15:19:16 +0300 Subject: [PATCH 09/10] post review fixes --- .cargo/config.toml | 4 ++-- backend/Cargo.toml | 2 +- backend/src/main.rs | 6 +++--- dockerfiles/alpine/Dockerfile | 28 ++++++++++++++++------------ 4 files changed, 22 insertions(+), 18 deletions(-) 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/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/dockerfiles/alpine/Dockerfile b/dockerfiles/alpine/Dockerfile index 6a3e8de3..95451a9b 100644 --- a/dockerfiles/alpine/Dockerfile +++ b/dockerfiles/alpine/Dockerfile @@ -18,8 +18,14 @@ RUN apt-get update \ && 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 USER=root cargo new --bin bob-management RUN mkdir -p backend/src frontend cli/src RUN mkdir target COPY Cargo.toml Cargo.toml @@ -34,25 +40,22 @@ RUN echo "fn main() {println!(\"if you see this, the build broke\")}" > backend/ && echo "fn main() {println!(\"if you see this, the build broke\")}" > build.rs \ && cargo build-backend --profile=$BUILD_PROFILE --target=$BUILD_TARGET -ADD . ./ +COPY . ./ -RUN \ - # --mount=type=cache,target=/usr/local/cargo/registry \ - # --mount=type=cache,target=/home/root/app/target \ - cargo build-backend --profile=$BUILD_PROFILE --target=$BUILD_TARGET \ +RUN cargo build-backend --profile=$BUILD_PROFILE --target=$BUILD_TARGET \ && mkdir /build_output \ - && cp -f target/$BUILD_TARGET/$BUILD_PROFILE/backend /build_output/backend + && 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 +RUN cd frontend && yarn && yarn build && mkdir /build_output && cp -r ./frontend /build_output/frontend FROM alpine:3.18 ARG APP=/usr/src/app ENV TZ=Etc/UTC \ - APP_USER=appuser + APP_USER=bobm ENV PATH="$PATH:${APP}" RUN addgroup -S $APP_USER \ && adduser -S -g $APP_USER $APP_USER \ @@ -62,11 +65,12 @@ RUN addgroup -S $APP_USER \ EXPOSE 9000 -COPY --from=backend --chown=$APP_USER:$APP_USER /build_output/backend ${APP}/backend -COPY --from=frontend --chown=$APP_USER:$APP_USER /frontend/frontend ${APP}/frontend +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 ["./backend"] +ENTRYPOINT ["./bob-management"] +CMD ["--default"] From 9aeb373143404680134dc9634f43f0c70d0f90ad Mon Sep 17 00:00:00 2001 From: Simeon Romanov Date: Wed, 15 Nov 2023 09:02:09 +0300 Subject: [PATCH 10/10] change app dir --- dockerfiles/alpine/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dockerfiles/alpine/Dockerfile b/dockerfiles/alpine/Dockerfile index 95451a9b..8e7a0e6b 100644 --- a/dockerfiles/alpine/Dockerfile +++ b/dockerfiles/alpine/Dockerfile @@ -53,7 +53,7 @@ COPY ./frontend ./frontend RUN cd frontend && yarn && yarn build && mkdir /build_output && cp -r ./frontend /build_output/frontend FROM alpine:3.18 -ARG APP=/usr/src/app +ARG APP=/home/bob-management ENV TZ=Etc/UTC \ APP_USER=bobm ENV PATH="$PATH:${APP}"