Please write your posts in English. If you don’t feel comfortable writing in English, use a translating service like Google Translate. Furthermore, please format your post properly.
Thank you for co-operation!
Share and learn in the Docker community.
Please write your posts in English. If you don’t feel comfortable writing in English, use a translating service like Google Translate. Furthermore, please format your post properly.
Thank you for co-operation!
Hello I have the same error, but with mysql
this is my yml file
version: "3.8"
services:
my_bdd:
container_name: mysql_databaase
image: mysql
ports:
- 3306:3306
environment:
- MYSQL_ROOT_PASSWORD=123456
- MYSQL_DATABASE=prueba
- MYSQL_USER=rodrigo
- MYSQL_PASSWORD=oliverman12
networks:
- red_app
node_js:
depends_on:
- my_bdd
build: .
ports:
- 3000:3000
links:
- my_bdd
networks:
- red_app
networks:
red_app:
external: true
and this is my index.js file
const express = require("express");
const app = express();
const mysql = require("mysql");
const pool = mysql.createConnection({
host: 'my_bdd',
user: 'root',
password: '123456',
database: 'prueba',
port: '3306'
});
pool.connect((error)=> {
console.log("error ",error);
})
app.get("/", (req, res) => {
return res.json({
"ok": true,
"msg": "hello"
});
});
app.listen(3000, () => {
console.log("lissten 3000");
});
this is my error
node_js-1 | error en la conexion Error: connect ECONNREFUSED 192.168.224.2:3306
node_js-1 | at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1605:16)
node_js-1 | --------------------
node_js-1 | at Protocol._enqueue (/home/app/node_modules/mysql/lib/protocol/Protocol.js:144:48)
node_js-1 | at Protocol.handshake (/home/app/node_modules/mysql/lib/protocol/Protocol.js:51:23)
node_js-1 | at Connection.connect (/home/app/node_modules/mysql/lib/Connection.js:116:18)
node_js-1 | at Object.<anonymous> (/home/app/index.js:12:6)
node_js-1 | at Module._compile (node:internal/modules/cjs/loader:1369:14)
mysql_databaase | '/var/lib/mysql/mysql.sock' -> '/var/run/mysqld/mysqld.sock'
node_js-1 | at Module._extensions..js (node:internal/modules/cjs/loader:1427:10)
node_js-1 | at Module.load (node:internal/modules/cjs/loader:1206:32)
node_js-1 | at Module._load (node:internal/modules/cjs/loader:1022:12)
node_js-1 | at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
node_js-1 | at node:internal/main/run_main_module:28:49 {
node_js-1 | errno: -111,
node_js-1 | code: 'ECONNREFUSED',
node_js-1 | syscall: 'connect',
node_js-1 | address: '192.168.224.2',
node_js-1 | port: 3306,
node_js-1 | fatal: true
node_js-1 | }
Format your code/config with 3 backticks before and after to improve readability. In yaml every space matters.
version: “3.8”
services:
my_bdd:
image: mysql
ports:
- 3306:3306
environment:
- MYSQL_ROOT_PASSWORD=123456
- MYSQL_DATABASE=prueba
- MYSQL_USER=rodrigo
- MYSQL_PASSWORD=oliverman12
networks:
- red_app
node_js:
depends_on:
- my_bdd
build: .
ports:
- 3000:3000
links:
- my_bdd
networks:
- red_app
networks:
red_app:
external: true
Something I noticed is that node js is always executed first, although the database is not ready yet.
You could have edited your original post and format everything instead of just the yaml. True, it is the most important for yaml files, but it makes it easier to read everything. Some help: How to format your forum posts
I fixed it for you this time.
It seems you try to connect to
but receive error
Are you sure everything is up to date? Usually Docker IPs are not from 192.x.x.x range.
You don’t need links:
, as you already use a common Docker network.
You don’t need ports:
on database, as all ports are reachable within a Docker network.
Hi, Metin.
I tried your solution but i always get an error Error: getaddrinfo ENOTFOUND mysqlservice:3306
. I don’t know why.
Hier is my docker-compose.yml.
services:
mysqlservice:
image: mysql:8.0
restart: always
environment:
MYSQL_DATABASE: ws24
MYSQL_USER: test
MYSQL_PASSWORD: abcdef
MYSQL_ROOT_PASSWORD: abcdef
ports:
- 3306:3306
volumes:
# Mounts the init.sql file to docker entrypoint - runs the query on startup
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
orderservice:
build: ./orderservice
ports:
- 6010:6010
depends_on:
- mysqlservice
And hier is the connection in my app:
const mysql = require("mysql2/promise");
const pool = mysql.createPool({
host: "mysqlservice:3306",
user: "test",
password: "abcdef",
database: "ws24",
});
module.exports = pool;
Your compose file looks fine. Have you tried host: "mysqlservice"
without the port?
If you check the docks of the mysql2 library, you can see that host is supposed to be the hostname without the port, as port is a seperate field: https://sidorares.github.io/node-mysql2/docs/examples/connections/create-pool#pooloptions (you need to expand “ConnectionOptions Specification” to see it)
OMG, It works. Thank you so much