Merge pull request #98 from Hexastack/fix/api-prod-dockerfile

Fix/api prod dockerfile
This commit is contained in:
Mohamed Marrouchi 2024-09-29 12:15:23 +01:00 committed by GitHub
commit 620570b955
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 51 additions and 74 deletions

View File

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

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,13 +2,25 @@ FROM node:18-alpine AS builder
WORKDIR /app
COPY . .
COPY package*.json ./
FROM node:18-alpine AS installer
COPY merge-extensions-deps.js ./
COPY src/extensions ./src/extensions
COPY patches ./patches
RUN npm ci
COPY . .
RUN npm run build
FROM node:18-alpine AS development
WORKDIR /app
COPY --from=builder /app/package*.json ./
COPY package*.json ./
COPY --from=builder /app/merge-extensions-deps.js ./
@ -16,20 +28,34 @@ 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 i --verbose --maxsockets 6
FROM node:18-alpine AS runner
WORKDIR /app
COPY --from=installer /app/ .
RUN npm install
COPY . .
ENV NODE_ENV=development
EXPOSE 3000
CMD [ "npm", "run" , "start:dev" ]
CMD ["npm", "run", "start:debug"]
FROM node:18-alpine AS production
WORKDIR /app
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
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,18 +1,10 @@
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
target: development
pull_policy: build
ports:
- ${API_PORT}:3000
@ -21,7 +13,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

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