Skip to content

Commit

Permalink
Merge pull request #83 from Rocketseat/chore/docker-compose-environme…
Browse files Browse the repository at this point in the history
…nt-variables

chore: set environment variables
  • Loading branch information
gabrielbuzziv authored Aug 5, 2023
2 parents fbaa9f0 + f9fcca0 commit 94601c9
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 29 deletions.
26 changes: 24 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
# App
APP_PORT=3333
APP_NAME=backend-template
APP_VERSION='0.0.1'

JWT_SECRET_KEY='JWT_SECRET_KEY'
JWT_PUBLIC_KEY='JWT_PUBLIC_KEY'

# Database
DATABASE_HOST=localhost
DATABASE_PORT=3306
DATABASE_USER=root
DATABASE_PASS=toor
DATABASE_NAME=app
DATAVASE_ROOT_PASSWORD=toor

DATABASE_URL="mysql://${DATABASE_USER}:${DATABASE_PASS}@${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_NAME}"

JWT_SECRET_KEY='JWT_SECRET_KEY'
JWT_PUBLIC_KEY='JWT_PUBLIC_KEY'
# Redis
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=

# kafka
KAFKA_HOST=localhost
KAFKA_PORT=9092
KAFKA_USER=
KAFKA_PASSWORD=
KAFKA_BROKERS=

# Zookeeper
ZOOKEEPER_HOST=localhost
ZOOKEEPER_PORT=2181
12 changes: 6 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ COPY --from=build /var/app/prisma prisma/
RUN wget -O /var/app/dumb-init https://github.com/Yelp/dumb-init/releases/download/v1.2.5/dumb-init_1.2.5_x86_64 && \
chmod -v +x /var/app/dumb-init
RUN npx prisma generate
RUN npx pkg@5.5.2 . -o $SERVICE_NAME
RUN chmod -v +x /var/app/$SERVICE_NAME
RUN npx pkg@5.5.2 . -o $APP_NAME
RUN chmod -v +x /var/app/$APP_NAME

FROM node:16-alpine AS runtime
ARG DATABASE_URL
ENV DATABASE_URL $DATABASE_URL
ARG VERSION="0.0.1"
ENV VERSION $VERSION
ARG APP_VERSION="0.0.1"
ENV APP_VERSION $APP_VERSION
ARG COMMIT
ENV COMMIT $COMMIT
ENV NODE_ENV production
Expand All @@ -56,6 +56,6 @@ COPY --chown=node:node --from=package /var/app/src /src/
COPY --chown=node:node --from=package /var/app/src src/
# :)
COPY --chown=node:node --from=package /var/app/dumb-init dumb-init
COPY --chown=node:node --from=package /var/app/$SERVICE_NAME $SERVICE_NAME
COPY --chown=node:node --from=package /var/app/$APP_NAME $APP_NAME
ENTRYPOINT ["/var/app/dumb-init", "--"]
CMD ["/var/app/$SERVICE_NAME"]
CMD ["/var/app/$APP_NAME"]
28 changes: 14 additions & 14 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ services:

database:
image: 'bitnami/mysql'
container_name: 'mysql'
container_name: 'mysql-${APP_NAME}'
ports:
- '3306:3306'
- '${DATABASE_PORT}:3306'
environment:
- MYSQL_DATABASE=app
- MYSQL_USER=docker
- MYSQL_PASSWORD=docker
- MYSQL_ROOT_PASSWORD=toor
- MYSQL_DATABASE=${DATABASE_NAME}
- MYSQL_USER=${DATABASE_USER}
- MYSQL_PASSWORD=${DATABASE_PASS}
- MYSQL_ROOT_PASSWORD=${DATAVASE_ROOT_PASSWORD}
volumes:
- 'mysql_data:/bitnami/mysql'
networks:
- app-net

redis:
image: 'bitnami/redis'
container_name: 'redis'
container_name: 'redis-${APP_NAME}'
ports:
- '6379:6379'
- '${REDIS_PORT}:6379'
environment:
- ALLOW_EMPTY_PASSWORD=yes
volumes:
Expand All @@ -32,9 +32,9 @@ services:

zookeeper:
image: 'bitnami/zookeeper:3'
container_name: 'zookeeper'
container_name: 'zookeeper-${APP_NAME}'
ports:
- '2181:2181'
- '${ZOOKEEPER_PORT}:2181'
volumes:
- 'zookeeper_data:/bitnami'
environment:
Expand All @@ -44,15 +44,15 @@ services:

kafka:
image: 'bitnami/kafka:3'
container_name: 'kafka'
container_name: 'kafka-${APP_NAME}'
ports:
- '9092:9092'
- '${KAFKA_PORT}:9092'
volumes:
- 'kafka_data:/bitnami'
environment:
- KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181
- KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:${ZOOKEEPER_PORT}
- KAFKA_CFG_OFFSETS_TOPIC_REPLICATION_FACTOR=1
- KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://localhost:9092
- KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://${KAFKA_HOST}:${KAFKA_PORT}
- ALLOW_PLAINTEXT_LISTENER=yes
depends_on:
- zookeeper
Expand Down
6 changes: 3 additions & 3 deletions src/config/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dotenv/config';

export const PORT = process.env.PORT ?? 3333;
export const PORT = process.env.APP_PORT ?? 3333;
export const ENV = process.env.NODE_ENV ?? 'development';
export const SERVICE = process.env.SERVICE ?? 'backend-template';
export const VERSION = process.env.VERSION ?? '1.0.0';
export const APP_NAME = process.env.APP_NAME ?? 'backend-template';
export const APP_VERSION = process.env.APP_VERSION ?? '1.0.0';
2 changes: 1 addition & 1 deletion src/infra/logger/logger.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const statusText: Record<string, string> = {
imports: [
PinoLoggerModule.forRoot({
pinoHttp: {
name: `${process.env.SERVICE || 'backend'}-logger`,
name: `${process.env.APP_NAME || 'backend'}-logger`,
level: process.env.NODE_ENV !== 'production' ? 'debug' : 'info',
customLogLevel: (res: ServerResponse, err: Error) => {
if (res.statusCode >= 400 && res.statusCode < 500) {
Expand Down
6 changes: 3 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { LoggerErrorInterceptor } from 'nestjs-pino';
import { LoggerService } from '@infra/logger/logger.service';

import { AppModule } from './app.module';
import { ENV, PORT, SERVICE, VERSION } from './config/app';
import { ENV, PORT, APP_NAME, APP_VERSION } from './config/app';

async function bootstrap() {
const app = await NestFactory.create(AppModule, {
Expand Down Expand Up @@ -35,9 +35,9 @@ async function bootstrap() {
}

tracer.init({
service: SERVICE,
service: APP_NAME,
env: ENV,
version: VERSION,
version: APP_VERSION,
runtimeMetrics: true,
logInjection: true,
});
Expand Down

0 comments on commit 94601c9

Please sign in to comment.