1

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 

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

5 Comments

That doesn't fix the problem, still the second API can't connect to the database. Any more ideas?
Are you able to connect to the second mysql instance from the host? What kind of error do you get within the second API container? A connection refused?
This is the error I get for the second API connection. original: Error: connect ECONNREFUSED 172.19.0.2:3307 isaac-main-ISAAC-sensor-back-end-1 | at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16) { isaac-main-ISAAC-sensor-back-end-1 | errno: -111, isaac-main-ISAAC-sensor-back-end-1 | code: 'ECONNREFUSED', isaac-main-ISAAC-sensor-back-end-1 | syscall: 'connect', isaac-main-ISAAC-sensor-back-end-1 | address: '172.19.0.2', isaac-main-ISAAC-sensor-back-end-1 | port: 3307, isaac-main-ISAAC-sensor-back-end-1 | fatal: true }
Fixed it with setting the DB_PORT to 3306 in the environments in the second API!
Awesome! I think that the reason it worked is because when you use the docker's hostname of the mysql server ( ISAAC-sensor-database ) you don't need to specify any port-mapping, the API container will directly connect to the port of the DB container

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.