0

First of all, here is my folder structure:

enter image description here

db/Dockerfile

FROM mysql:5.7

COPY script.sql /docker-entrypoint-initdb.d/

web/Dockerfile

FROM node:10.16.0

# Set the work directory
WORKDIR /app

# Install packages
COPY package.json /app
RUN npm install

# Add application files
COPY . /app

CMD ["npm", "start"]

docker-compose.yml

version: '3'

services:
  db:
    build: ./db
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=docker
      - MYSQL_USER=root
      - MYSQL_PASSWORD=root
  web:
    build: ./web
    ports:
      - "3333:3333"
    depends_on:
      - db
    restart: on-failure

db/script.sql

CREATE DATABASE IF NOT EXISTS `docker` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE `docker`;

Node app

const connection = mysql.createConnection({
  host: 'db',
  user: 'root',
  password: 'root',
  database: 'docker'
});

Whenever I run docker-compose up I always get

{ Error: connect ECONNREFUSED 192.168.80.3:3306
web_1  |     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
web_1  |     --------------------
web_1  |     at Protocol._enqueue (/app/node_modules/mysql/lib/protocol/Protocol.js:144:48)
web_1  |     at Protocol.handshake (/app/node_modules/mysql/lib/protocol/Protocol.js:51:23)
web_1  |     at Connection.connect (/app/node_modules/mysql/lib/Connection.js:119:18)
web_1  |     at Object.<anonymous> (/app/index.js:12:12)
web_1  |     at Module._compile (internal/modules/cjs/loader.js:776:30)
web_1  |     at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
web_1  |     at Module.load (internal/modules/cjs/loader.js:653:32)
web_1  |     at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
web_1  |     at Function.Module._load (internal/modules/cjs/loader.js:585:3)
web_1  |     at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
web_1  |   errno: 'ECONNREFUSED',
web_1  |   code: 'ECONNREFUSED',
web_1  |   syscall: 'connect',
web_1  |   address: '192.168.80.3',
web_1  |   port: 3306,
web_1  |   fatal: true }

Am I missing something here? I am running it on Windows 10.

halfer
  • 19,824
  • 17
  • 99
  • 186
wobsoriano
  • 12,348
  • 24
  • 92
  • 162
  • The error `ECONNREFUSED` means that it is not possible to connect to port 3306 on 192.168.80.3. The most likely reason for this is that the database isn't ready, but you could also add a `ports:` line to the `docker-compose.yml` for db and see if that makes it accessible. [This question asks about how to make sure X is running before starting Y](https://stackoverflow.com/q/31746182/103081) and may also be of interest. – Paul Jul 19 '19 at 06:15
  • you missed to expose port in DB contaienr – Adiii Jul 19 '19 at 06:19
  • @Paul Hi. Already tried your suggestion before and got the same output. So likely the reason is that it's not yet ready? – wobsoriano Jul 19 '19 at 06:19
  • Either the app configuration and the db service ports have mismatched port numbers, the db port is inaccessible (firewall, etc.), or the app tries to contact the db before the db listens on the port. This is a fairly low level networking issue. It is not about what is in the database, or having the correct database password. – Paul Jul 19 '19 at 06:21
  • I will try and give some updates @Paul. Thank you. – wobsoriano Jul 19 '19 at 06:33

0 Answers0