From caf6e771f8d39a31f31895258e22434c92fe0b2a Mon Sep 17 00:00:00 2001 From: Nabil Ould Hamou Date: Tue, 17 Dec 2024 17:43:15 +0100 Subject: [PATCH] wip: final docker compose --- Dockerfile | 13 +++++++++++++ docker-compose.yml | 25 ++++++++++++++++++++----- docker_entrypoint.sh | 12 ++++++++++++ prisma/schema.prisma | 1 + src/lib/redisClient.ts | 9 +++++++-- 5 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 Dockerfile create mode 100644 docker_entrypoint.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..afad1b4 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +# Build stage +FROM node:18.18-alpine + +RUN npm i -g pnpm + +EXPOSE 3000 +ENV NODE_ENV=production + +COPY . /app + +WORKDIR /app + +CMD docker_entrypoint.sh \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 0e5d47c..ae600c8 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,19 @@ services: + app: + build: . + hostname: chat-app + restart: always + environment: + - DATABASE_URL=mongodb://temp-root-username:temp-password@mongodb:27017/chat_projetweb + - JWT_SECRET=1f49ba5426afebd6e27eed416d0c31925ca5c4dc39ab4fdea514c8a858312608 + ports: + - "3000:3000" + networks: + - app_network + depends_on: + - mongodb + - redis + mongodb: build: ./mongodb_rs hostname: mongodb @@ -15,11 +30,11 @@ services: - mongo-data:/data/db/ - mongo-logs:/var/log/mongodb/ networks: - - mongodb_network + - app_network redis: image: redis:latest - hostname: redis + hostname: redis-server restart: always ports: - "6379:6379" @@ -29,11 +44,11 @@ services: volumes: - redis-data:/root/redis networks: - - redis_network + - app_network networks: - mongodb_network: - redis_network: + app_network: + driver: bridge volumes: mongo-data: diff --git a/docker_entrypoint.sh b/docker_entrypoint.sh new file mode 100644 index 0000000..b45081e --- /dev/null +++ b/docker_entrypoint.sh @@ -0,0 +1,12 @@ +#!/bin/sh +set -xe + +pnpm install +pnpm prisma generate + +pnpm run build +pnpm prune --prod + +pnpx prisma db push + +node build/index.js \ No newline at end of file diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 6045648..7a6bdd8 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -6,6 +6,7 @@ generator client { provider = "prisma-client-js" + binaryTargets = ["debian-openssl-1.1.x", "linux-musl-openssl-3.0.x"] } datasource db { diff --git a/src/lib/redisClient.ts b/src/lib/redisClient.ts index 28b31bf..62fec25 100644 --- a/src/lib/redisClient.ts +++ b/src/lib/redisClient.ts @@ -1,11 +1,16 @@ import { createClient } from 'redis'; const client = await createClient({ - url: process.env.REDIS_URL || 'redis://localhost:6379' + url: process.env.REDIS_URL || 'redis://redis-server:6379' }); + client.on('error', (err) => console.error('Redis Error:', err)); -await client.connect(); +try { + await client.connect(); +} catch (error) { + console.error('Redis Error:', error); +} export default client;