Skip to content

Commit

Permalink
chore: dockerize monorepo
Browse files Browse the repository at this point in the history
  • Loading branch information
francislagares committed Jul 4, 2024
1 parent ef2005c commit 9fbb377
Show file tree
Hide file tree
Showing 58 changed files with 711 additions and 1,047 deletions.
26 changes: 26 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Versioning and metadata
.git
.vscode
.gitignore
.dockerignore

# Build dependencies
dist
/node_modules

# code formatter
.editorconfig
.eslintignore
.eslintrc.json
.huskyrc
.lintstagedrc.json
.prettierrc
README.md

# test
coverage
vitest.config.js

# docker
Dockerfile
docker-compose.yml
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
MIT License

Copyright (c) 2023 Kliton

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 changes: 26 additions & 0 deletions api/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Versioning and metadata
.git
.gitignore
.dockerignore

# Build dependencies
.vscode
dist.do
/node_modules

# code formatter
.editorconfig
.eslintignore
.eslintrc.json
.huskyrc
.lintstagedrc.json
.prettierrc
README.md

# test
coverage
vitest.config.js

# docker
Dockerfile
docker-compose.yml
24 changes: 24 additions & 0 deletions api/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM node:alpine

RUN apk update && apk add --no-cache nodejs && corepack enable
RUN addgroup backend && adduser -S -G backend francis

USER francis

WORKDIR /node-api

COPY package.json tsconfig.json ./

USER root

RUN chown -R francis:backend .

USER francis

RUN pnpm install

COPY . .

EXPOSE 4000

CMD ["pnpm", "start:dev"]
35 changes: 35 additions & 0 deletions api/Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
FROM node:alpine AS builder

RUN apk update && apk add --no-cache nodejs && corepack enable
RUN addgroup mern && adduser -S -G mern api

USER api

WORKDIR /api

COPY package.json tsconfig.json ./
COPY src ./

RUN pnpm ci && pnpm build


FROM node:alpine

RUN apk update && apk add --no-cache nodejs && corepack enable
RUN addgroup mern && adduser -S -G mern api

USER api

WORKDIR /api

COPY package.json tsconfig.json ./
COPY src ./

RUN pnpm add -g pm2
RUN pnpm ci && pnpm build
RUN pnpm ci --production
COPY --from=builder /api/build ./build

EXPOSE 4000

CMD [ "pnpm", "start" ]
64 changes: 0 additions & 64 deletions api/docker-compose-mongodb.yaml

This file was deleted.

26 changes: 26 additions & 0 deletions api/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
services:
api:
container_name: node-api
image: node-api
build:
context: .
dockerfile: Dockerfile
restart: unless-stopped
stdin_open: true
env_file: .env.development.local
depends_on:
- mongodb
networks:
- mern
ports:
- 4000:4000
environment:
- NODE_ENV=development
- BASE_URL=http://localhost:4000
volumes:
- .:/node-api
- node_modules:/node-api/node_modules

volumes:
node_modules:
name: node-modules
2 changes: 1 addition & 1 deletion api/ecosystem.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = {
},
{
name: 'dev', // pm2 start App name
script: '../node_modules/.bin/ts-node', // ts-node
script: './node_modules/.bin/ts-node', // ts-node
args: '-r tsconfig-paths/register --transpile-only src/server.ts', // ts-node args
exec_mode: 'cluster', // 'cluster' or 'fork'
instance_var: 'INSTANCE_ID', // instance variable
Expand Down
11 changes: 8 additions & 3 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"author": "Francis Lagares",
"license": "MIT",
"scripts": {
"start:dev": "pnpm prisma:generate && pnpm prisma:seed cross-env NODE_ENV=development && tsx watch src/server.ts",
"start:dev": "pnpm prisma:generate && tsx watch src/server.ts",
"start": "pnpm build && NODE_ENV=production && node dist/server.js",
"build": "tsc && tsc-alias",
"deploy:clean": "pm2 stop all && pm2 delete all",
Expand Down Expand Up @@ -65,12 +65,17 @@
"ts-node": "^10.9.1",
"tsc-alias": "^1.8.6",
"tsconfig-paths": "^4.2.0",
"tsx": "^4.16.0",
"tsx": "^4.16.2",
"typescript-transform-paths": "^3.4.7"
},
"config": {
"commitizen": {
"path": "cz-conventional-changelog"
}
}
},
"engines": {
"node": ">=22.0.0",
"pnpm": ">=9.4.0"
},
"packageManager": "pnpm@9.4.0+sha512.f549b8a52c9d2b8536762f99c0722205efc5af913e77835dbccc3b0b0b2ca9e7dc8022b78062c17291c48e88749c70ce88eb5a74f1fa8c4bf5e18bb46c8bd83a"
}
13 changes: 8 additions & 5 deletions api/src/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import hpp from 'hpp';
import swaggerJSDoc from 'swagger-jsdoc';
import swaggerUi from 'swagger-ui-express';

