'How to solve docker-compose docker mysql ECONNREFUSED?

I have the following docker compose:

services:
  server:
    container_name: server
    build: 
      context: ./BE
      dockerfile: ./Dockerfile.prod
    depends_on:
      - mysql
    environment:
      MYSQL_HOST_IP: mysql
    ports:
      - 4000:4000
    links:
      - mysql
  mysql:
    image: mysql:latest
    container_name: mysqldb
    cap_add:
      - SYS_NICE # CAP_SYS_NICE
    # container_name: my_very_special_server
    ports:
      - 3307:3306
    volumes:
      - ./DbScripts/DumpTodoOnly.sql:/docker-entrypoint-initdb.d/DumpTodoOnly.sql
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_DATABASE: todo
      MYSQL_ROOT_PASSWORD: password!
      MYSQL_PASSWORD: password!
    healthcheck:
      test: "mysql -uroot -p$MYSQL_ROOT_PASSWORD content -e 'select 1'"
      interval: 1s
      retries: 120
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: dev_pma
    links:
      - mysql
    environment:
      PMA_HOST: mysql
      PMA_PORT: 3307
      PMA_ARBITRARY: 1
    restart: always
    ports:
      - 8183:80
  client:
    container_name: FE
    build: 
      context: ./FE
      dockerfile: ./Dockerfile.prod
    ports:
      - 3000:3000
    environment:
      - CHOKIDAR_USEPOLLING=true
    tty: true
volumes:
  db_data:

When i run: docker-compose up -d --build

Everything builds without errors.

I viste my php admin: http://localhost:8183/index.php?route=/&route=%2F

And I can see that my two tables are created:

enter image description here

Problem:

On the server I have a route http://localhost:4000/api/create

Which basically take in a body param that should add a todo to the todos table.

but when using postman I get :

{
    "status": "error",
    "statusCode": 500,
    "message": {
        "errno": "ECONNREFUSED",
        "code": "ECONNREFUSED",
        "syscall": "connect",
        "address": "127.0.0.1",
        "port": 3306,
        "fatal": true
    }
}

I thought that ny db image did not get build in time so I added a healtcheck in the compose file:

healthcheck:
  test: "mysql -uroot -p$MYSQL_ROOT_PASSWORD content -e 'select 1'"
  interval: 1s
  retries: 120

and on the server image:

depends_on:
        mysql:
          condition: service_healthy

But when I wanted to build the compose file I got:

ERROR: for server Container "a7916ff588af" is unhealthy. Encountered errors while bringing up the project

What am I missing?



Solution 1:[1]

Took me a while to update this question.

@Phil was correct. I needed to add the image name on my sql configuration in node express rather than the ip.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ThunD3eR