From a28f7447c658421a6f4943fe30ea5b8016e8e087 Mon Sep 17 00:00:00 2001 From: HouXiaoxuan Date: Mon, 19 Aug 2024 01:08:18 +0800 Subject: [PATCH 1/3] fix(docker): start_mono config file check error Signed-off-by: HouXiaoxuan --- docker/start_mono.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker/start_mono.sh b/docker/start_mono.sh index 883946a86..4194acbe7 100755 --- a/docker/start_mono.sh +++ b/docker/start_mono.sh @@ -1,7 +1,7 @@ #!/bin/sh -CONFIG_FILE="/etc/mega" -MEGA_BASE_DIR="/etc/mega" +export MEGA_BASE_DIR="/etc/mega" +CONFIG_FILE="$MEGA_BASE_DIR/config.toml" # check if config file exists if [ -f "$CONFIG_FILE" ]; then exec /opt/mega/target/debug/mono -c "$CONFIG_FILE" service http --host 0.0.0.0 From 9222f1a01b530ae63116abb7d292d8313685af1f Mon Sep 17 00:00:00 2001 From: HouXiaoxuan Date: Mon, 19 Aug 2024 01:14:48 +0800 Subject: [PATCH 2/3] chore(docker): update mono services, split build stage, and adjust volume path to /opt/mega/etc Signed-off-by: HouXiaoxuan --- .dockerignore | 7 ++++++- docker/README.md | 5 +++-- docker/mega_mono_dockerfile | 34 +++++++++++++++++++++++++++++----- docker/start_mono.sh | 8 +++++--- 4 files changed, 43 insertions(+), 11 deletions(-) diff --git a/.dockerignore b/.dockerignore index 37bfc1ab4..9bef78afd 100644 --- a/.dockerignore +++ b/.dockerignore @@ -27,4 +27,9 @@ ztm_agent.db ztm_agent_db* # buck2 out -buck-out \ No newline at end of file +buck-out + +# docker files +docker/mega_mono_dockerfile +docker/mega_moon_dockerfile +docker/mega_pg_dockerfile \ No newline at end of file diff --git a/docker/README.md b/docker/README.md index 01b348c7f..e2c3946f7 100644 --- a/docker/README.md +++ b/docker/README.md @@ -7,8 +7,9 @@ # build postgres image docker buildx build -t mega-db:1.0 -f ./docker/mega_pg_dockerfile . -# build backend mono image -docker buildx build -t mega-mono:1.0 -f ./docker/mega_mono_dockerfile . +# build backend mono image (default in release mode) +# build backend mono in debug mode +# docker buildx build -t mega-mono:1.0 -f ./docker/mega_mono_dockerfile --build-arg BUILD_TYPE=debug . # build frontend moon image docker buildx build -t mega-moon:1.0 -f ./docker/mega_moon_dockerfile . diff --git a/docker/mega_mono_dockerfile b/docker/mega_mono_dockerfile index 2e82d09f1..c93002c62 100644 --- a/docker/mega_mono_dockerfile +++ b/docker/mega_mono_dockerfile @@ -1,7 +1,16 @@ -FROM debian:bookworm-slim +FROM debian:bookworm-slim AS builder WORKDIR /opt/mega +# build args, to specify the build type, release or debug +ARG BUILD_TYPE=release + +# check arg value +RUN if [ "$BUILD_TYPE" != "release" ] && [ "$BUILD_TYPE" != "debug" ]; then \ + echo "Invalid BUILD_TYPE: $BUILD_TYPE, must be release or debug"; \ + exit 1; \ + fi + # set mirror for apt # RUN echo "deb http://mirrors.ustc.edu.cn/debian bookworm main contrib non-free" > /etc/apt/sources.list && \ # echo "deb http://mirrors.ustc.edu.cn/debian-security bookworm-security main contrib non-free" >> /etc/apt/sources.list && \ @@ -28,11 +37,26 @@ ENV PATH=/root/.cargo/bin:$PATH! # copy the source code, the context must be the root of the project COPY . . -RUN chmod +x /opt/mega/docker/start_mono.sh # build -RUN cargo build -p mono +RUN if [ "$BUILD_TYPE" = "release" ]; then \ + cargo build -p mono --release; \ + else \ + cargo build -p mono; \ + fi + +# final image +FROM debian:bookworm-slim + +RUN apt-get update && apt-get install -y libssl-dev + +ARG BUILD_TYPE=release + +COPY --from=builder /opt/mega/target/$BUILD_TYPE/mono /usr/local/bin/mono +COPY --from=builder /opt/mega/docker/start_mono.sh /usr/local/bin/start_mono.sh +RUN chmod +x /usr/local/bin/start_mono.sh +RUN chmod +x /usr/local/bin/mono -VOLUME /etc/mega +VOLUME /opt/mega/etc -CMD ["bash", "-c", "/opt/mega/docker/start_mono.sh"] \ No newline at end of file +CMD ["bash", "-c", "/usr/local/bin/start_mono.sh"] \ No newline at end of file diff --git a/docker/start_mono.sh b/docker/start_mono.sh index 4194acbe7..eef43412f 100755 --- a/docker/start_mono.sh +++ b/docker/start_mono.sh @@ -1,10 +1,12 @@ #!/bin/sh -export MEGA_BASE_DIR="/etc/mega" +export MEGA_BASE_DIR="/opt/mega/etc" CONFIG_FILE="$MEGA_BASE_DIR/config.toml" + # check if config file exists if [ -f "$CONFIG_FILE" ]; then - exec /opt/mega/target/debug/mono -c "$CONFIG_FILE" service http --host 0.0.0.0 + echo "Using config file: $CONFIG_FILE" + exec /usr/local/bin/mono -c "$CONFIG_FILE" service http --host 0.0.0.0 else - exec /opt/mega/target/debug/mono service http --host 0.0.0.0 + exec /usr/local/bin/mono service http --host 0.0.0.0 fi \ No newline at end of file From 4dec83ab0c258dbc1a3edc22fc659b5a7e3493df Mon Sep 17 00:00:00 2001 From: HouXiaoxuan Date: Mon, 19 Aug 2024 01:21:02 +0800 Subject: [PATCH 3/3] chore(docker): adjust image tag to `0.1-pre-release` Signed-off-by: HouXiaoxuan --- docker/README.md | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/docker/README.md b/docker/README.md index e2c3946f7..c901df051 100644 --- a/docker/README.md +++ b/docker/README.md @@ -5,14 +5,15 @@ # cd root of the project # build postgres image -docker buildx build -t mega-db:1.0 -f ./docker/mega_pg_dockerfile . +docker buildx build -t mega-db:0.1-pre-release -f ./docker/mega_pg_dockerfile . # build backend mono image (default in release mode) +docker buildx build -t mega-mono:0.1-pre-release -f ./docker/mega_mono_dockerfile . # build backend mono in debug mode # docker buildx build -t mega-mono:1.0 -f ./docker/mega_mono_dockerfile --build-arg BUILD_TYPE=debug . # build frontend moon image -docker buildx build -t mega-moon:1.0 -f ./docker/mega_moon_dockerfile . +docker buildx build -t mega-moon:0.1-pre-release -f ./docker/mega_moon_dockerfile . ``` # test mono and moon @@ -23,8 +24,8 @@ docker buildx build -t mega-moon:1.0 -f ./docker/mega_moon_dockerfile . # create network docker network create mega-network -docker run --rm -it -d --network mega-network --name mega-mono -v ./mega_base:/etc/mega mega-mono:1.0 -docker run --rm -it -d --network mega-network -e NEXT_PUBLIC_API_URL=http://mega-mono:8000 -p 3000:3000 mega-moon:1.0 +docker run --rm -it -d --network mega-network --name mega-mono -v ./mega_base:/opt/mega/etc mega-mono:0.1-pre-release +docker run --rm -it -d --network mega-network -e NEXT_PUBLIC_API_URL=http://mega-mono:8000 -p 3000:3000 mega-moon:0.1-pre-release ``` visit http://localhost:3000 to see the frontend @@ -38,13 +39,13 @@ visit http://localhost:3000 to see the frontend docker network create mega-network # run postgres -docker run --rm -it -d --network mega-network --name mega-db mega-db:1.0 +docker run --rm -it -d --network mega-network --name mega-db mega-db:0.1-pre-release ``` 2. create default config ```bash -docker run --rm -it -d --network mega-network --name mega-mono -v ./mega_base:/etc/mega mega-mono:1.0 +docker run --rm -it -d --network mega-network --name mega-mono -v ./mega_base:/opt/mega/etc mega-mono:0.1-pre-release docker stop mega-mono ``` @@ -64,6 +65,6 @@ db_url = "postgres://mega:mega@mega-db:5432/mega" 4. Start the mono again, and run the frontend. ```bash -docker run --rm -it -d --network mega-network --name mega-mono -v ./mega_base:/etc/mega mega-mono:1.0 -docker run --rm -it -d --network mega-network -e NEXT_PUBLIC_API_URL=http://mega-mono:8000 -p 3000:3000 mega-moon:1.0 +docker run --rm -it -d --network mega-network --name mega-mono -v ./mega_base:/opt/mega/etc mega-mono:0.1-pre-release +docker run --rm -it -d --network mega-network -e NEXT_PUBLIC_API_URL=http://mega-mono:8000 -p 3000:3000 mega-moon:0.1-pre-release ``` \ No newline at end of file