Skip to content

Commit e2ffe33

Browse files
committed
Dockerized the backend, closes #33
- Integrated Redis and Express into a single container using supervisord for cost optimization ⚠️ WARNING: Running Redis and Node.js in the same container saves costs but may reduce scalability and fault isolation.
1 parent ff2d603 commit e2ffe33

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed

tyche-backend/.dockerignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Ignore node_modules and other dependencies
2+
node_modules
3+
npm-debug.log*
4+
yarn-debug.log*
5+
yarn-error.log*
6+
7+
# Ignore Docker-related files
8+
Dockerfile
9+
docker-compose.yml
10+
11+
# Ignore environment variables
12+
.env
13+
14+
# Ignore Git files
15+
.git
16+
.gitignore
17+
18+
# Ignore logs
19+
logs
20+
*.log
21+
22+
# Ignore OS-specific files
23+
.DS_Store
24+
Thumbs.db

tyche-backend/Dockerfile

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# tyche-backend/Dockerfile
2+
3+
# Use an official Node.js runtime as a parent image
4+
FROM node:18-alpine
5+
6+
# Install dependencies for building and Redis
7+
RUN apk add --no-cache python3 make g++ redis supervisor
8+
9+
# Set environment variables
10+
ENV NODE_ENV=production
11+
12+
# Set working directory
13+
WORKDIR /usr/src/app
14+
15+
# Copy package.json and package-lock.json first for better caching
16+
COPY package*.json ./
17+
18+
# Install dependencies
19+
RUN npm install --only=production
20+
21+
# Remove build dependencies to reduce image size
22+
RUN apk del python3 make g++
23+
24+
# Copy the rest of the application code
25+
COPY . .
26+
27+
# Copy supervisord configuration
28+
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
29+
30+
# Expose the ports the app and Redis run on
31+
EXPOSE 5005 6379
32+
33+
# Define the command to run supervisord
34+
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]

tyche-backend/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,14 @@ For the initial run, execute `pnpm install`, then:
3838
```bash
3939
pnpm run dev
4040
```
41+
42+
#### Alternative: Running with Docker
43+
44+
- Install [docker](https://www.docker.com/products/docker-desktop/) and run it.
45+
- In `backend` directory:
46+
47+
```bash
48+
docker compose up --build
49+
```
50+
51+
- Backend will be running on `http://localhost:5005/`

tyche-backend/docker-compose.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
version: '3.8'
2+
3+
services:
4+
app:
5+
build: .
6+
container_name: tyche-backend
7+
restart: unless-stopped
8+
ports:
9+
- "5005:5005"
10+
- "6379:6379"
11+
environment:
12+
# Application environment variables
13+
PORT: 5005
14+
NODE_ENV: development
15+
REDIS_URL: redis://localhost:6379
16+
env_file:
17+
- .env
18+
networks:
19+
- tyche-network
20+
21+
networks:
22+
tyche-network:
23+
driver: bridge

tyche-backend/supervisord.conf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[supervisord]
2+
nodaemon=true
3+
4+
[program:redis]
5+
command=redis-server --appendonly yes
6+
autorestart=true
7+
stdout_logfile=/var/log/redis.log
8+
stderr_logfile=/var/log/redis.err
9+
10+
[program:app]
11+
command=npm start
12+
autorestart=true
13+
stdout_logfile=/var/log/app.log
14+
stderr_logfile=/var/log/app.err

0 commit comments

Comments
 (0)