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

[WIP] Adding a complete Docker setup #7

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,12 @@ npm run migration:run

> ### Docker Compose

Use the command below to run our `infrastructure` with `docker` using the [infrastructure.yaml](./deployments/docker-compose/infrastructure.yaml) file at the `root` of the app:
Use the commands below to run both `infrastructure` and `services` with `docker` using the `compose.taml` files located in the [associated directory](./deployments/docker-compose/) :

```bash
docker-compose -f ./deployments/docker-compose/infrastructure.yaml up -d
docker-compose -f ./deployments/docker-compose/compose.infrastructure.yaml up -d
docker-compose -f ./deployments/docker-compose/compose.services.yaml up -d
```
##### Todo
I will add `docker-compsoe` for up and running whole app here in the next...

> ### Build
To `build` each microservice, run this command in the root directory of each microservice where the `package.json` file is located:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ services:
#######################################################
# Postgress
#######################################################
#
# TODO
# psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB"
# CREATE DATABASE identity; GRANT ALL PRIVILEGES ON DATABASE identity TO postgres;
# CREATE DATABASE flight; GRANT ALL PRIVILEGES ON DATABASE flight TO postgres;
# CREATE DATABASE booking; GRANT ALL PRIVILEGES ON DATABASE booking TO postgres;
# CREATE DATABASE passenger; GRANT ALL PRIVILEGES ON DATABASE passenger TO postgres;
#
postgres:
container_name: postgres
image: postgres:latest
Expand All @@ -30,7 +38,6 @@ services:
networks:
- booking


#######################################################
# Jaeger
#######################################################
Expand All @@ -43,7 +50,8 @@ services:
- 5778:5778
- 6831:6831/udp
- 6832:6832/udp
- 9411:9411
# Zipkin compatible endpoint (@see https://www.jaegertracing.io/docs/1.6/getting-started/#migrating-from-zipkin )
# - 9411:9411
- 14268:14268
- 16686:16686
networks:
Expand All @@ -63,7 +71,4 @@ services:

networks:
booking:




name: bmn_network
83 changes: 83 additions & 0 deletions deployments/docker-compose/compose.services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
version: "3.3"
services:

#######################################################
# Identities
#######################################################
identity:
container_name: bmn-identity
build:
context: ../../
dockerfile: ./src/Dockerfile
args:
TARGET: identity
restart: unless-stopped
ports:
- 3333:3333
environment:
# Used to select which .env file should be used
- NODE_ENV=devdocker
networks:
- booking

#######################################################
# Passengers
#######################################################
passenger:
container_name: bmn-passenger
build:
context: ../../
dockerfile: ./src/Dockerfile
args:
TARGET: passenger
restart: unless-stopped
ports:
- 3355:3355
environment:
# Used to select which .env file should be used
- NODE_ENV=devdocker
networks:
- booking

#######################################################
# Flights
#######################################################
flight:
container_name: bmn-flight
build:
context: ../../
dockerfile: ./src/Dockerfile
args:
TARGET: flight
restart: unless-stopped
ports:
- 3344:3344
environment:
# Used to select which .env file should be used
- NODE_ENV=devdocker
networks:
- booking

#######################################################
# Bookings
#######################################################
booking:
container_name: bmn-booking
build:
context: ../../
dockerfile: ./src/Dockerfile
args:
TARGET: booking
restart: unless-stopped
ports:
- 3366:3366
environment:
# Used to select which .env file should be used
- NODE_ENV=devdocker
networks:
- booking

networks:
booking:
name: bmn_network
external: true
38 changes: 38 additions & 0 deletions src/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
FROM node:21.7-alpine

# Install NestJS CLI tool
RUN npm install -g @nestjs/cli

# Switch to non-root user
USER node

# Get build target
ARG TARGET
ENV BLOCKS=building-blocks

# Define env variables for workdirs
ENV WORKDIR_BASE=/usr/src/app
ENV WORKDIR_BLOCKS=${WORKDIR_BASE}/${BLOCKS}
ENV WORKDIR_TARGET=${WORKDIR_BASE}/${TARGET}

# Build utility package
WORKDIR ${WORKDIR_BLOCKS}
COPY --chown=node:node ./src/${BLOCKS}/package*.json .
RUN npm install
COPY --chown=node:node ./src/${BLOCKS} .
RUN npm run build

# Build target package
WORKDIR ${WORKDIR_TARGET}
COPY --chown=node:node ./src/${TARGET}/package*.json .
RUN npm install
COPY --chown=node:node ./src/${TARGET} .
RUN npm run build

# Run the app (target)
CMD exec npm run dev

# ARG NODE_ENV
# ENV NODE_ENV=${NODE_ENV:-dev}
# # start, start:dev, start:debug, start:prod
# # CMD exec yarn start:${NODE_ENV}
41 changes: 41 additions & 0 deletions src/booking/.env.devdocker
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Environment
NODE_ENV=devdocker

# Service
SERVICE_NAME="BOOKING SERVICE - ONLINE"

# Port number
PORT=3366

# JWT
JWT_SECRET=thisisafakesecretchangeit
JWT_ACCESS_EXPIRATION_MINUTES=30
JWT_REFRESH_EXPIRATION_DAYS=1

