title: Docker: Connected as id undefined. Error trying to establish connection between database container and backend container
body: <p>I'm trying to make a Docker project containing Backend (express.js), GUI (Angular), and Database (MySQL) containers. Each container runs correctly individually, but when I try to make the connection between database and backend I get the following error when I try to print the threadID. Also, if I try to query from this connection I just get a promise and not the data itself.</p>
  <p>&quot;connected as id undefined&quot;
  <a href="https://i.stack.imgur.com/x38Il.png" rel="nofollow noreferrer">Error</a></p>
  <p>The files composition of the project is the following.</p>
  <p><a href="https://i.stack.imgur.com/9fv9f.png" rel="nofollow noreferrer">File Directory</a></p>
  <pre><code>version: '3.8'
  
  services:
  
    gui:
      container_name: gui-container
      restart: always
      build: ./gui
      ports:
        - '4200:4200'
  
      environment:
        NODE_ENV: dev
        DEBUG: 'true'
      volumes:
        - ./gui:/usr/src/app/gui
        - /usr/src/app/gui/node_modules
      links:
        - backend
      command: npm run start
  
    backend:
      container_name: backend-container
      restart: always
      # command: sh -c &quot;npm cache clean --force &amp;&amp; npm start&quot;
      build: ./backend
      ports:
        - 3000:3000
      volumes:
        - ./backend:/usr/src/app/backend
        - /usr/src/app/backend/node_modules
      environment:
        NODE_ENV: dev
        DATABASE_HOST: database
      depends_on:
        - database
      # command: npm run debug
  
    database: 
      build: ./database
      container_name: database-container
      command: --default-authentication-plugin=mysql_native_password
      restart: always
      ports: 
        - 3318:3306
  </code></pre>
  <p>My config file is the following.</p>
  <pre><code>var mysql = require(&quot;mysql&quot;);
  
  function DataBaseHandler() {
      this.connection = null;
  }
  
  DataBaseHandler.prototype.createConnection = function () {
  
      console.log(&quot;Trying to connect to database.&quot;);
      this.connection = mysql.createConnection({
          host: 'database',
          user: 'root',
          password: 'testingpassword',
          database: 'KuoteSuite',
          port: 3306
      });
      this.connection.connect(function (err) {
          if (err) {
              console.error(&quot;error connecting &quot; + err.stack);
              return null;
          }
          console.log(&quot;connected as id &quot; + this.threadId);
      });
      return this.connection;
  };
  
  module.exports = DataBaseHandler;
  
  </code></pre>
  <p>I'm trying to establish the connection inside the lead.repository.js file inside backend/repositories.</p>
  <pre><code>const Lead = require('../models/lead');
  const mysql = require('mysql');
  
  var DataBaseHandler = require(&quot;../config/DataBaseHandler&quot;);
  var dataBaseHandler = new DataBaseHandler();
  
  var connection = dataBaseHandler.createConnection();
  
  
  const table = '`Lead`';
  // Repository uses Object Models to interact with Tables from Databases
  class LeadRepository{
  
      async getAllLeads(){
          const result = await connection.query(`SELECT * FROM ${table}`);
          await connection.commit();
          return result;
      }
  
  }
  
  
  module.exports = LeadRepository; 
  
  </code></pre>