fix: api dockerfile for prod

This commit is contained in:
Mohamed Marrouchi 2024-09-29 11:49:05 +01:00
parent a71bef2161
commit e84090b6bf
7 changed files with 36 additions and 74 deletions

View File

@ -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

View File

@ -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"]

View File

@ -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",

View File

@ -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();

View File

@ -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);
}

View File

@ -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

View File

@ -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