A
A
Abdul Amenov2020-12-28 10:25:32
Node.js
Abdul Amenov, 2020-12-28 10:25:32

Docker Nodejs ERROR: connect ECONNREFUSED 127.0.0.1:3306, how to connect to MySQL?

Hello!
I am developing a site on nodejs + express + sequelize-cli
There was a task for development to deploy everything through docker.
You need to run mysql, adminer, backend at startup Docker

-compose.yml file:

version: '3'

services:
  backend:
    build: ./backend
    ports:
      - 5000:5000

  db:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_DATABASE: database_development
      MYSQL_ROOT_PASSWORD: password_development

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080


Dockerfile (backend):

FROM node:latest

COPY . /app

WORKDIR /app

RUN npm install
RUN npx sequelize db:migrate
RUN npx sequelize db:seed:all

EXPOSE 5000

VOLUME .:/app

CMD npm run dev


After running the command: docker-composer up

This error appears:

Step 5/9 : RUN npx sequelize db:migrate
 ---> Running in 08fd366ceb86

Sequelize CLI [Node: 15.5.0, CLI: 6.2.0, ORM: 6.3.5]

Loaded configuration file "database/config.json".
Using environment "development".

ERROR: connect ECONNREFUSED 127.0.0.1:3306

npm ERR! code 1
npm ERR! path /app
npm ERR! command failed
npm ERR! command sh -c sequelize db:migrate

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-12-28T07_22_31_094Z-debug.log
ERROR: Service 'backend' failed to build : The command '/bin/sh -c npx sequelize db:migrate' returned a non-zero code: 1


As I understand it is not connected to the database. On the Internet, it was advised to use depends_on, after applying depends_on, the build order just changed, but the error remained ... Tell me how to fix it. Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-12-28
@q2digger

read how the network is arranged in docker.
you need to connect not to 127.0.0.1:3306 , but to db:3306

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question