0

I programmed a little nodejs app that connects to a mysql database with the following code lines.

const DATABASE = MYSQL.createPool({
    host    : 'localhost',
    user    : dbconfig.DATABASE_USER,
    password: dbconfig.DATABASE_CREDENTIAL,
    database: dbconfig.DATABASE_USING,
    multipleStatements: true
});

When running the nodejs app with "yarn node server.js" everything works fine and the nodejs app connects to the mysql database over port 3306 (standard port).

Now I want to do the exact same thing with the only difference being: The Nodejs-App runs in a docker container exposed with the port 3300. Everytime I make an API-Call which triggers a query to the database the following error shows up:

(node:36) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:3306
    at Object.createConnection (/app/.yarn/cache/mysql2-npm-2.2.5-fb26898147-2a3c2f0748.zip/node_modules/mysql2/promise.js:241:31)
    at Function.Filling.getAllFillingLiter (/app/server/models/filling.model.js:59:28)
    at Object.exports.getAllFillingLiter (/app/server/controller/filling.controller.js:36:13)
    at /app/server.js:145:13
    at Layer.handle [as handle_request] (/app/.yarn/cache/express-npm-4.17.1-6815ee6bf9-c4b470d623.zip/node_modules/express/lib/router/layer.js:95:5)
    at next (/app/.yarn/cache/express-npm-4.17.1-6815ee6bf9-c4b470d623.zip/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/app/.yarn/cache/express-npm-4.17.1-6815ee6bf9-c4b470d623.zip/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/app/.yarn/cache/express-npm-4.17.1-6815ee6bf9-c4b470d623.zip/node_modules/express/lib/router/layer.js:95:5)
    at /app/.yarn/cache/express-npm-4.17.1-6815ee6bf9-c4b470d623.zip/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/app/.yarn/cache/express-npm-4.17.1-6815ee6bf9-c4b470d623.zip/node_modules/express/lib/router/index.js:335:12)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:36) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:36) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I honestly can't tell what I am doing wrong. The docker container should be able to connect to the mysql-database running on host right? But what is my mistake?

Thanks in advance

  • If you try conect docker in docker you must use service name as host of mysql – rad11 Jul 08 '21 at 19:45
  • 1
    In order to connect to a service installed on the docker host , you can start the container with network=host and then connect over 127.0.0.1/localhost . – abhishek phukan Jul 08 '21 at 20:03
  • @abhishekphukan that worked for me. setting the network to host solved the issue. – Silent3rror Jul 08 '21 at 20:06
  • @rad11 i guess that would work in the situation that you described docker container to docker container. I was trying to connect a docker container to the host – Silent3rror Jul 08 '21 at 20:07
  • If you're on a MacOS or Windows host, you can use the special host name `host.docker.internal` to reach the host; the linked question describes how to do it on native Linux as well. Host networking generally disables Docker networking and can cause other trouble, especially if you introduce more containers into the system. – David Maze Jul 08 '21 at 23:02

0 Answers0