Skip to content

Commit 94601c9

Browse files
Merge pull request #83 from Rocketseat/chore/docker-compose-environment-variables
chore: set environment variables
2 parents fbaa9f0 + f9fcca0 commit 94601c9

File tree

6 files changed

+51
-29
lines changed

6 files changed

+51
-29
lines changed

.env.example

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
1+
# App
2+
APP_PORT=3333
3+
APP_NAME=backend-template
4+
APP_VERSION='0.0.1'
5+
6+
JWT_SECRET_KEY='JWT_SECRET_KEY'
7+
JWT_PUBLIC_KEY='JWT_PUBLIC_KEY'
8+
19
# Database
210
DATABASE_HOST=localhost
311
DATABASE_PORT=3306
412
DATABASE_USER=root
513
DATABASE_PASS=toor
614
DATABASE_NAME=app
15+
DATAVASE_ROOT_PASSWORD=toor
716

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

10-
JWT_SECRET_KEY='JWT_SECRET_KEY'
11-
JWT_PUBLIC_KEY='JWT_PUBLIC_KEY'
19+
# Redis
20+
REDIS_HOST=localhost
21+
REDIS_PORT=6379
22+
REDIS_PASSWORD=
23+
24+
# kafka
25+
KAFKA_HOST=localhost
26+
KAFKA_PORT=9092
27+
KAFKA_USER=
28+
KAFKA_PASSWORD=
29+
KAFKA_BROKERS=
30+
31+
# Zookeeper
32+
ZOOKEEPER_HOST=localhost
33+
ZOOKEEPER_PORT=2181

Dockerfile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ COPY --from=build /var/app/prisma prisma/
3838
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 && \
3939
chmod -v +x /var/app/dumb-init
4040
RUN npx prisma generate
41-
RUN npx pkg@5.5.2 . -o $SERVICE_NAME
42-
RUN chmod -v +x /var/app/$SERVICE_NAME
41+
RUN npx pkg@5.5.2 . -o $APP_NAME
42+
RUN chmod -v +x /var/app/$APP_NAME
4343

4444
FROM node:16-alpine AS runtime
4545
ARG DATABASE_URL
4646
ENV DATABASE_URL $DATABASE_URL
47-
ARG VERSION="0.0.1"
48-
ENV VERSION $VERSION
47+
ARG APP_VERSION="0.0.1"
48+
ENV APP_VERSION $APP_VERSION
4949
ARG COMMIT
5050
ENV COMMIT $COMMIT
5151
ENV NODE_ENV production
@@ -56,6 +56,6 @@ COPY --chown=node:node --from=package /var/app/src /src/
5656
COPY --chown=node:node --from=package /var/app/src src/
5757
# :)
5858
COPY --chown=node:node --from=package /var/app/dumb-init dumb-init
59-
COPY --chown=node:node --from=package /var/app/$SERVICE_NAME $SERVICE_NAME
59+
COPY --chown=node:node --from=package /var/app/$APP_NAME $APP_NAME
6060
ENTRYPOINT ["/var/app/dumb-init", "--"]
61-
CMD ["/var/app/$SERVICE_NAME"]
61+
CMD ["/var/app/$APP_NAME"]

docker-compose.yml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@ services:
55

66
database:
77
image: 'bitnami/mysql'
8-
container_name: 'mysql'
8+
container_name: 'mysql-${APP_NAME}'
99
ports:
10-
- '3306:3306'
10+
- '${DATABASE_PORT}:3306'
1111
environment:
12-
- MYSQL_DATABASE=app
13-
- MYSQL_USER=docker
14-
- MYSQL_PASSWORD=docker
15-
- MYSQL_ROOT_PASSWORD=toor
12+
- MYSQL_DATABASE=${DATABASE_NAME}
13+
- MYSQL_USER=${DATABASE_USER}
14+
- MYSQL_PASSWORD=${DATABASE_PASS}
15+
- MYSQL_ROOT_PASSWORD=${DATAVASE_ROOT_PASSWORD}
1616
volumes:
1717
- 'mysql_data:/bitnami/mysql'
1818
networks:
1919
- app-net
2020

2121
redis:
2222
image: 'bitnami/redis'
23-
container_name: 'redis'
23+
container_name: 'redis-${APP_NAME}'
2424
ports:
25-
- '6379:6379'
25+
- '${REDIS_PORT}:6379'
2626
environment:
2727
- ALLOW_EMPTY_PASSWORD=yes
2828
volumes:
@@ -32,9 +32,9 @@ services:
3232

3333
zookeeper:
3434
image: 'bitnami/zookeeper:3'
35-
container_name: 'zookeeper'
35+
container_name: 'zookeeper-${APP_NAME}'
3636
ports:
37-
- '2181:2181'
37+
- '${ZOOKEEPER_PORT}:2181'
3838
volumes:
3939
- 'zookeeper_data:/bitnami'
4040
environment:
@@ -44,15 +44,15 @@ services:
4444

4545
kafka:
4646
image: 'bitnami/kafka:3'
47-
container_name: 'kafka'
47+
container_name: 'kafka-${APP_NAME}'
4848
ports:
49-
- '9092:9092'
49+
- '${KAFKA_PORT}:9092'
5050
volumes:
5151
- 'kafka_data:/bitnami'
5252
environment:
53-
- KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181
53+
- KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:${ZOOKEEPER_PORT}
5454
- KAFKA_CFG_OFFSETS_TOPIC_REPLICATION_FACTOR=1
55-
- KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://localhost:9092
55+
- KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://${KAFKA_HOST}:${KAFKA_PORT}
5656
- ALLOW_PLAINTEXT_LISTENER=yes
5757
depends_on:
5858
- zookeeper

src/config/app.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'dotenv/config';
22

3-
export const PORT = process.env.PORT ?? 3333;
3+
export const PORT = process.env.APP_PORT ?? 3333;
44
export const ENV = process.env.NODE_ENV ?? 'development';
5-
export const SERVICE = process.env.SERVICE ?? 'backend-template';
6-
export const VERSION = process.env.VERSION ?? '1.0.0';
5+
export const APP_NAME = process.env.APP_NAME ?? 'backend-template';
6+
export const APP_VERSION = process.env.APP_VERSION ?? '1.0.0';

src/infra/logger/logger.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ const statusText: Record<string, string> = {
6767
imports: [
6868
PinoLoggerModule.forRoot({
6969
pinoHttp: {
70-
name: `${process.env.SERVICE || 'backend'}-logger`,
70+
name: `${process.env.APP_NAME || 'backend'}-logger`,
7171
level: process.env.NODE_ENV !== 'production' ? 'debug' : 'info',
7272
customLogLevel: (res: ServerResponse, err: Error) => {
7373
if (res.statusCode >= 400 && res.statusCode < 500) {

src/main.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { LoggerErrorInterceptor } from 'nestjs-pino';
66
import { LoggerService } from '@infra/logger/logger.service';
77

88
import { AppModule } from './app.module';
9-
import { ENV, PORT, SERVICE, VERSION } from './config/app';
9+
import { ENV, PORT, APP_NAME, APP_VERSION } from './config/app';
1010

1111
async function bootstrap() {
1212
const app = await NestFactory.create(AppModule, {
@@ -35,9 +35,9 @@ async function bootstrap() {
3535
}
3636

3737
tracer.init({
38-
service: SERVICE,
38+
service: APP_NAME,
3939
env: ENV,
40-
version: VERSION,
40+
version: APP_VERSION,
4141
runtimeMetrics: true,
4242
logInjection: true,
4343
});

0 commit comments

Comments
 (0)