From 8c99a8254e3c21f8e4b91f75ee2cfbfa2e0dfb99 Mon Sep 17 00:00:00 2001 From: LarryKwon <65128957+LarryKwon@users.noreply.github.com> Date: Thu, 22 Aug 2024 03:42:36 +0900 Subject: [PATCH 1/3] apply multi-stage build to docker and make the share.service choose proper font file path by DOCKER_DEPLOY --- .dockerignore | 1 + docker/Dockerfile.server | 18 ++++++++++++------ docker/docker-compose-db-local.yml | 2 +- docker/docker-compose.dev.yml | 10 +++++++--- docker/docker-compose.local.yml | 26 ++++++++++++++++++++++++++ docker/docker-compose.prod.yml | 23 +++++++++++++++++++++++ src/modules/share/share.service.ts | 6 ++---- src/settings.ts | 10 ++++++++++ 8 files changed, 82 insertions(+), 14 deletions(-) create mode 100644 docker/docker-compose.local.yml create mode 100644 docker/docker-compose.prod.yml diff --git a/.dockerignore b/.dockerignore index ec9673fe..a255a93f 100644 --- a/.dockerignore +++ b/.dockerignore @@ -2,3 +2,4 @@ *Dockerfile* node_modules dist +volumes diff --git a/docker/Dockerfile.server b/docker/Dockerfile.server index 717c6c70..5454137b 100644 --- a/docker/Dockerfile.server +++ b/docker/Dockerfile.server @@ -1,16 +1,22 @@ -FROM node:18 - -ENV NODE_ENV=dev +FROM node:18 AS build RUN mkdir -p /var/www/otlplus-server WORKDIR /var/www/otlplus-server COPY package*.json ./ -RUN npm install --force +RUN npm install COPY . . RUN npm run client:generate RUN npm run build -EXPOSE 8000 -CMD [ "npm", "run", "start:dev" ] +FROM node:18-slim AS server +RUN apt-get update -y && apt-get install -y openssl + +COPY --from=build /var/www/otlplus-server/ /var/www/otlplus-server/ + +WORKDIR /var/www/otlplus-server + +RUN npm prune --production + +EXPOSE 8000 \ No newline at end of file diff --git a/docker/docker-compose-db-local.yml b/docker/docker-compose-db-local.yml index 6c6795f0..101630f9 100644 --- a/docker/docker-compose-db-local.yml +++ b/docker/docker-compose-db-local.yml @@ -8,7 +8,7 @@ services: ports: - '${OTLPLUS_DB_PORT:-43306}:3306' environment: - - MYSQL_ROOT_HOSTS=% + - MYSQL_ROOT_HOSTS=admin - MYSQL_ROOT_PASSWORD=${OTLPLUS_DB_PASSWORD:-password} - MYSQL_DATABASE=otlplus - TZ=Asia/Seoul diff --git a/docker/docker-compose.dev.yml b/docker/docker-compose.dev.yml index 8ae800e4..850e69f7 100644 --- a/docker/docker-compose.dev.yml +++ b/docker/docker-compose.dev.yml @@ -2,18 +2,22 @@ version: '3.4' services: back: - container_name: otlplus-server-nest + container_name: otlplus-server-nest-dev platform: linux/amd64 env_file: - ../env/.env.dev + environment: + - NODE_ENV=dev + - DOCKER_DEPLOY=true build: context: .. dockerfile: ./docker/Dockerfile.server - image: otlplus-server-nest + image: otlplus-server-nest-dev restart: always tty: true ports: - - '8080:8000' + - '58000:8000' volumes: - '/etc/timezone:/etc/timezone:ro' working_dir: /var/www/otlplus-server + command: node dist/src/bootstrap/bootstrap.js diff --git a/docker/docker-compose.local.yml b/docker/docker-compose.local.yml new file mode 100644 index 00000000..07570442 --- /dev/null +++ b/docker/docker-compose.local.yml @@ -0,0 +1,26 @@ +version: '3.4' + +services: + back: + container_name: otlplus-server-nest-local + platform: linux/amd64 + env_file: + - ../env/.env.local + environment: + - NODE_ENV=local + - DOCKER_DEPLOY=true + build: + context: .. + dockerfile: ./docker/Dockerfile.server + image: otlplus-server-nest-local + restart: always + tty: true + ports: + - '58000:8000' + expose: + - '8000' + volumes: + - '/etc/timezone:/etc/timezone:ro' + network_mode: 'host' + working_dir: /var/www/otlplus-server + command: node dist/src/bootstrap/bootstrap.js diff --git a/docker/docker-compose.prod.yml b/docker/docker-compose.prod.yml new file mode 100644 index 00000000..01ad13f8 --- /dev/null +++ b/docker/docker-compose.prod.yml @@ -0,0 +1,23 @@ +version: '3.4' + +services: + back: + container_name: otlplus-server-nest-prod + platform: linux/amd64 + env_file: + - ../env/.env.prod + environment: + - NODE_ENV=prod + - DOCKER_DEPLOY=true + build: + context: .. + dockerfile: ./docker/Dockerfile.server + image: otlplus-server-nest-prod + restart: always + tty: true + ports: + - '58000:8000' + volumes: + - '/etc/timezone:/etc/timezone:ro' + working_dir: /var/www/otlplus-server + command: node dist/src/bootstrap/bootstrap.js diff --git a/src/modules/share/share.service.ts b/src/modules/share/share.service.ts index ac82239f..a2765aa1 100644 --- a/src/modules/share/share.service.ts +++ b/src/modules/share/share.service.ts @@ -19,6 +19,7 @@ import { SemesterRepository } from 'src/prisma/repositories/semester.repository' import { LecturesService } from '../lectures/lectures.service'; import { SemestersService } from '../semesters/semesters.service'; import { TimetablesService } from '../timetables/timetables.service'; +import settings from '@src/settings'; interface RoundedRectangleOptions { ctx: CanvasRenderingContext2D; @@ -65,10 +66,7 @@ interface DrawTimetableDatas { @Injectable() export class ShareService { - private readonly file_path = - process.env.NODE_ENV === 'dev' - ? 'static/' - : '/var/www/otlplus-server/static/'; + private readonly file_path = settings().getStaticConfig().file_path; constructor( private readonly semesterRepository: SemesterRepository, diff --git a/src/settings.ts b/src/settings.ts index d680dc27..127e3a56 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -15,6 +15,7 @@ export default () => { getSsoConfig: () => getSsoConfig(), getCorsConfig: () => getCorsConfig(), getVersion: () => getVersion(), + getStaticConfig: () => staticConfig(), }; }; @@ -106,3 +107,12 @@ const getSsoConfig = (): any => { const getVersion = () => { return String(process.env.npm_package_version); }; + +const staticConfig = (): any => { + return { + file_path: + process.env.DOCKER_DEPLOY === 'true' + ? '/var/www/otlplus-server/static/' + : 'static/', + }; +}; From d37a7a92a9f2b1bcd717a38e7fe3bb9c218a2a73 Mon Sep 17 00:00:00 2001 From: LarryKwon <65128957+LarryKwon@users.noreply.github.com> Date: Thu, 22 Aug 2024 03:59:54 +0900 Subject: [PATCH 2/3] add deploy.sh --- deploy.sh | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100755 deploy.sh diff --git a/deploy.sh b/deploy.sh new file mode 100755 index 00000000..6c094fc0 --- /dev/null +++ b/deploy.sh @@ -0,0 +1,39 @@ +#!/bin/bash + +usage() { + echo "Usage: $0 -e value" + echo " -e: 현재 배포 환경을 입력하세요. (prod, dev, local)" +} + +NODE_ENV="" + +while getopts "e:" opt; do + case $opt in + e) + NODE_ENV=$OPTARG + echo "NODE_ENV: $NODE_ENV" + if [ "$NODE_ENV" == "prod" ]; then + docker-compose -f docker/docker-compose.prod.yml up -d + elif [ "$NODE_ENV" == "dev" ]; then + docker-compose -f docker/docker-compose.dev.yml up -d + elif [ "$NODE_ENV" == "local" ]; then + docker-compose -f docker/docker-compose.local.yml up -d + else + echo "Invalid environment: $NODE_ENV" 1>&2 + usage + exit 1 + fi + ;; + \?) + echo "Invalid option: -$OPTARG" 1>&2 + usage + exit 1 + ;; + esac +done + +if [ -z "$NODE_ENV" ]; then + echo "Error: Environment option is required." + usage + exit 1 +fi From ca5c79a037d6d839aefe05d96f9f2abc43fb40a7 Mon Sep 17 00:00:00 2001 From: LarryKwon <65128957+LarryKwon@users.noreply.github.com> Date: Thu, 22 Aug 2024 04:07:17 +0900 Subject: [PATCH 3/3] reflect review --- deploy.sh | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/deploy.sh b/deploy.sh index 6c094fc0..8a47ce82 100755 --- a/deploy.sh +++ b/deploy.sh @@ -12,17 +12,24 @@ while getopts "e:" opt; do e) NODE_ENV=$OPTARG echo "NODE_ENV: $NODE_ENV" - if [ "$NODE_ENV" == "prod" ]; then - docker-compose -f docker/docker-compose.prod.yml up -d - elif [ "$NODE_ENV" == "dev" ]; then - docker-compose -f docker/docker-compose.dev.yml up -d - elif [ "$NODE_ENV" == "local" ]; then - docker-compose -f docker/docker-compose.local.yml up -d + if [[ "$NODE_ENV" = "prod" ]]; then + COMPOSE_FILE="docker/docker-compose.prod.yml" + elif [[ "$NODE_ENV" = "dev" ]]; then + COMPOSE_FILE="docker/docker-compose.dev.yml" + elif [[ "$NODE_ENV" = "local" ]]; then + COMPOSE_FILE="docker/docker-compose.local.yml" else echo "Invalid environment: $NODE_ENV" 1>&2 usage exit 1 fi + + if [[ ! -f "$COMPOSE_FILE" ]]; then + echo "Error: Compose file does not exist: $COMPOSE_FILE" 1>&2 + exit 1 + fi + + docker-compose -f "$COMPOSE_FILE" up -d ;; \?) echo "Invalid option: -$OPTARG" 1>&2 @@ -36,4 +43,4 @@ if [ -z "$NODE_ENV" ]; then echo "Error: Environment option is required." usage exit 1 -fi +fi \ No newline at end of file