Home > front end >  Second node.js can't connect to mysql in docker compose
Second node.js can't connect to mysql in docker compose

Time:11-17

At the moment, I'm trying to set up my docker-compose file to run multiple (two) Node.js API's. The first Node.js server connects fine to the database.

The second Node.js server keeps throwing this error original: Error: connect ECONNREFUSED 172.29.0.3:3307

So my question is how to run multiple Node.js API's in docker?

This is my docker-compose.yaml:

version: '3.7'

services:
  ISAAC-floor-database:
    image: mysql
    environment:
      MYSQL_ROOT_PASSWORD: isaac
    ports:
      - "3306:3306"
    volumes:
      - ./sql-scripts:/docker-entrypoint-initdb.d

  ISAAC-sensor-database:
    image: mysql
    environment:
      MYSQL_ROOT_PASSWORD: isaac
    ports:
      - "3307:3307"
    volumes:
      - ./sql-scripts:/docker-entrypoint-initdb.d

  ISAAC-floor-back-end:
    image: jjuless/isaac-floor-back-end
    environment:
      DB_HOST: ISAAC-floor-database
      DB_USER: root
      DB_PASSWORD: isaac
      DB_DATABASE: isaac
      DB_PORT: 3306
      DB_DIALECT: mysql
    ports:
      - "3000:80"
    depends_on:
      - ISAAC-floor-database

  ISAAC-sensor-back-end:
    image: jjuless/isaac-sensor-back-end
    environment:
      DB_HOST: ISAAC-sensor-database
      DB_USER: root
      DB_PASSWORD: isaac
      DB_DATABASE: isaac
      DB_PORT: 3307
      DB_DIALECT: mysql
    ports:
      - "3001:80"
    depends_on:
      - ISAAC-sensor-database 

CodePudding user response:

Mysql is always listening on port 3306 in each container, so if you want to have multiple mysql instances in the same host, you will need to map different host ports to the same guest port

Hence you will need to change your docker-compose file as follows

ISAAC-floor-database:
    image: mysql
    environment:
      MYSQL_ROOT_PASSWORD: isaac
    ports:
      - "3306:3306" # <-- HOST port same as GUEST port
    volumes:
      - ./sql-scripts:/docker-entrypoint-initdb.d

ISAAC-sensor-database:
    image: mysql
    environment:
      MYSQL_ROOT_PASSWORD: isaac
    ports:
      - "3307:3306" # <-- Notice here the GUEST port is the same as the first container!
    volumes:
      - ./sql-scripts:/docker-entrypoint-initdb.d
  • Related