Skip to content

Commit

Permalink
chore: add Dockerfile & docker-compose for local dev
Browse files Browse the repository at this point in the history
  • Loading branch information
domingo1021 committed Jun 30, 2024
1 parent fbe503e commit f641bf1
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 10 deletions.
20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Build stage
FROM node:20.15.0-alpine as builder
WORKDIR /app
COPY ["package.json", "package-lock.json", "./"]
COPY ["tsconfig.json", "tsconfig.build.json", "./"]
COPY config ./config
COPY src ./src
RUN npm install &&\
npm run build

# Development stage
FROM node:20.15.0-alpine as development
ENV NODE_ENV=production
WORKDIR /app
COPY --from=builder /app/config ./config
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/package*.json ./
RUN npm install --only=production
CMD node ./dist/main.js

4 changes: 2 additions & 2 deletions config/default.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"API_REDIS_HOST": "localhost",
"API_REDIS_PORT": 6379
"REDIS_HOST": "localhost",
"REDIS_PORT": 6379
}
4 changes: 4 additions & 0 deletions config/docker.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"REDIS_HOST": "hero-cache",
"REDIS_PORT": 6379
}
4 changes: 2 additions & 2 deletions config/test.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"API_REDIS_HOST": "localhost",
"API_REDIS_PORT": 6379
"REDIS_HOST": "localhost",
"REDIS_PORT": 6379
}
30 changes: 30 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
version: '3.8'
services:
app:
build: .
image: hero-app
container_name: hero-app
environment:
- NODE_ENV=docker
ports:
- 3000:3000
depends_on:
- redis
redis:
image: redis:7.2.5-alpine
container_name: hero-cache
ports:
- 6379:6379
vault:
image: vault:1.13.3
container_name: hero-vault
volumes:
- ./config:/vault/config
ports:
- 8200:8200
cap_add:
- IPC_LOCK
environment:
- VAULT_ADDR=http://localhost:8200
- VAULT_TOKEN=myroot
command: server -config=/vault/config
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"prepare": "husky install",
"postinstall": "if [ \"$NODE_ENV\" != \"production\" ]; then husky install; fi",
"lint": "lint-staged",
"test": "jest"
},
Expand Down
8 changes: 3 additions & 5 deletions src/modules/cache/cache.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,15 @@ import { CacheService } from './cache.service';
inject: [ConfigService],
useFactory: async (configService: ConfigService) => {
const DEFAULT_REDIS_HOST = 'localhost';
const DEFAULT_REDIS_PORT = 3000;
const DEFAULT_REDIS_PORT = 6379;

return {
store: await redisStore({
socket: {
host:
configService.get<string>('API_REDIS_HOST') ||
DEFAULT_REDIS_HOST,
configService.get<string>('REDIS_HOST') || DEFAULT_REDIS_HOST,
port: +(
configService.get<number>('API_REDIS_PORT') ||
DEFAULT_REDIS_PORT
configService.get<number>('REDIS_PORT') || DEFAULT_REDIS_PORT
),
},
}),
Expand Down

0 comments on commit f641bf1

Please sign in to comment.