brn
brn

Reputation: 305

nodejs dockerized app can't connect to mariadb dockerized database

My setup is :

My issue :

I can't connect my nodejs app to the database. I can access the database locally through dbeaver or command line so I know it's working. But when I try to access it thourgh my index.js I get the following error :

Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
From event:
at _registerHandshakeCmd (/usr/src/app/node_modules/mariadb/lib/connection.js:689:11)
at /usr/src/app/node_modules/mariadb/lib/connection.js:57:11
at new Promise (<anonymous>)
at Connection.connect (/usr/src/app/node_modules/mariadb/lib/connection.js:56:16)
at Object.createConnection (/usr/src/app/node_modules/mariadb/promise.js:17:36)
at Object.<anonymous> (/usr/src/app/src/index.js:24:28)
at Module._compile (internal/modules/cjs/loader.js:1147:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:10)
at Module.load (internal/modules/cjs/loader.js:996:32)
at Function.Module._load (internal/modules/cjs/loader.js:896:14) {
    errno: -111,
    code: 'ECONNREFUSED',
    syscall: 'connect',
    address: '127.0.0.1',
    port: 3306,
    fatal: true
}

NodeJs Dockerfile:

FROM node:latest
WORKDIR /usr/src/app
COPY package*.json /usr/usr/app
RUN npm install
COPY . /usr/src/app
EXPOSE 8080
CMD ["npm", "start"]

Mariadb Dockerfile:

FROM mariadb:latest
ENV MYSQL_ROOT_PASSWORD=mdp
ENV MYSQL_DATABASE=dashboard
ENV MYSQL_USER=monty
ENV MYSQL_PASSWORD=monty
USER 1000
EXPOSE 3306

docker-compose.yml:

version: '3'
services: 
    web:
        build: ./backend
        ports:
            - "8080:8080"
        volumes:
            - ./backend:/usr/src/app/
        working_dir: /usr/src/app
        environment:
            - MARIADB_HOST=database
            - MARIADB_PORT_NUMBER=3306
            - MARIADB_USER=monty
            - MARIADB_PASSWORD=monty
            - MARIADB_DATABASE=dashboard
        user: "1000"
        container_name: backend
        depends_on:
            - database
        links:
            - database:database
    database:
        build: ./database
        ports:
            - "3306:3306"
        expose:
            - "3306"
        volumes:
            - /database/data:/var/lib/mysql:rw
        user: "1000"
        hostname: "localhost"
        environment:
            - ALLOW_EMPTY_PASSWORD=yes
            - MARIADB_DATABASE=dashboard
            - MARIADB_PORT=3306
            - MARIADB_USER=monty
            - MARIADB_PASSWORD=monty
        container_name: database

And here is my index.js:

const express = require('express')
const mariadb = require('mariadb');
const cors = require('cors');
const bodyParser = require('body-parser');
const app = express();
app.use(
    bodyParser.urlencoded({
        extended: true
    })
)
app.use(cors());

app.get('/', (req, res) => {
    res.send('hello world');
});

const connection = mariadb.createConnection({
    host: '127.0.0.1',
    user: 'someuser',
    password: 'somepassword',
    database: 'dashboard',
    port: '3306',
}).then(conn => {
    console.log('connection established.');
}).catch(err => {
    console.log(err);
});

app.listen(8080);

If anyone has any tips regarding where to look or where the error might come from I'd be grateful. Thanks!

Upvotes: 1

Views: 1200

Answers (1)

adebasi
adebasi

Reputation: 3945

You are using 127.0.0.1 as db host name in your NodeJS application. But that is the IP address of your NodeJS container. Every container has its own IP address (exception: using docker network host). This causes the error:

Error: connect ECONNREFUSED 127.0.0.1:3306

When using docker-compose, you can access the db using the service name you specified in the docker-compose.yml file. In your example the service name database can be used as host name. Try this:

const connection = mariadb.createConnection({
    host: 'database',
    ...

Upvotes: 4

Related Questions