Skip to content

Commit

Permalink
feat: update prod docker compose impl
Browse files Browse the repository at this point in the history
  • Loading branch information
aseerkt committed Aug 6, 2024
1 parent 7dd8cb5 commit 4469e92
Show file tree
Hide file tree
Showing 16 changed files with 95 additions and 100 deletions.
7 changes: 7 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
node_modules
dist
*.md
.git
.gitignore
.prettierrc
.env
38 changes: 38 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
FROM node:20-slim AS base
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable

FROM base AS build
WORKDIR /usr/src/app

COPY pnpm-workspace.prod.yaml pnpm-workspace.yaml
COPY pnpm-lock.yaml .

COPY server/package.json server/package.json
COPY web/package.json web/package.json

RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile

COPY . .
COPY server/.env.docker server/.env
COPY web/.env.docker web/.env

RUN NODE_ENV=production pnpm run -r build

RUN pnpm deploy --filter=server --prod /prod/server --ignore-scripts
RUN mv ./web /prod/web

FROM base AS app
WORKDIR /prod/app

COPY --from=build /prod/server .
COPY --from=build /prod/web/dist ./web/dist

RUN pnpm swag:gen

ENV NODE_ENV production

EXPOSE 5000

CMD [ "pnpm", "start" ]
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,12 @@
```bash
pnpm docker:up
```
> Make sure ports `6379`,`5432`,`5000` and `3000` are not occupied
- Run migrations
```bash
pnpm docker:db:migrate
```
> Make sure the ports `6379`,`5432` and `5000` are not occupied
- Seed database
```bash
pnpm docker:db:seed
```
- Go to [http://localhost:3000](http://localhost:3000)
- Go to [http://localhost:5000](http://localhost:5000)

- Stop docker containers
```bash
Expand Down
23 changes: 4 additions & 19 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3.8'

services:
db:
image: postgres:16-alpine
Expand Down Expand Up @@ -29,26 +27,13 @@ services:
timeout: 10s
retries: 5

web:
build:
context: ./web
dockerfile: Dockerfile
ports:
- '3000:3000'
environment:
VITE_BACKEND_URL: http://localhost:5000
depends_on:
- server

server:
build:
context: ./server
dockerfile: Dockerfile
container_name: mchat-server
app:
build: .
container_name: mchat-app
ports:
- '5000:5000'
depends_on:
db:
condition: service_healthy
redis:
dragonfly:
condition: service_healthy
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"prepare": "husky install",
"commitlint": "commitlint --edit",
"docker:up": "docker compose -f docker-compose.prod.yml up --build -d",
"docker:db:migrate": "docker exec -it mchat-server pnpm migrate:run",
"docker:db:seed": "docker exec -it mchat-server pnpm seed",
"docker:db:migrate": "docker exec -it mchat-app pnpm migrate:run",
"docker:db:seed": "docker exec -it mchat-app pnpm seed",
"docker:down": "docker compose -f docker-compose.prod.yml down",
"e2e:test": "pnpm --filter e2e test"
},
Expand All @@ -34,5 +34,6 @@
"workspaces": [
"client",
"server"
]
],
"packageManager": "[email protected]+sha512.38dc6fba8dba35b39340b9700112c2fe1e12f10b17134715a4aa98ccf7bb035e76fd981cf0bb384dfa98f8d6af5481c2bef2f4266a24bfa20c34eb7147ce0b5e"
}
14 changes: 11 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pnpm-workspace.prod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
packages:
- 'web'
- 'server'
3 changes: 0 additions & 3 deletions server/.dockerignore

This file was deleted.

21 changes: 0 additions & 21 deletions server/Dockerfile

This file was deleted.

17 changes: 9 additions & 8 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
"description": "mChat server implementation",
"private": true,
"scripts": {
"start": "tsx src/index.ts",
"start": "node dist/index.js",
"dev": "tsx watch src/index.ts",
"build": "tsc",
"lint": "tsc && eslint src/**/*.ts",
"migrate:gen": "drizzle-kit generate",
"migrate:run": "tsx src/scripts/migrate.ts",
"seed": "tsx src/scripts/seed.ts",
"swag:gen": "tsx src/scripts/swagger.ts"
"migrate:run": "node dist/scripts/migrate.js",
"seed": "node dist/scripts/seed.js",
"swag:gen": "node dist/scripts/swagger.js"
},
"keywords": [
"socket.io",
Expand All @@ -33,16 +33,18 @@
"ioredis": "^5.4.1",
"jsonwebtoken": "^9.0.2",
"lodash": "^4.17.21",
"module-alias": "^2.2.3",
"morgan": "^1.10.0",
"pg": "^8.12.0",
"socket.io": "^4.7.5",
"swagger-autogen": "^2.23.7",
"swagger-ui-express": "^5.0.1",
"uuid": "^10.0.0"
"uuid": "^10.0.0",
"@faker-js/faker": "^8.4.1"
},
"devDependencies": {
"drizzle-kit": "^0.22.8",
"@eslint/js": "^9.6.0",
"@faker-js/faker": "^8.4.1",
"@types/cookie-parser": "^1.4.7",
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
Expand All @@ -53,14 +55,13 @@
"@types/pg": "^8.11.6",
"@types/swagger-ui-express": "^4.1.6",
"@types/uuid": "^10.0.0",
"drizzle-kit": "^0.22.8",
"eslint": "9.x",
"globals": "^15.7.0",
"tsx": "^4.15.6",
"typescript": "^5.5.3",
"typescript-eslint": "^7.15.0"
},
"_moduleAliases": {
"@": "src"
"@": "dist"
}
}
15 changes: 11 additions & 4 deletions server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import 'module-alias/register'

import { createAdapter } from '@socket.io/redis-streams-adapter'
import 'colors'
import cookieParser from 'cookie-parser'
import cors from 'cors'
import { migrate } from 'drizzle-orm/node-postgres/migrator'
import express from 'express'
import helmet from 'helmet'
import morgan from 'morgan'
Expand All @@ -10,7 +13,7 @@ import path from 'node:path'
import { Server } from 'socket.io'
import swaggerUi from 'swagger-ui-express'
import { config } from './config'
import { connectDB } from './database'
import { connectDB, db } from './database'
import { errorHandler } from './middlewares'
import { getRedisClient } from './redis'
import rootRouter from './routes'
Expand All @@ -26,6 +29,9 @@ import swaggerDocument from './swagger-output.json'

const createApp = async () => {
await connectDB()

await migrate(db, { migrationsFolder: './drizzle' })

const redisClient = getRedisClient()

const app = express()
Expand Down Expand Up @@ -65,9 +71,10 @@ const createApp = async () => {
app.use(errorHandler)

if (config.isProd) {
app.use(express.static(path.join('../../web/dist')))
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '../../web/dist/index.html'))
const webStaticPath = path.join(__dirname, '..', 'web', 'dist')
app.use(express.static(webStaticPath))
app.get('*', (_req, res) => {
res.sendFile(path.join(webStaticPath, 'index.html'))
})
}

Expand Down
2 changes: 2 additions & 0 deletions server/src/scripts/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'module-alias/register'

import 'colors'
import { migrate } from 'drizzle-orm/node-postgres/migrator'
import { client, connectDB, db } from '../database'
Expand Down
2 changes: 2 additions & 0 deletions server/src/scripts/seed.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'module-alias/register'

import { connectDB, db } from '@/database'
import { NewGroup, groupsTable } from '@/modules/groups/groups.schema'
import { NewMember, membersTable } from '@/modules/members/members.schema'
Expand Down
2 changes: 2 additions & 0 deletions server/src/scripts/swagger.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'module-alias/register'

import swaggerAutogen from 'swagger-autogen'

const doc = {
Expand Down
2 changes: 0 additions & 2 deletions web/.dockerignore

This file was deleted.

31 changes: 0 additions & 31 deletions web/Dockerfile

This file was deleted.

0 comments on commit 4469e92

Please sign in to comment.