skip to Main Content

I am running a nestjs project through docker which connects to redis and mysql
I am facing connection with with docker mysql and prisma service and also with redis
I have gone through lot of links but no luck.

Here is my code:
Dockerfile

    # ******** DEVELOPMENT ********
FROM node:14 AS development

RUN npm install --global pnpm

ENV NODE_ENV=development

WORKDIR /usr/src/app

COPY package.json pnpm-lock.yaml prisma ./

RUN pnpm install --frozen-lockfile --strict-peer-dependencies --reporter append-only --unsafe-perm

COPY . .

RUN pnpm build

docker-compose.yml

version: '3.8'

services:
  main:
    container_name: nestproject
    build:
      context: .
      target: development
    volumes:
    - .:/usr/src/app
    - /usr/src/app/node_modules
    ports:
    - 3000:3000
    command: pnpm start:dev
    env_file:
      - .env
   

  mysql:
    platform: linux/amd64
    image: mysql:8
    volumes:
      - nestproject-mysql-data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: mydb

  redis:
    image: 'redis:5.0'
    volumes:
      - /data
    ports:
      - '6379'
    networks:
      - redis


networks:
  nestproject:
  redis:
    driver: bridge

volumes:
  nestproject-mysql-data:

This is how I am connecting through my prisma client

const { url } = this.configService.get('database');

this.write_client = new PrismaClient({ datasources: { db: { url: url } } });

//url="mysql://root:root@localhost:3306/mydb"

The error which I get for Redis

{"name":"NestProject - RedisService","hostname":"1b080bc9c160","pid":36,"level":50,"err":{"message":"connect ECONNREFUSED 127.0.0.1:6379","name":"Error","stack":"Error: connect ECONNREFUSED 127.0.0.1:6379n    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1159:16)n    at TCPConnectWrap.callbackTrampoline (internal/async_hooks.js:130:17)","code":"ECONNREFUSED"},"msg":"Redis Client Error","time":"2022-06-04T12:19:34.854Z","v":0}

The error which I get for mysql

{"name":"NestProject - ExceptionHandler","hostname":"1b080bc9c160","pid":36,"level":50,"msg":"Error: Can't reach database server at `localhost`:`3306`nnPlease make sure your database server is running at `localhost`:`3306`.n    at /usr/src/app/node_modules/.pnpm/@[email protected][email protected]/node_modules/@prisma/client/runtime/index.js:36270:21","time":"2022-06-04T12:19:34.868Z","v":0}

I don’t understand what I am missing.

2

Answers


  1. Chosen as BEST ANSWER

    Yes I got it.

    The problem was with the mysql url which I was using to connect.

    The correct format

    url="mysql://username:password@mysql:3306/database_name"
    

    We have to use mysql instead of localhost since my service name is mysql

    And regarding the redis issue I got the hint from @alex067 and I removed the

    redis:
        driver: bridge
    

    Thanks alex067


  2. You’re not exposing any ports for your mysql docker container.

    As for your redis issue, you’re creating a new bridged network called "redis." Bridged networks allow containers within the same network, to communicate.

    Your main service is not part of this bridged network, thus the connection is being refused.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search