import { MongoDBInstance as dbConnection } from 'api/src/config/database';
import { ErrorMiddleware } from '@/libs/shared/middlewares/error.middleware';

import logger from '@/utils/logger';

import { MongoDBInstance as dbConnection } from '@/config/database';
import {
BASE_URL,
CLIENT_URL,
Expand All @@ -16,10 +20,9 @@ import {
NODE_ENV,
ORIGIN,
PORT,
} from 'api/src/config/environment';
import { ErrorMiddleware } from 'api/src/libs/shared/middlewares/error.middleware';
import applicationRoutes from 'api/src/routes/index';
import logger from 'api/src/utils/logger';
} from '@/config/environment';

import applicationRoutes from '@/routes/index';

export class App {
private app: Application;
Expand Down
5 changes: 3 additions & 2 deletions api/src/config/database.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { database } from 'api/src/libs/shared/prisma/prisma';
import logger from 'api/src/utils/logger';
import { database } from '@/libs/shared/prisma/prisma';

import logger from '@/utils/logger';

export class MongoDBInstance {
private static instance: MongoDBInstance;
Expand Down
5 changes: 3 additions & 2 deletions api/src/libs/shared/middlewares/error.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { NextFunction, Request, Response } from 'express';

import { HttpException } from 'api/src/libs/shared/exceptions/httpException';
import logger from 'api/src/utils/logger';
import { HttpException } from '@/libs/shared/exceptions/httpException';

import logger from '@/utils/logger';

export const ErrorMiddleware = (
error: HttpException,
Expand Down
2 changes: 1 addition & 1 deletion api/src/libs/shared/prisma/middlewares/query-logger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Prisma } from '@prisma/client';

import logger from 'api/src/utils/logger';
import logger from '@/utils/logger';

export const queryLogger: Prisma.Middleware = async (params, next) => {
const before = Date.now();
Expand Down
2 changes: 1 addition & 1 deletion api/src/libs/shared/redis/cache-manager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Redis from 'ioredis';

import { REDIS_HOST, REDIS_PORT, REDIS_TTL } from 'api/src/config/environment';
import { REDIS_HOST, REDIS_PORT, REDIS_TTL } from '@/config/environment';

export class CacheManager {
private redis: Redis;
Expand Down
2 changes: 1 addition & 1 deletion api/src/routes/health.router.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import express, { Router } from 'express';

import HealthController from 'api/src/controllers/health.controller';
import HealthController from '@/controllers/health.controller';

export class HealthRoutes {
private router: Router;
Expand Down
5 changes: 3 additions & 2 deletions api/src/tests/health.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import express, { Application } from 'express';
import request from 'supertest';
import { beforeAll, describe, expect, it } from 'vitest';

import { App } from 'api/src/app';
import HealthController from 'api/src/controllers/health.controller';
import HealthController from '@/controllers/health.controller';

import { App } from '@/app';

describe('HealthController', () => {
let app: Application;
Expand Down
39 changes: 36 additions & 3 deletions api/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,41 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"module": "commonjs",
/* Project Setup */
"target": "esnext",
"module": "NodeNext",
"outDir": "./dist",
"allowJs": true,
"sourceMap": true,
"removeComments": false,
"isolatedModules": true,
"downlevelIteration": true,
"skipLibCheck": true,
"lib": ["dom", "esnext", "es2022"],

/* Strict Type-Checking */
"strict": true,

/* Module Resolution */
"esModuleInterop": true,
"moduleResolution": "NodeNext",
"resolveJsonModule": true,
"allowSyntheticDefaultImports": true,
"baseUrl": ".",
"paths": {
"@/*": ["src/*"]
},

/* Linter Checks */
"noFallthroughCasesInSwitch": true,

/* Experimental Options */
"emitDecoratorMetadata": true,
"experimentalDecorators": true,

/* Advanced Options */
"forceConsistentCasingInFileNames": true
},

"include": ["src/**/*.ts"],
"exclude": ["node_modules", "coverage"]
}
}
Loading

0 comments on commit 9fbb377

Please sign in to comment.