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.
The file composition of the project is the following.
My docker-compose file is the following.
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 "npm cache clean --force && npm start"
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
My config file is the following.
var mysql = require("mysql");
function DataBaseHandler() {
this.connection = null;
}
DataBaseHandler.prototype.createConnection = function () {
console.log("Trying to connect to database.");
this.connection = mysql.createConnection({
host: 'database',
user: 'root',
password: 'testingpassword',
database: 'KuoteSuite',
port: 3318
});
this.connection.connect(function (err) {
if (err) {
console.error("error connecting " err.stack);
return null;
}
console.log("connected as id " this.threadId);
});
return this.connection;
};
module.exports = DataBaseHandler;
I'm trying to establish the connection inside the lead.repository.js file inside backend/repositories.
const Lead = require('../models/lead');
const mysql = require('mysql');
var DataBaseHandler = require("../config/DataBaseHandler");
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}`);
console.log("Leads in repository= " result);
await connection.commit();
return result;
}
}
module.exports = LeadRepository;
CodePudding user response:
Connections between containers ignore Compose ports:
(if they're present at all). In your configuration, you always need to connect to the default port: 3306
; since it's the default port you can probably omit that configuration.
Also consider making these parameters configurable via environment variables, since they'll be different in different environments. For instance, the connection parameters will be different running the same code against the same containerized database if the application is running in a container or in a non-container development environment, and they'll be different again if you're running in a production environment with a cloud-hosted non-container database.
this.connection = mysql.createConnection({
host: process.env.DATABASE_HOST || 'localhost',
user: process.env.DATABASE_USER || 'root',
password: process.env.DATABASE_PASSWORD || 'testingpassword',
database: process.env.DATABASE_DATABASE || 'KuoteSuite',
port: process.env.DATABASE_PORT || 3306 // not 3318
});
# Local development:
export MYSQL_HOST=localhost MYSQL_PORT=3318
npm run dev
# Compose setup:
version: '3.8'
services:
database:
build: ./database
ports: # ignored for inter-container connections
- '3318:3306'
backend:
build: ./backend
depends_on:
- database
ports:
- '3000:3000'
environment:
NODE_ENV: dev
DATABASE_HOST: database
DATABASE_PORT: '3306' # default MySQL port, not remapped ports:
CodePudding user response:
Keep in mind docker-compose
by default uses the service name as hostname. So if you want to connect with database
from another container in the same docker-compose.yml
file, you have to use the service name as hostname. In your case you should change your database host to: http://database
instead of 127.0.0.1
in your DatabaseHandler
line 11
.
And port to 3318
on line 15
, as you declared in your docker-compose.yml
to port the database port to 3318
instead of 3306
. Keep in mind that for porting the syntax is: HOST_PORT:CONTAINER_PORT
.