Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Modifica configurações do Docker da Aplicação (fga-eps-mds/2024.2-ARANDU-DOC#42) #9

Merged
merged 7 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
.dockerignore

# Build dependencies
dist.do
node_modules
node_module
# dist.do
# node_modules
# node_module

# Misc
.eslintrc.js
Expand Down
19 changes: 0 additions & 19 deletions .env.dev

This file was deleted.

22 changes: 22 additions & 0 deletions .env.dev.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MONGO_INITDB_ROOT_USERNAME=
MONGO_INITDB_ROOT_PASSWORD=
ME_CONFIG_MONGODB_ADMINUSERNAME=
ME_CONFIG_MONGODB_ADMINPASSWORD=
ME_CONFIG_MONGODB_URL=
ME_CONFIG_BASICAUTH=
ME_CONFIG_BASICAUTH_USERNAME=
ME_CONFIG_BASICAUTH_PASSWORD=
MONGODB_URI=
EMAIL_USER=
EMAIL_PASS=
SENDGRID_API_KEY=
JWT_SECRET=
JWT_EXPIRATION=
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
GOOGLE_CALLBACK_URL=
MICROSOFT_CLIENT_ID=
MICROSOFT_CLIENT_SECRET=
MICROSOFT_TENANT_ID=
MICROSOFT_CALLBACK_URL=
FRONTEND_URL=
22 changes: 22 additions & 0 deletions .env.prod.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MONGO_INITDB_ROOT_USERNAME=
MONGO_INITDB_ROOT_PASSWORD=
ME_CONFIG_MONGODB_ADMINUSERNAME=
ME_CONFIG_MONGODB_ADMINPASSWORD=
ME_CONFIG_MONGODB_URL=
ME_CONFIG_BASICAUTH=
ME_CONFIG_BASICAUTH_USERNAME=
ME_CONFIG_BASICAUTH_PASSWORD=
MONGODB_URI=
EMAIL_USER=
EMAIL_PASS=
SENDGRID_API_KEY=
JWT_SECRET=
JWT_EXPIRATION=
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
GOOGLE_CALLBACK_URL=
MICROSOFT_CLIENT_ID=
MICROSOFT_CLIENT_SECRET=
MICROSOFT_TENANT_ID=
MICROSOFT_CALLBACK_URL=
FRONTEND_URL=
26 changes: 22 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
FROM node:22-alpine
FROM node:22 AS base

WORKDIR /app

COPY package.json package-lock.json* ./
RUN \
if [ -f package-lock.json ]; then npm ci; \
fi

COPY package*.json ./
RUN npm install nodemon --save-dev

FROM base AS builder
WORKDIR /app
RUN npm install

COPY . .

COPY .env .env
RUN npm run build

CMD ["npm", "run", "start:dev"]
FROM base AS runner
WORKDIR /app

ENV NODE_ENV=development

COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/ ./

EXPOSE 3000

ENV PORT=3000

CMD ["npm", "run", "start:dev"]
31 changes: 31 additions & 0 deletions Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
FROM node:22 AS base

WORKDIR /app

COPY package.json package-lock.json* ./
RUN \
if [ -f package-lock.json ]; then npm ci; \
fi

RUN npm install nodemon --save-dev

FROM base AS builder
WORKDIR /app
RUN npm install
COPY . .

COPY .env .env
RUN npm run build

FROM base AS runner

ENV NODE_ENV=production

COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/ ./

EXPOSE 3000

ENV PORT=3000

CMD ["npm", "run", "start:prod"]
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.PHONY: build
build:
npm install && docker-compose build --no-cache

.PHONY: start
start:
docker-compose up

.PHONY: run
run:
npm install & docker-compose build --no-cache && docker-compose up

.PHONY: stop
stop:
docker-compose down
50 changes: 41 additions & 9 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,51 @@ services:
user-api:
container_name: user-api
restart: on-failure
build: .
env_file:
- .env
build:
context: .
dockerfile: Dockerfile
image: user-api
ports:
- "3000:3000"
networks:
- arandu-network

mongo:
image: mongo
restart: always
env_file:
- .env
environment:
- NODE_ENV=development
MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
ports:
- 3000:3000
volumes:
- ./src:/app/src
- ./test:/app/test
- "27017:27017"
command: ["--quiet"]
networks:
- arandu-network

mongo-express:
image: mongo-express
restart: always
env_file:
- .env
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: ${ME_CONFIG_MONGODB_ADMINUSERNAME}
ME_CONFIG_MONGODB_ADMINPASSWORD: ${ME_CONFIG_MONGODB_ADMINPASSWORD}
ME_CONFIG_MONGODB_URL: ${MONGODB_URI}
ME_CONFIG_BASICAUTH: ${ME_CONFIG_BASICAUTH}
ME_CONFIG_BASICAUTH_USERNAME: ${ME_CONFIG_BASICAUTH_USERNAME}
ME_CONFIG_BASICAUTH_PASSWORD: ${ME_CONFIG_BASICAUTH_PASSWORD}
ports:
- "8081:8081"
networks:
- calculus-network
- arandu-network
depends_on:
- mongo

networks:
calculus-network:
driver: bridge
arandu-network:
name: arandu-network
external: true
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"start:prod": "nest start --watch",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest --passWithNoTests --no-cache --runInBand",
"test:all": "npm run test -- --coverage --config jest.config.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { ConfigModule, ConfigService } from '@nestjs/config';
import { MongooseModule } from '@nestjs/mongoose';
import { UsersModule } from './users/users.module';

import { AuthModule } from './auth/auth.module';
import * as Joi from 'joi';
import { AuthModule } from './auth/auth.module';

@Module({
imports: [
Expand Down
10 changes: 6 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Logger, ValidationPipe } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ConfigService } from '@nestjs/config';
import { Logger, ValidationPipe } from '@nestjs/common';

const configService = new ConfigService();
const logger = new Logger('Main');
Expand All @@ -10,7 +10,9 @@ async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(new ValidationPipe());
app.enableCors();
await app.listen(configService.get('PORT'));
logger.log(`Application listening on port ${configService.get('PORT')}`);
await app.listen(configService.get('PORT'), '0.0.0.0', () => {
logger.log(`Application listening on port ${configService.get('PORT')}`);
});

}
bootstrap();
Loading