0

I have a node container that I'm trying to connect to locally hosted mysql db (outside container). I've tried "localhost", "mysql" and now ip address of the instance container is running on 172.17.x.x:3306 (found using docker inspect | grep Gateway) in config.js not working. ideas?

Mysql binds to 0.0.0.0

Error:

Unable to connect to the database: { SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306
    at Promise.tap.then.catch.err (/usr/src/app/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:123:19)
    at tryCatcher (/usr/src/app/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/usr/src/app/node_modules/bluebird/js/release/promise.js:547:31)
    at Promise._settlePromise (/usr/src/app/node_modules/bluebird/js/release/promise.js:604:18)
    at Promise._settlePromise0 (/usr/src/app/node_modules/bluebird/js/release/promise.js:649:10)
    at Promise._settlePromises (/usr/src/app/node_modules/bluebird/js/release/promise.js:725:18)
    at _drainQueueStep (/usr/src/app/node_modules/bluebird/js/release/async.js:93:12)
    at _drainQueue (/usr/src/app/node_modules/bluebird/js/release/async.js:86:9)
    at Async._drainQueues (/usr/src/app/node_modules/bluebird/js/release/async.js:102:5)
    at Immediate.Async.drainQueues [as _onImmediate] (/usr/src/app/node_modules/bluebird/js/release/async.js:15:14)
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)
  name: 'SequelizeConnectionRefusedError',
  parent:
   { Error: connect ECONNREFUSED 127.0.0.1:3306
       at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1107:14)
     errno: 'ECONNREFUSED',
     code: 'ECONNREFUSED',
     syscall: 'connect',
     address: '127.0.0.1',
     port: 3306,
     fatal: true },
  original:
   { Error: connect ECONNREFUSED 127.0.0.1:3306
       at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1107:14)
     errno: 'ECONNREFUSED',
     code: 'ECONNREFUSED',
     syscall: 'connect',
     address: '127.0.0.1',
     port: 3306,
     fatal: true } }

config.js

console.log("Running....");
var sequelize = new Sequelize("dbname", "admin", "pass", {
  host: "172.17.x.x:3306",
  dialect: "mysql",
  port: 3306,
  pool: {
    max: 5,
    min: 0,
    idle: 10000,
  },
});

docker file

FROM node:10

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

EXPOSE 8000
CMD [ "npm", "start" ]

is it my docker run command (need to port map to 8000?): docker run -i -t 49920e8c68b2

Shaz
  • 1,443
  • 1
  • 27
  • 67
  • 1
    Add `--net=host` to your command while running node container. Do not map any specific port while using this option. Also, update host to "localhost:3306" in the config.js. – Kapil Khandelwal Apr 08 '20 at 05:50
  • try with `172.17.0.1:3306` – akazuko Apr 08 '20 at 05:51
  • @KapilKhandelwal I think its close! New error about unknown database. The database exists but the docker container is looking for the wrong db name... why? Unable to connect to the database: { SequelizeConnectionError: Unknown database 'db_test_1' at Promise.tap.then.catch.err (/usr/src/app/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:133:19) – Shaz Apr 08 '20 at 06:03
  • one with the right name exists in the VM on the mysql...its trying to connect to the wrong db name even tho it is specified in config.js – Shaz Apr 08 '20 at 06:04
  • I think Sequelize won't create a database for you... you need to make sure that database `db_test_1` is already created. However, tables will be created if not already present. – Kapil Khandelwal Apr 08 '20 at 06:08
  • The thing is the database had been created, migrated via workbench remotely to instance. can see its there with show databases; so weird ? the dbname its looking for is an old name used in developement, check everywhere in code for the occurrence of that name. not there either – Shaz Apr 08 '20 at 06:13
  • maybe sequilize is still using the development dbname in the code...but where the config.js has the dbname only... – Shaz Apr 08 '20 at 06:14
  • @KapilKhandelwal that's resolved. however when i make a post request it fails due to: { Error: getaddrinfo ENOTFOUND localhost:3306 localhost:3306:3306 ideas pls? – Shaz Apr 08 '20 at 06:42
  • @KapilKhandelwal i've removed localhost:3306 and kept --net:host, works now – Shaz Apr 08 '20 at 06:50
  • Great!!! Btw, can you confirm once if your `/etc/hosts` file contains this line `127.0.0.1 localhost` or not. – Kapil Khandelwal Apr 08 '20 at 06:55

1 Answers1

0

As mentioned here:

The --net=host option is used to make the programs inside the Docker container look like they are running on the host itself, from the perspective of the network. It allows the container greater network access than it can normally get.

Add --net=host to your command while running node container.

Do not map any specific port while using this option.

Also, update host to "localhost:3306" in the config.js.

Make sure that the /etc/hosts file of node container contains must have the following line in it.

127.0.0.1 localhost
Kapil Khandelwal
  • 1,096
  • 12
  • 19
  • This generally disables Docker's network stack, and breaks things like inter-container networking. It will address this particular issue though. – David Maze Apr 08 '20 at 11:26