From 20c296424249b666ce72938cd0d510b5a1312ff8 Mon Sep 17 00:00:00 2001 From: Lumither Tao Date: Sun, 8 Dec 2024 22:25:47 -0800 Subject: [PATCH] feat(container) production build --- .env | 18 +++++------ Makefile | 16 ++++++++-- backend/.dockerignore | 2 ++ backend/api_server/src/blog/cron/mod.rs | 2 +- container/release/backend.containerfile | 10 +++++++ container/release/frontend.containerfile | 15 ++++++++++ dev.compose.yaml | 14 ++++----- frontend/.dockerignore | 4 +-- frontend/next.config.mjs | 4 +-- prod.compose.yaml | 38 ++++++++++++++++++++++++ 10 files changed, 99 insertions(+), 24 deletions(-) create mode 100644 prod.compose.yaml diff --git a/.env b/.env index 3708ad5..3007617 100644 --- a/.env +++ b/.env @@ -1,19 +1,19 @@ # postgres settings DB_HOST=localhost DB_PORT=5432 -POSTGRES_PASSWORD= -POSTGRES_USER= +#POSTGRES_PASSWORD= +POSTGRES_USER=nocturne POSTGRES_DB=nocturne +POSTGRES_HOST=postgres # backend settings - #GIT_WORK_DIR=../blog_posts_git #LOG_ROOT_DIR=/var/log/nocturne - -BLOG_ENDPOINT=https://github.com/Lumither/blog-posts.git -WORK_DIR=~/.nocturne -BACKEND_PORT=3001 +#BLOG_ENDPOINT=https://github.com/Lumither/blog-posts.git +BLOG_GIT_URL=https://github.com/Lumither/blog-posts.git +WORK_DIR=/nocturne +#BACKEND_PORT=3001 # frontend settings -API_LOCAL_URL=http://localhost:3001 -API_REMOTE_URL=https://lumither.com/api +API_LOCAL_URL=http://backend:3001 +API_REMOTE_URL=https://lumither.com/api \ No newline at end of file diff --git a/Makefile b/Makefile index b837df5..0c4e0d9 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,16 @@ -ddev: +ARCH := $(shell uname -m) +ifeq ($(ARCH), arm64) + ARCH := aarch64 +endif + +all: release + +release: + docker compose -f prod.compose.yaml build --build-arg ARCH=$(ARCH) + +dev: docker-compose -f dev.compose.yaml up --build -dclean: - docker-compose -f dev.compose.yaml rm -fsv +clean: + docker-compose -f prod.compose.yaml -f dev.compose.yaml rm -fsv diff --git a/backend/.dockerignore b/backend/.dockerignore index ea8c4bf..e07dbd6 100644 --- a/backend/.dockerignore +++ b/backend/.dockerignore @@ -1 +1,3 @@ /target + +/Cargo.lock \ No newline at end of file diff --git a/backend/api_server/src/blog/cron/mod.rs b/backend/api_server/src/blog/cron/mod.rs index e8fd785..686fb26 100644 --- a/backend/api_server/src/blog/cron/mod.rs +++ b/backend/api_server/src/blog/cron/mod.rs @@ -7,7 +7,7 @@ pub mod check_update; pub fn get_tasks(db_connection: Pool) -> Vec> { let db_conn_clone = db_connection.clone(); vec![ - BasicTask::new(move || check_update::task(&db_conn_clone), "0 * * * * *") + BasicTask::new(move || check_update::task(&db_conn_clone), "0/15 * * * * *") .unwrap() .to_task(), ] diff --git a/container/release/backend.containerfile b/container/release/backend.containerfile index e69de29..a82fbb1 100644 --- a/container/release/backend.containerfile +++ b/container/release/backend.containerfile @@ -0,0 +1,10 @@ +FROM rust:latest AS builder +WORKDIR /app +COPY . . +RUN ["cargo", "build", "-r"] + +FROM gcr.io/distroless/cc AS production +ARG ARCH +COPY --from=builder /app/target/release/api_server ./api_server +COPY --from=builder /usr/lib/${ARCH}-linux-gnu/libz.so.1 /usr/lib/${ARCH}-linux-gnu/ +ENTRYPOINT ["./api_server"] \ No newline at end of file diff --git a/container/release/frontend.containerfile b/container/release/frontend.containerfile index e69de29..19f4be4 100644 --- a/container/release/frontend.containerfile +++ b/container/release/frontend.containerfile @@ -0,0 +1,15 @@ +FROM node:22 AS builder +WORKDIR /app +ENV NEXT_TELEMETRY_DISABLED=1 +COPY package*.json ./ +RUN npm install +COPY . . +RUN npm run build + +FROM gcr.io/distroless/nodejs22 AS production +ENV NEXT_TELEMETRY_DISABLED=1 +COPY --from=builder /app/next.config.mjs ./ +COPY --from=builder /app/.next/standalone ./ +COPY --from=builder /app/.next/static ./.next/static +ENV HOSTNAME="0.0.0.0" +CMD ["./server.js"] diff --git a/dev.compose.yaml b/dev.compose.yaml index 1a7bf29..a8c89b9 100644 --- a/dev.compose.yaml +++ b/dev.compose.yaml @@ -1,11 +1,11 @@ services: frontend: build: - context: ./frontend + context: ./frontend dockerfile: ../container/dev/frontend.containerfile restart: always ports: - - 3000:3000 + - "3000:3000" volumes: - ./frontend:/app - /app/node_modules @@ -14,15 +14,15 @@ services: - ./container/dev/.env dns: - 1.1.1.1 - - + + backend: - build: - context: ./backend + build: + context: ./backend dockerfile: ../container/dev/backend.containerfile restart: always ports: - - 3001:3001 + - "3001:3001" volumes: - ./blog_posts_git:/app/blog_posts_git:ro env_file: diff --git a/frontend/.dockerignore b/frontend/.dockerignore index 88c64d6..0226748 100644 --- a/frontend/.dockerignore +++ b/frontend/.dockerignore @@ -1,7 +1,7 @@ /node_modules -/.next/ -/out/ +/.next +/out /build .DS_Store diff --git a/frontend/next.config.mjs b/frontend/next.config.mjs index 1ff5b8e..c98ab2c 100644 --- a/frontend/next.config.mjs +++ b/frontend/next.config.mjs @@ -11,8 +11,8 @@ const nextConfig = { hostname: 'pic.re' } ] - } - // output: 'standalone' + }, + output: 'standalone' }; export default nextConfig; diff --git a/prod.compose.yaml b/prod.compose.yaml new file mode 100644 index 0000000..aece627 --- /dev/null +++ b/prod.compose.yaml @@ -0,0 +1,38 @@ +services: + frontend: + depends_on: + - backend + build: + context: ./frontend + dockerfile: ../container/release/frontend.containerfile + restart: always + ports: + - "3000:3000" + env_file: + - ./.env + + + backend: + depends_on: + - postgres + build: + context: ./backend + dockerfile: ../container/release/backend.containerfile + args: + ARCH: ${ARCH} + restart: always + ports: + - "3001:3001" + env_file: + - ./.env + - ./.env.local + dns: + - 1.1.1.1 + + postgres: + image: postgres:16.3 + ports: + - "5432:5432" + env_file: + - ./.env + - ./.env.local