Skip to content

Commit 102c745

Browse files
authored
Feat/docker
* add docker and docker-compose file * docs: 📝 add docker related information
1 parent a050461 commit 102c745

File tree

8 files changed

+196
-4
lines changed

8 files changed

+196
-4
lines changed

.dockerignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
node_modules
2+
dist
3+
.gitignore
4+
.env
5+
Dockerfile
6+
REAME.md
7+
.github
8+
bin
9+
coverage
10+
*.log
11+
.DS_Store
12+
docker-compose.yml

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
DB_HOST=localhost
22
DB_NAME=name
33
DB_PASSWORD=password
4-
DB_PORT=3306
4+
DB_PORT=5432
55
DB_TYPE=postgres
66
DB_USER=username
77
PORT=5000

.github/workflows/npm-publish.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ jobs:
1414
with:
1515
node-version: 20
1616
registry-url: https://registry.npmjs.org/
17-
- run: npm ci
1817
- run: npm publish
1918
env:
2019
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

Dockerfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
FROM node:20-alpine AS builder
2+
WORKDIR /app
3+
COPY package*.json ./
4+
RUN npm install
5+
COPY . .
6+
RUN npm run build
7+
8+
FROM node:20-alpine
9+
WORKDIR /app
10+
COPY --from=builder /app/dist ./dist
11+
COPY package*.json ./
12+
# install postgres dependencies
13+
RUN npm install pg pg-hstore
14+
RUN npm install --only=production
15+
ENV NODE_ENV=production
16+
COPY . .
17+
EXPOSE 5000
18+
CMD ["npm", "start"]

