0

I am very new with docker.

My dockerFile:

FROM node:14
RUN mkdir /app
WORKDIR /app
copy package.json .
run npm install
copy . ./
EXPOSE 3000
VOLUME [ "/app/node_modules" ]
CMD ["npm", "start"]

My docker-compose.yml:

version: "3.8"

services: 
  smartdev-app-image:
    build: 
      context: .
      dockerfile: Dockerfile
    ports: 
      - 3000:3000
    volumes: 
      - .:/app
      - '/app/node_modules'
    depends_on: 
      - smartdev__mysqldb_server
  
  smartdev__mysqldb_server:
    image: mysql:8.0
    environment: 
      - MYSQL_DATABASE="smartdev_db"
      - MYSQL_USER="smartdev"
      - MYSQL_PASSWORD="*RUNSman001*"
      - MYSQL_ROOT_PASSWORD="*RUNSman001*"
    ports:
      # <Port exposed> : < MySQL Port running inside container>
      - '3306:3306'
    expose:
      # Opens port 3306 on the container
      - '3306'

My database connection credentials:

const mysql = require('mysql');

const connection = mysql.createConnection({
    host: "smartdev__mysqldb_server",
    user: "mydb_user",
    password: "mydb_password",
    database: "mydb_name",
    port: 3306,
    connectionLimit: 10
});

var del = connection._protocol._delegateError;
connection._protocol._delegateError = function(err, sequence){
  if (err.fatal) {
    console.trace('fatal error: ' + err.message);
  }
  return del.call(this, err, sequence);
};

connection.connect();

when i run:

docker compose up

i get the error:

[nodemon] 2.0.15
smartdev-pro-smartdev-app-image-1        | [nodemon] to restart at any time, enter `rs`
smartdev-pro-smartdev-app-image-1        | [nodemon] watching path(s): *.*
smartdev-pro-smartdev-app-image-1        | [nodemon] watching extensions: js,mjs,json
smartdev-pro-smartdev-app-image-1        | [nodemon] starting `node app.js`
smartdev-pro-smartdev-app-image-1        | The server is now listening on port 3306
smartdev-pro-smartdev-app-image-1        | Trace: fatal error: connect ECONNREFUSED 172.21.0.2:3306
smartdev-pro-smartdev-app-image-1        |     at Protocol.connection._protocol._delegateError (/app/controllers/db/connect.js:15:13)
smartdev-pro-smartdev-app-image-1        |     at Handshake.<anonymous> (/app/node_modules/mysql/lib/protocol/Protocol.js:153:12)
smartdev-pro-smartdev-app-image-1        |     at Handshake.emit (events.js:400:28)
smartdev-pro-smartdev-app-image-1        |     at Handshake.Sequence.end (/app/node_modules/mysql/lib/protocol/sequences/Sequence.js:78:12)
smartdev-pro-smartdev-app-image-1        |     at Protocol.handleNetworkError (/app/node_modules/mysql/lib/protocol/Protocol.js:369:14)
smartdev-pro-smartdev-app-image-1        |     at Connection._handleNetworkError (/app/node_modules/mysql/lib/Connection.js:418:18)
smartdev-pro-smartdev-app-image-1        |     at Socket.emit (events.js:400:28)
smartdev-pro-smartdev-app-image-1        |     at emitErrorNT (internal/streams/destroy.js:106:8)
smartdev-pro-smartdev-app-image-1        |     at emitErrorCloseNT (internal/streams/destroy.js:74:3)
smartdev-pro-smartdev-app-image-1        |     at processTicksAndRejections (internal/process/task_queues.js:82:21)
smartdev-pro-smartdev-app-image-1        | events.js:377
smartdev-pro-smartdev-app-image-1        |       throw er; // Unhandled 'error' event
smartdev-pro-smartdev-app-image-1        |       ^
smartdev-pro-smartdev-app-image-1        |
smartdev-pro-smartdev-app-image-1        | Error: connect ECONNREFUSED 172.21.0.2:3306
smartdev-pro-smartdev-app-image-1        |     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1159:16)
smartdev-pro-smartdev-app-image-1        |     --------------------
smartdev-pro-smartdev-app-image-1        |     at Protocol._enqueue (/app/node_modules/mysql/lib/protocol/Protocol.js:144:48)
smartdev-pro-smartdev-app-image-1        |     at Protocol.handshake (/app/node_modules/mysql/lib/protocol/Protocol.js:51:23)
smartdev-pro-smartdev-app-image-1        |     at Connection.connect (/app/node_modules/mysql/lib/Connection.js:116:18)
smartdev-pro-smartdev-app-image-1        |     at Object.<anonymous> (/app/controllers/db/connect.js:20:12)
smartdev-pro-smartdev-app-image-1        |     at Module._compile (internal/modules/cjs/loader.js:1085:14)
smartdev-pro-smartdev-app-image-1        |     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
smartdev-pro-smartdev-app-image-1        |     at Module.load (internal/modules/cjs/loader.js:950:32)
smartdev-pro-smartdev-app-image-1        |     at Function.Module._load (internal/modules/cjs/loader.js:790:12)
smartdev-pro-smartdev-app-image-1        |     at Module.require (internal/modules/cjs/loader.js:974:19)
smartdev-pro-smartdev-app-image-1        |     at require (internal/modules/cjs/helpers.js:101:18)
smartdev-pro-smartdev-app-image-1        | Emitted 'error' event on Connection instance at:
smartdev-pro-smartdev-app-image-1        |     at Connection._handleProtocolError (/app/node_modules/mysql/lib/Connection.js:423:8)
smartdev-pro-smartdev-app-image-1        |     at Protocol.emit (events.js:400:28)
smartdev-pro-smartdev-app-image-1        |     at Protocol._delegateError (/app/node_modules/mysql/lib/protocol/Protocol.js:398:10)
smartdev-pro-smartdev-app-image-1        |     at Protocol.connection._protocol._delegateError (/app/controllers/db/connect.js:17:14)
smartdev-pro-smartdev-app-image-1        |     at Handshake.<anonymous> (/app/node_modules/mysql/lib/protocol/Protocol.js:153:12)
smartdev-pro-smartdev-app-image-1        |     at Handshake.emit (events.js:400:28)
smartdev-pro-smartdev-app-image-1        |     at Handshake.Sequence.end (/app/node_modules/mysql/lib/protocol/sequences/Sequence.js:78:12)
smartdev-pro-smartdev-app-image-1        |     at Protocol.handleNetworkError (/app/node_modules/mysql/lib/protocol/Protocol.js:369:14)
smartdev-pro-smartdev-app-image-1        |     at Connection._handleNetworkError (/app/node_modules/mysql/lib/Connection.js:418:18)
smartdev-pro-smartdev-app-image-1        |     at Socket.emit (events.js:400:28) {
smartdev-pro-smartdev-app-image-1        |   errno: -111,
smartdev-pro-smartdev-app-image-1        |   code: 'ECONNREFUSED',
smartdev-pro-smartdev-app-image-1        |   syscall: 'connect',
smartdev-pro-smartdev-app-image-1        |   address: '172.21.0.2',
smartdev-pro-smartdev-app-image-1        |   port: 3306,
smartdev-pro-smartdev-app-image-1        |   fatal: true
smartdev-pro-smartdev-app-image-1        | }
smartdev-pro-smartdev-app-image-1        | [nodemon] app crashed - waiting for file changes before starting..

