From 99c5765e6fea28db1870a0393a13648e22853061 Mon Sep 17 00:00:00 2001 From: Emnaghz Date: Fri, 27 Sep 2024 11:22:18 +0100 Subject: [PATCH 1/3] fix: add production target to api --- .github/workflows/docker.yml | 1 + api/Dockerfile | 46 +++++++++++++++++++++++------------ docker/docker-compose.dev.yml | 1 + 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 4bafe0d..25c2ebb 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -66,6 +66,7 @@ jobs: uses: docker/build-push-action@v6 with: context: ./api/ + target: production file: ./api/Dockerfile platforms: linux/amd64,linux/arm64 push: true diff --git a/api/Dockerfile b/api/Dockerfile index 7e90a7a..a8a9f54 100644 --- a/api/Dockerfile +++ b/api/Dockerfile @@ -4,32 +4,46 @@ WORKDIR /app COPY . . -FROM node:18-alpine AS installer - -WORKDIR /app - -COPY --from=builder /app/package*.json ./ - -COPY --from=builder /app/merge-extensions-deps.js ./ - -COPY --from=builder /app/src/extensions ./src/extensions - -COPY --from=builder /app/patches ./patches - RUN npm update -g npm RUN npm config set registry https://registry.npmjs.com/ +RUN npm run preinstall + RUN npm i --verbose --maxsockets 6 -FROM node:18-alpine AS runner +RUN npm run build + + +FROM node:18-alpine AS production WORKDIR /app -COPY --from=installer /app/ . +COPY --from=builder /app/package*.json ./ +COPY --from=builder /app/ . +COPY --from=builder /app/node_modules ./node_modules -COPY . . +ENV NODE_ENV=production + +# Run npm prune to remove dev dependencies +RUN npm prune --production EXPOSE 3000 -CMD [ "npm", "run" , "start:dev" ] +CMD ["npm", "run", "start:prod"] + + +FROM node:18-alpine AS development + +WORKDIR /app + +COPY --from=builder /app/package*.json ./ +COPY --from=builder /app/ . + +ENV NODE_ENV=development + +RUN npm install + +EXPOSE 3000 + +CMD ["npm", "run", "start:dev"] diff --git a/docker/docker-compose.dev.yml b/docker/docker-compose.dev.yml index afa04d2..e577e8b 100644 --- a/docker/docker-compose.dev.yml +++ b/docker/docker-compose.dev.yml @@ -13,6 +13,7 @@ services: api: build: context: ../api + target: development pull_policy: build ports: - ${API_PORT}:3000 From a71bef2161c9557189bd2be1af4caff3f1d7b526 Mon Sep 17 00:00:00 2001 From: Emnaghz Date: Fri, 27 Sep 2024 23:57:15 +0100 Subject: [PATCH 2/3] fix: add few changes --- api/Dockerfile | 32 ++++++++++++++++---------------- docker/docker-compose.dev.yml | 1 - 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/api/Dockerfile b/api/Dockerfile index a8a9f54..ba71e9f 100644 --- a/api/Dockerfile +++ b/api/Dockerfile @@ -15,6 +15,22 @@ RUN npm i --verbose --maxsockets 6 RUN npm run build +FROM node:18-alpine AS development + +WORKDIR /app + +COPY --from=builder /app/package*.json ./ +COPY --from=builder /app/ . + +ENV NODE_ENV=development + +RUN npm install + +EXPOSE 3000 + +CMD ["npm", "run", "start:debug"] + + FROM node:18-alpine AS production WORKDIR /app @@ -31,19 +47,3 @@ RUN npm prune --production EXPOSE 3000 CMD ["npm", "run", "start:prod"] - - -FROM node:18-alpine AS development - -WORKDIR /app - -COPY --from=builder /app/package*.json ./ -COPY --from=builder /app/ . - -ENV NODE_ENV=development - -RUN npm install - -EXPOSE 3000 - -CMD ["npm", "run", "start:dev"] diff --git a/docker/docker-compose.dev.yml b/docker/docker-compose.dev.yml index e577e8b..a294004 100644 --- a/docker/docker-compose.dev.yml +++ b/docker/docker-compose.dev.yml @@ -22,7 +22,6 @@ services: - ../api/src:/app/src - ../api/migrations:/app/migrations #- ../api/node_modules:/app/node_modules - command: ["npm", "run", "start:debug"] mongo-express: container_name: mongoUi From e84090b6bff00745d6d1d866772412a5b4c0c054 Mon Sep 17 00:00:00 2001 From: Mohamed Marrouchi Date: Sun, 29 Sep 2024 11:49:05 +0100 Subject: [PATCH 3/3] fix: api dockerfile for prod --- Makefile | 2 +- api/Dockerfile | 44 ++++++++++++++++++++++------------- api/package.json | 5 ++-- api/src/database-init.ts | 31 ------------------------ api/src/main.ts | 6 ++++- docker/docker-compose.dev.yml | 9 ------- docker/docker-compose.yml | 13 ----------- 7 files changed, 36 insertions(+), 74 deletions(-) delete mode 100644 api/src/database-init.ts diff --git a/Makefile b/Makefile index 915f3fd..08753f3 100644 --- a/Makefile +++ b/Makefile @@ -41,7 +41,7 @@ start: check-env # Dev command: runs docker-compose with the main file, dev file, and any additional service dev files (if they exist) dev: check-env - @docker compose -f $(FOLDER)/docker-compose.yml -f $(FOLDER)/docker-compose.dev.yml $(call compose_files) $(call compose_dev_files) up -d + @docker compose -f $(FOLDER)/docker-compose.yml -f $(FOLDER)/docker-compose.dev.yml $(call compose_files) $(call compose_dev_files) up --build -d # Start command: runs docker-compose with the main file and any additional service files start-prod: check-env diff --git a/api/Dockerfile b/api/Dockerfile index ba71e9f..8f057a9 100644 --- a/api/Dockerfile +++ b/api/Dockerfile @@ -2,48 +2,60 @@ FROM node:18-alpine AS builder WORKDIR /app -COPY . . +COPY package*.json ./ -RUN npm update -g npm +COPY merge-extensions-deps.js ./ -RUN npm config set registry https://registry.npmjs.com/ +COPY src/extensions ./src/extensions -RUN npm run preinstall +COPY patches ./patches -RUN npm i --verbose --maxsockets 6 +RUN npm ci + +COPY . . RUN npm run build - FROM node:18-alpine AS development WORKDIR /app -COPY --from=builder /app/package*.json ./ -COPY --from=builder /app/ . +COPY package*.json ./ -ENV NODE_ENV=development +COPY --from=builder /app/merge-extensions-deps.js ./ + +COPY --from=builder /app/src/extensions ./src/extensions + +COPY --from=builder /app/patches ./patches RUN npm install +COPY . . + +ENV NODE_ENV=development + EXPOSE 3000 CMD ["npm", "run", "start:debug"] - FROM node:18-alpine AS production WORKDIR /app -COPY --from=builder /app/package*.json ./ -COPY --from=builder /app/ . -COPY --from=builder /app/node_modules ./node_modules +COPY package*.json ./ + +COPY --from=builder /app/merge-extensions-deps.js ./ + +COPY --from=builder /app/src/extensions ./src/extensions + +COPY --from=builder /app/patches ./patches + +RUN npm ci --only=production + +COPY --from=builder /app/dist ./dist ENV NODE_ENV=production -# Run npm prune to remove dev dependencies -RUN npm prune --production - EXPOSE 3000 CMD ["npm", "run", "start:prod"] diff --git a/api/package.json b/api/package.json index 238d822..3d78dba 100644 --- a/api/package.json +++ b/api/package.json @@ -24,7 +24,6 @@ "test:cov": "jest --coverage --runInBand --detectOpenHandles --forceExit", "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", "test:e2e": "jest --config ./test/jest-e2e.json", - "cache:init": "ts-node -T ./src/database-init.ts", "typecheck": "tsc --noEmit", "containers:restart": "cd .. && make init && make stop && make start", "containers:rebuild": "cd .. && make init && make destroy && make start", @@ -77,13 +76,13 @@ "rxjs": "^7.8.1", "slug": "^8.2.2", "ts-migrate-mongoose": "^3.8.3", - "uuid": "^9.0.1" + "uuid": "^9.0.1", + "@nestjs/swagger": "^7.2.0" }, "devDependencies": { "@compodoc/compodoc": "^1.1.24", "@nestjs/cli": "^10.0.0", "@nestjs/schematics": "^10.0.0", - "@nestjs/swagger": "^7.2.0", "@nestjs/testing": "^10.0.0", "@types/bcryptjs": "^2.4.6", "@types/cookie-parser": "^1.4.6", diff --git a/api/src/database-init.ts b/api/src/database-init.ts deleted file mode 100644 index 7dc544d..0000000 --- a/api/src/database-init.ts +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright © 2024 Hexastack. All rights reserved. - * - * Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms: - * 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission. - * 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file). - * 3. SaaS Restriction: This software, or any derivative of it, may not be used to offer a competing product or service (SaaS) without prior written consent from Hexastack. Offering the software as a service or using it in a commercial cloud environment without express permission is strictly prohibited. - */ - -import { NestFactory } from '@nestjs/core'; -import moduleAlias from 'module-alias'; - -moduleAlias.addAliases({ - '@': __dirname, -}); - -import { AppModule } from './app.module'; -import { config } from './config'; -import { seedDatabase } from './seeder'; - -async function databaseInit() { - const isProduction = config.env.toLowerCase().includes('prod'); - - const app = await NestFactory.createApplicationContext(AppModule); - - if (!isProduction) await seedDatabase(app); - - process.exit(0); -} - -databaseInit(); diff --git a/api/src/main.ts b/api/src/main.ts index 7ad1eef..a25db9c 100644 --- a/api/src/main.ts +++ b/api/src/main.ts @@ -22,6 +22,7 @@ moduleAlias.addAliases({ import { AppModule } from './app.module'; import { config } from './config'; import { LoggerService } from './logger/logger.service'; +import { seedDatabase } from './seeder'; import { swagger } from './swagger'; import { sessionStore } from './utils/constants/session-store'; import { ObjectIdPipe } from './utils/pipes/object-id.pipe'; @@ -83,7 +84,10 @@ async function bootstrap() { else throw error; }); - if (!isProduction) swagger(app); + if (!isProduction) { + await seedDatabase(app); + swagger(app); + } await app.listen(3000); } diff --git a/docker/docker-compose.dev.yml b/docker/docker-compose.dev.yml index a294004..9705032 100644 --- a/docker/docker-compose.dev.yml +++ b/docker/docker-compose.dev.yml @@ -1,15 +1,6 @@ version: "3.8" services: - database-init: - build: - context: ../api - pull_policy: build - volumes: - - ../api/src:/app/src - - ../api/migrations:/app/migrations - # - ../api/node_modules:/app/node_modules - api: build: context: ../api diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 80bcdf3..4d1b68d 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -1,17 +1,6 @@ version: "3.9" services: - database-init: - container_name: database-init - image: hexastack/hexabot-api:latest - command: sh -c "npm run cache:init && npm run migrate prune && npm run migrate up" - env_file: .env - networks: - - db-network - depends_on: - mongo: - condition: service_healthy - api: container_name: api image: hexastack/hexabot-api:latest @@ -26,8 +15,6 @@ services: depends_on: mongo: condition: service_healthy - database-init: - condition: service_completed_successfully healthcheck: test: "wget --spider http://localhost:3000" interval: 10s