README.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ A boilerplate/starter project for quickly building RESTful APIs using Node.js,Ty
1919
- [Manual Installation](#manual-installation)
2020
- [Getting started](#getting-started)
2121
- [For development](#for-development)
22+
- [Docker Setup](#docker-setup)
23+
- [Using Docker Compose (Recommended)](#using-docker-compose-recommended)
24+
- [Using Docker Only](#using-docker-only)
2225
- [Sample .ENV](#sample-env)
2326
- [Commands](#commands)
2427
- [Project Structure](#project-structure)
@@ -84,6 +87,113 @@ npm run dev
8487

8588
```
8689

90+
## Docker Setup
91+
92+
### Using Docker Compose (Recommended)
93+
94+
The easiest way to run the application with Docker is using Docker Compose, which will set up both the Node.js application and PostgreSQL database.
95+
96+
1. **Create a `.env` file** in the project root:
97+
98+
```bash
99+
# Database Configuration
100+
DB_HOST=postgres
101+
DB_PORT=5432
102+
DB_TYPE=postgres
103+
DB_NAME=myapp
104+
DB_USER=postgres
105+
DB_PASSWORD=password
106+
107+
# JWT Configuration
108+
SECRET=your-secret-key-change-this-in-production
109+
TOKEN_EXPIRY_HOUR=24
110+
111+
# Email Configuration
112+
EMAIL_SERVICE=gmail
113+
EMAIL_USER=your-email@gmail.com
114+
EMAIL_PASS=your-email-password
115+
EMAIL_FROM=your-email@gmail.com
116+
117+
# OTP Configuration
118+
OTP_EXPIRY_MIN=5
119+
OTP_SECRET=your-otp-secret-change-this-in-production
120+
```
121+
122+
2. **Build and run the containers**:
123+
124+
```bash
125+
# Build and start all services
126+
docker compose up --build
127+
128+
# Run in detached mode (background)
129+
docker compose up -d --build
130+
131+
# Stop the services
132+
docker compose down
133+
134+
```
135+
136+
3. **Access the application**:
137+
- **API**: http://localhost:5000/api/
138+
- **Swagger Documentation**: http://localhost:5000/api/v1/docs/
139+
- **Database**: PostgreSQL on localhost:5432
140+
141+
### Using Docker Only
142+
143+
If you prefer to run only the application container and connect to an external database:
144+
145+
1. **Build the Docker image**:
146+
147+
```bash
148+
docker build -t typescript-node-app .
149+
```
150+
151+
2. **Run the container**:
152+
153+
```bash
154+
docker run -p 5000:5000 \
155+
-e DB_HOST=your-db-host \
156+
-e DB_PORT=5432 \
157+
-e DB_TYPE=postgres \
158+
-e DB_NAME=your-db-name \
159+
-e DB_USER=your-db-user \
160+
-e DB_PASSWORD=your-db-password \
161+
-e SECRET=your-secret-key \
162+
-e TOKEN_EXPIRY_HOUR=24 \
163+
-e EMAIL_SERVICE=gmail \
164+
-e EMAIL_USER=your-email@gmail.com \
165+
-e EMAIL_PASS=your-email-password \
166+
-e EMAIL_FROM=your-email@gmail.com \
167+
-e OTP_EXPIRY_MIN=5 \
168+
-e OTP_SECRET=your-otp-secret \
169+
typescript-node-app
170+
```
171+
172+
173+
### Docker Commands
174+
175+
```bash
176+
# Build the image
177+
docker build -t typescript-node-app .
178+
179+
# Run container
180+
docker run -p 5000:5000 typescript-node-app
181+
182+
# Run with environment file
183+
docker run -p 5000:5000 --env-file .env typescript-node-app
184+
185+
# View running containers
186+
docker ps
187+
188+
# View logs
189+
docker logs <container-id>
190+
191+
# Stop container
192+
docker stop <container-id>
193+
194+
```
195+
196+
87197
## Sample .ENV
88198
```sh
89199
DB_HOST=localhost
@@ -128,6 +238,9 @@ npm run lint
128238
# format files
129239
npm run format
130240

241+
# Docker commands
242+
docker compose up --build
243+
docker compose down
131244
```
132245

133246

@@ -149,6 +262,9 @@ src\
149262
|--validations\ # Request data validation schemas
150263
|--app.ts\ # Express app
151264
|--server.ts\ # App entry point
265+
docker-compose.yml # Docker Compose configuration
266+
Dockerfile # Docker image definition
267+
.env # Environment variables
152268
```
153269
## Changing Database
154270

docker-compose.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
version: '3.8'
2+
3+
services:
4+
app:
5+
build: .
6+
ports:
7+
- "5000:5000"
8+
env_file:
9+
- .env
10+
environment:
11+
- NODE_ENV=production
12+
# Database Configuration
13+
- DB_HOST=${DB_HOST}
14+
- DB_PORT=${DB_PORT}
15+
- DB_TYPE=${DB_TYPE}
16+
- DB_NAME=${DB_NAME}
17+
- DB_USER=${DB_USER}
18+
- DB_PASSWORD=${DB_PASSWORD}
19+
# JWT Configuration
20+
- SECRET=${SECRET}
21+
- TOKEN_EXPIRY_HOUR=${TOKEN_EXPIRY_HOUR}
22+
# Email Configuration
23+
- EMAIL_SERVICE=${EMAIL_SERVICE}
24+
- EMAIL_USER=${EMAIL_USER}
25+
- EMAIL_PASS=${EMAIL_PASS}
26+
- EMAIL_FROM=${EMAIL_FROM}
27+
# OTP Configuration
28+
- OTP_EXPIRY_MIN=${OTP_EXPIRY_MIN}
29+
- OTP_SECRET=${OTP_SECRET}
30+
# Port Configuration
31+
- PORT=5000
32+
depends_on:
33+
- postgres
34+
35+
postgres:
36+
image: postgres:16.0-alpine3.17
37+
ports:
38+
- "5432:5432"
39+
environment:
40+
- POSTGRES_USER=${DB_USER}
41+
- POSTGRES_PASSWORD=${DB_PASSWORD}
42+
- POSTGRES_DB=${DB_NAME}
43+
volumes:
44+
- postgres_data:/var/lib/postgresql/data
45+
46+
volumes:
47+
postgres_data:

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nabadeep25/create-ts-node-app",
3-
"version": "1.0.2",
3+
"version": "1.1.0",
44
"description": "A boilerplate/starter project for quickly building RESTful APIs using Node.js,Typescript, Express, and Sequelize.",
55
"author": "Nabadeep Thakuria",
66
"bin": "bin/cli.js",

src/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ if (process.env.NODE_ENV === "development") {
1111
/**
1212
* Start Express server.
1313
*/
14-
const server = app.listen(app.get("port"), () => {
14+
const server = app.listen(app.get("port"),"0.0.0.0",() => {
1515
console.log(
1616
" App is running at http://localhost:%d in %s mode",
1717
app.get("port"),

0 commit comments

Comments
 (0)