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..c901df051 100644 --- a/docker/README.md +++ b/docker/README.md @@ -5,13 +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 -docker buildx build -t mega-mono:1.0 -f ./docker/mega_mono_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 @@ -22,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 @@ -37,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 ``` @@ -63,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 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 883946a86..eef43412f 100755 --- a/docker/start_mono.sh +++ b/docker/start_mono.sh @@ -1,10 +1,12 @@ #!/bin/sh -CONFIG_FILE="/etc/mega" -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