# Postgress
POSTGRES_HOST=postgres
POSTGRES_PORT=5432
POSTGRES_USERNAME=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_DATABASE=booking
POSTGRES_SYNCHRONIZE=false
POSTGRES_ENTITIES=/../**/*.entity.{js,ts}
POSTGRES_MIGRATIONS=/../**/data/migrations/*.{js,ts}
POSTGRES_MIGRATIONS_RUN=true

# Rabbitmq
RABBITMQ_HOST=rabbitmq
RABBITMQ_PORT=5672
RABBITMQ_USERNAME=guest
RABBITMQ_PASSWORD=guest
RABBITMQ_EXCHANGE=booking

# RETRY
RETRY_COUNT=3
RETRY_FACTOR=2
RETRY_MIN_TIMEOUT=1000
RETRY_MAX_TIMEOUT=60000

# Monitoring
MONITORING_JAEGER_ENDPOINT=http://jaeger:14268/api/traces
MONITORING_ZIPKIN_ENDPOINT=http://zipkin:9411/api/v2/spans
6 changes: 3 additions & 3 deletions src/booking/.env.development
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_USERNAME=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_Database=booking
POSTGRES_SYNCHRONIZE =false
POSTGRES_DATABASE=booking
POSTGRES_SYNCHRONIZE=false
POSTGRES_ENTITIES=/../**/*.entity.{js,ts}
POSTGRES_MIGRATIONS=/../**/data/migrations/*.{js,ts}
POSTGRES_MIGRATIONS_RUN=true

# Rabbitmq
RABBITMQ_Host=localhost
RABBITMQ_HOST=localhost
RABBITMQ_PORT=5672
RABBITMQ_USERNAME=guest
RABBITMQ_PASSWORD=guest
Expand Down
29 changes: 15 additions & 14 deletions src/booking/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@
"test": "jest"
},
"dependencies": {
"@nestjs/cli": "10.2.1",
"@nestjs/config": "^3.1.1",
"@nestjs/core": "10.2.10",
"@nestjs/cqrs": "^10.2.6",
"@nestjs/cli": "^10.2.1",
"@nestjs/config": "^3.2.2",
"@nestjs/core": "^10.2.1",
"@nestjs/cqrs": "^10.2.7",
"@nestjs/jwt": "^10.2.0",
"@nestjs/mapped-types": "2.0.4",
"@nestjs/microservices": "^10.2.10",
"@nestjs/passport": "^10.0.2",
"@nestjs/platform-express": "^10.2.10",
"@nestjs/schematics": "10.0.3",
"@nestjs/swagger": "^7.1.16",
"@nestjs/terminus": "^10.1.1",
"@nestjs/testing": "10.2.10",
"@nestjs/typeorm": "^10.0.1",
"@nestjs/mapped-types": "^2.0.5",
"@nestjs/microservices": "^10.3.7",
"@nestjs/passport": "^10.0.3",
"@nestjs/platform-express": "^10.3.7",
"@nestjs/schematics": "^10.1.1",
"@nestjs/swagger": "^7.3.1",
"@nestjs/terminus": "^10.2.3",
"@nestjs/testing": "^10.3.7",
"@nestjs/typeorm": "^10.0.2",
"axios": "^1.6.2",
"building-blocks": "file:../building-blocks",
"class-transformer": "^0.5.1",
Expand All @@ -40,7 +40,8 @@
"nodemon": "^3.0.2",
"pg": "^8.11.3",
"testcontainers": "^10.3.2",
"ts-mapper": "^1.2.4"
"ts-mapper": "^1.2.4",
"ts-node": "^10.9.2"
},
"devDependencies": {
"@faker-js/faker": "^8.3.1",
Expand Down
8 changes: 4 additions & 4 deletions src/building-blocks/configs/configs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions src/building-blocks/configs/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const envVarsSchema = Joi.object()
POSTGRES_PASSWORD: Joi.string()
.default('postgres')
.description('Postgres password'),
POSTGRES_Database: Joi.string()
POSTGRES_DATABASE: Joi.string()
.default('default_database')
.description('Postgres database name'),
POSTGRES_SYNCHRONIZE: Joi.boolean()
Expand All @@ -52,7 +52,7 @@ const envVarsSchema = Joi.object()
POSTGRES_MIGRATIONS_RUN: Joi.boolean()
.default(false)
.description('Run migrations after running project'),
RABBITMQ_Host: Joi.string()
RABBITMQ_HOST: Joi.string()
.default('localhost')
.description('Rabbitmq host'),
RABBITMQ_PORT: Joi.number().default(5672).description('Rabbitmq port'),
Expand Down Expand Up @@ -95,7 +95,7 @@ export default {
serviceName: envVars.SERVICE_NAME,
port: envVars.PORT,
rabbitmq: {
host: envVars.RABBITMQ_Host,
host: envVars.RABBITMQ_HOST,
port: envVars.RABBITMQ_PORT,
username: envVars.RABBITMQ_USERNAME,
password: envVars.RABBITMQ_PASSWORD,
Expand All @@ -106,7 +106,7 @@ export default {
port: envVars.POSTGRES_PORT,
username: envVars.POSTGRES_USERNAME,
password: envVars.POSTGRES_PASSWORD,
database: envVars.POSTGRES_Database,
database: envVars.POSTGRES_DATABASE,
synchronize: envVars.POSTGRES_SYNCHRONIZE,
autoLoadEntities: envVars.POSTGRES_AUTO_LOAD_ENTITIES,
entities: envVars.POSTGRES_ENTITIES,
Expand Down
2 changes: 1 addition & 1 deletion src/building-blocks/openTelemetry/open-telemetry.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { OpenTelemetryOptions, OpenTelemetryTracer} from './open-telemetry-trace
provide: 'IOpenTelemetryTracer',
useClass: OpenTelemetryTracer
}],
exports: ['IOpenTelemetryTracer']
exports: ['IOpenTelemetryTracer', OpenTelemetryOptions]
})
export class OpenTelemetryModule implements OnApplicationShutdown {

Expand Down
Loading