One thing that marvels me is where 172.21.0.2:3306 is coming from, i thought the address should have been that of my localhost i.e 127.0.0.1:3306. I've being on this for days, please can someone help point me to what i'm not doing right?

eedideyahoocom
  • 143
  • 1
  • 8
  • - MYSQL_DATABASE=mydb_name - MYSQL_USER=mydb_user - MYSQL_PASSWORD=mydb_password - MYSQL_ROOT_PASSWORD=mydb_password have you added above variables in your system environment? – RaviPatidar Mar 01 '22 at 08:29
  • _`host: smartdev__mysqldb_server`_ <- I don't see any quotes around `smartdev__mysqldb_server` so where have you defined that variable? Or did you mean to treat it as a string? – Phil Mar 01 '22 at 08:47
  • @Phil It is a string. I have quoted it now but that doesn't solve the problem – eedideyahoocom Mar 01 '22 at 09:10
  • @RaviPatidar I forgot to quote them. They are all strings, i have quoted them now, but that doesn't solve the problem. Thanks. – eedideyahoocom Mar 01 '22 at 09:11
  • Just a guess. But maybe docker-compose has troube with the underscores? This is a wild guess :) – lumio Mar 01 '22 at 09:25
  • 1
    When you run `docker-compose up` you should also see some messages about the database startup. Are these before or after the error message you quote? (It's very possible to get a "connect refused" error exactly like what you see if the application starts before the database is fully running; also see [Docker Compose wait for container X before starting Y](https://stackoverflow.com/questions/31746182/docker-compose-wait-for-container-x-before-starting-y).) – David Maze Mar 01 '22 at 10:43
  • did you able to run MySQL container, check whether there are other errors when setting up MySQL container also? – S.Sachith Mar 04 '22 at 19:04

1 Answers1

0

My guess is that the smartdev__mysqldb_server configuration isn't exposing any ports, so a connection can't be established over the Docker network. Try adding

    ports:
      # <Port exposed> : < MySQL Port running inside container>
      - '3306:3306'
    expose:
      # Opens port 3306 on the container
      - '3306'

to the configuration (extract from https://medium.com/@chrischuck35/how-to-create-a-mysql-instance-with-docker-compose-1598f3cc1bee)

kevstev01
  • 330
  • 5
  • 13