I've got a Docker project composed by:
Rest API written in Flask (called "python")
Backend written in nodejs (called "server")
Mongodb
#docker-compose.yml
version: '3.8'
services:
server:
build:
context: ./server
dockerfile: ./Dockerfile
restart: on-failure
ports:
- "6000:6000"
links:
- python
- mongo
depends_on:
- python
- mongo
volumes:
- ./server:/code
env_file: .env
environment:
- MONGO_USERNAME=$DATABASE_USERNAME
- MONGO_PASSWORD=$DATABASE_PASSWORD
- MONGO_HOST=mongo
- MONGO_PORT=$DATABASE_PORT
- MONGO_DB=$DATABASE
networks:
- todo-network
python:
build:
context: ./rest_api_wrapper_py
dockerfile: ./Dockerfile
restart: on-failure
ports:
- "5000:5000"
links:
- mongo
depends_on:
- mongo
volumes:
- ./rest_api_wrapper_py:/code
env_file: .env
environment:
- MONGO_USERNAME=$DATABASE_USERNAME
- MONGO_PASSWORD=$DATABASE_PASSWORD
- MONGO_HOST=mongo
- MONGO_PORT=$DATABASE_PORT
- MONGO_DB=$DATABASE
networks:
- todo-network
mongo:
image: mongo
restart: on-failure
env_file: .env
environment:
- MONGO_INITDB_ROOT_USERNAME=$DATABASE_ROOT_USERNAME
- MONGO_INITDB_ROOT_PASSWORD=$DATABASE_ROOT_PASSWORD
- MONGO_INITDB_DATABASE=$DATABASE
- DATABASE_USERNAME=$DATABASE_USERNAME
- DATABASE_PASSWORD=$DATABASE_PASSWORD
- MONGO_PORT=$DATABASE_PORT
volumes:
- /home/s0n1c/tmp/mongo:/data/mongo
- ./docker_scripts/:/docker-entrypoint-initdb.d
- /data/mongodb/db:/data/db
ports:
- "27017:27017"
networks:
- todo-network
networks:
todo-network:
driver: bridge
"python" starts correctly:
Running on http://127.0.0.1:5000/
I need to call the endpoint python:5000/variables from "server" (node js):
(async () => {
try {
const res = await superagent.get('http://python:5000/variables');
console.log(res);
} catch (err) {
console.error(err);
}
})();
This request fails:
server_1 | Error: connect ECONNREFUSED 172.19.0.3:5000
server_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16) {
server_1 | errno: -111,
server_1 | code: 'ECONNREFUSED',
server_1 | syscall: 'connect',
server_1 | address: '172.19.0.3',
server_1 | port: 5000,
server_1 | response: undefined
server_1 | }
Can someone help me, please? Thanks a lot
Related
Trying to connect my node app to mysql database. I already have an express api set up that manages to connect but for some reason node doesn't work. Using "links" in the docker compose file, and it resolves to the right local (internal) container IP but still doesn't connect. Error:
The node app isn't listening on any ports itself (does it need to?). Thanks!
Error: connect ECONNREFUSED 192.168.64.2:3306
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1278:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '192.168.64.2',
port: 3306,
fatal: true
}
let con;
async function create_connection() {
const username = await process.env.USERNAME;
const password = await process.env.PASSWORD;
con = await mysql.createConnection({
host: "db",
user: username,
password: password,
database: "database",
port: "3306",
});
await con.connect(function(err) {
if (err) throw err;
});
}
create_connection();
docker-compose:
version: '3.8'
services:
db:
image: mysql:8.0.31
cap_add:
- SYS_NICE
restart: always
env_file:
- somefile.env
ports:
- 127.0.0.1:external_port:3306
volumes:
- db:/var/lib/mysql
- ./db/init.sql:/docker-entrypoint-initdb.d/init.sql
express:
image: express/someapi
links:
- db:db
ports:
- 127.0.0.1:external_port:3355
env_file:
- somefile.env
depends_on:
- db
node_app:
image: some_image
links:
- db:db
env_file:
- somefile.env
depends_on:
- db
volumes:
db:
driver: local
~
Tried using links and mysql2 to connect to the database. Expected it to work as with my express backend but does not connect.
I have a simple web app which uses Sequelize with Mysql. After a long time and a series of updates I did not keep track of (it worked fine a year ago), the app now crashes upon starting the server.
I spent a couple of hours searching, but I did not find a satisfying answer. Does somebody know what's wrong?
Here is a minimal working example:
Setup
I have a basic directory structure with the following files:
+ test-nodejs
|- package.json
|- package-lock.json
|-+ node_modules
|-+ server
|- server.js
|-+ models
|- index.js
|- user.model.js
I ran npm init; npm install express; npm install sequelize in the root dir.
Code
I believe the only relevant part of the code is in server/server.js and server/models/index.js. Here it is:
server/server.js
const express = require("express");
const app = express();
const db = require("./models");
db.sequelize.sync()
app.get("/", (req, res) => {
res.json({message: "Welcome"});
});
const PORT = 8080
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`)
});
server/models/index.js
const Sequelize = require("sequelize");
const sequelize = new Sequelize("db", "user", "password", {host: 'localhost', dialect: 'mysql'});
const db = {};
db.Sequelize = Sequelize;
db.sequelize = sequelize;
db.users = require("./user.model.js")(sequelize, Sequelize); // This is actually not needed for the MWE
module.exports = db;
Output
When I run node server.js of this minimal working example, I get the following output:
Server is running on port 8080
node:internal/process/promises:246
triggerUncaughtException(err, true /* fromPromise */);
^
Error
at Query.run (/home/box/test-nodejs/node_modules/sequelize/dist/lib/dialects/mysql/query.js:52:25)
at /home/box/test-nodejs/node_modules/sequelize/dist/lib/sequelize.js:313:28
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async MySQLQueryInterface.databaseVersion (/home/box/test-nodejs/node_modules/sequelize/dist/lib/dialects/abstract/query-interface.js:69:12)
at async Sequelize.databaseVersion (/home/box/test-nodejs/node_modules/sequelize/dist/lib/sequelize.js:418:12)
at async /home/box/test-nodejs/node_modules/sequelize/dist/lib/dialects/abstract/connection-manager.js:181:31
at async ConnectionManager.getConnection (/home/box/test-nodejs/node_modules/sequelize/dist/lib/dialects/abstract/connection-manager.js:197:7)
at async /home/box/test-nodejs/node_modules/sequelize/dist/lib/sequelize.js:303:26
at async Sequelize.authenticate (/home/box/test-nodejs/node_modules/sequelize/dist/lib/sequelize.js:414:5)
at async Sequelize.sync (/home/box/test-nodejs/node_modules/sequelize/dist/lib/sequelize.js:374:7) {
name: 'SequelizeDatabaseError',
parent: Error: Encoding not recognized: 'undefined' (searched as: 'undefined')
at Object.getCodec (/home/box/test-nodejs/node_modules/mysql2/node_modules/iconv-lite/lib/index.js:104:23)
at Object.getEncoder (/home/box/test-nodejs/node_modules/mysql2/node_modules/iconv-lite/lib/index.js:115:23)
at Object.exports.encode (/home/box/test-nodejs/node_modules/mysql2/lib/parsers/string.js:23:25)
at Query.toPacket (/home/box/test-nodejs/node_modules/mysql2/lib/packets/query.js:16:30)
at Query.start (/home/box/test-nodejs/node_modules/mysql2/lib/commands/query.js:60:38)
at Query.execute (/home/box/test-nodejs/node_modules/mysql2/lib/commands/command.js:45:22)
at Connection.handlePacket (/home/box/test-nodejs/node_modules/mysql2/lib/connection.js:456:32)
at Connection.addCommand (/home/box/test-nodejs/node_modules/mysql2/lib/connection.js:478:12)
at Connection.query (/home/box/test-nodejs/node_modules/mysql2/lib/connection.js:546:17)
at results (/home/box/test-nodejs/node_modules/sequelize/dist/lib/dialects/mysql/query.js:60:22) {
sql: 'SELECT VERSION() as `version`',
parameters: undefined
},
original: Error: Encoding not recognized: 'undefined' (searched as: 'undefined')
at Object.getCodec (/home/box/test-nodejs/node_modules/mysql2/node_modules/iconv-lite/lib/index.js:104:23)
at Object.getEncoder (/home/box/test-nodejs/node_modules/mysql2/node_modules/iconv-lite/lib/index.js:115:23)
at Object.exports.encode (/home/box/test-nodejs/node_modules/mysql2/lib/parsers/string.js:23:25)
at Query.toPacket (/home/box/test-nodejs/node_modules/mysql2/lib/packets/query.js:16:30)
at Query.start (/home/box/test-nodejs/node_modules/mysql2/lib/commands/query.js:60:38)
at Query.execute (/home/box/test-nodejs/node_modules/mysql2/lib/commands/command.js:45:22)
at Connection.handlePacket (/home/box/test-nodejs/node_modules/mysql2/lib/connection.js:456:32)
at Connection.addCommand (/home/box/test-nodejs/node_modules/mysql2/lib/connection.js:478:12)
at Connection.query (/home/box/test-nodejs/node_modules/mysql2/lib/connection.js:546:17)
at results (/home/box/test-nodejs/node_modules/sequelize/dist/lib/dialects/mysql/query.js:60:22) {
sql: 'SELECT VERSION() as `version`',
parameters: undefined
},
sql: 'SELECT VERSION() as `version`',
parameters: {}
}
Environment
$ mysql --version
Ver 8.0.27-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))
$ node --version
v16.13.1
$ npm --version
8.3.0
$ npm list
test-nodejs#1.0.0 /home/box/test-nodejs
├── express#4.17.2
├── mysql2#2.3.3
└── sequelize#6.12.0
I'm trying to put in production a site I made with Angular and Express/Sequelize, but always have Sequelize errors :
It works perfectly fine in local with WAMP, I can reach my local database and it's all good.
When I'm trying to use my OVH database (that I got with my hosting plan), I have the following errors :
HostNotFoundError [SequelizeHostNotFoundError]: getaddrinfo ENOTFOUND dodobwtdodob.mysql.db
at ConnectionManager.connect (C:\Users\Dorian\Desktop\Développement Web\Mes sites\capitales\server\node_modules\sequelize\lib\dialects\mysql\connection-manager.js:120:17)
at processTicksAndRejections (node:internal/process/task_queues:94:5)
at async ConnectionManager._connect (C:\Users\Dorian\Desktop\Développement Web\Mes sites\capitales\server\node_modules\sequelize\lib\dialects\abstract\connection-manager.js:318:24)
at async C:\Users\Dorian\Desktop\Développement Web\Mes sites\capitales\server\node_modules\sequelize\lib\dialects\abstract\connection-manager.js:250:32
at async ConnectionManager.getConnection (C:\Users\Dorian\Desktop\Développement Web\Mes sites\capitales\server\node_modules\sequelize\lib\dialects\abstract\connection-manager.js:280:7)
at async C:\Users\Dorian\Desktop\Développement Web\Mes sites\capitales\server\node_modules\sequelize\lib\sequelize.js:613:26
at async MySQLQueryInterface.createTable (C:\Users\Dorian\Desktop\Développement Web\Mes sites\capitales\server\node_modules\sequelize\lib\dialects\abstract\query-interface.js:225:12)
at async Function.sync (C:\Users\Dorian\Desktop\Développement Web\Mes sites\capitales\server\node_modules\sequelize\lib\model.js:1300:5)
at async Sequelize.sync (C:\Users\Dorian\Desktop\Développement Web\Mes sites\capitales\server\node_modules\sequelize\lib\sequelize.js:793:35) {
parent: Error: getaddrinfo ENOTFOUND dodobwtdodob.mysql.db
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:69:26) {
errno: -3008,
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'dodobwtdodob.mysql.db',
fatal: true
},
original: Error: getaddrinfo ENOTFOUND dodobwtdodob.mysql.db
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:69:26) {
errno: -3008,
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'dodobwtdodob.mysql.db',
fatal: true
}
}
I don't understand because I've always used the same config, it works fine when I'm using a PHP PDO object on other projects for example. It doesn't work either on my OVH VPS, where I'm getting connection refused error (even if I had to look at all the tutorials on the net), but that's another problem.
Here is my db.config.js :
module.exports = {
HOST: "dodobwtdodob.mysql.db",
USER: "dodobwtdodob",
PASSWORD: "secretpassword",
DB: "dodobwtdodob",
dialect: "mysql",
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
},
port:3306
};
And here a part of my index.js :
const dbConfig = require("../config/db.config.js");
const Sequelize = require("sequelize");
const sequelize = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, {
host: dbConfig.HOST,
dialect: dbConfig.dialect,
port: dbConfig.port,
pool: {
max: dbConfig.pool.max,
min: dbConfig.pool.min,
acquire: dbConfig.pool.acquire,
idle: dbConfig.pool.idle
}
});
Please change hostname "dodobwtdodob.mysql.db" to ipAddress(host address).
When I run yarn dev, it runs nodemon via the start script. Once my express server is up and running, if I Ctrl + C on it then try to run yarn dev again I get:
Error: bind EADDRINUSE null:3000
at listenOnMasterHandle (net.js:1380:18)
at rr (internal/cluster/child.js:132:12)
at Worker.<anonymous> (internal/cluster/child.js:99:7)
at process.onInternalMessage (internal/cluster/utils.js:47:8)
at process.emit (events.js:327:22)
at emit (internal/child_process.js:906:12)
at processTicksAndRejections (internal/process/task_queues.js:85:21)
Emitted 'error' event on Server instance at:
at listenOnMasterHandle (net.js:1381:21)
at rr (internal/cluster/child.js:132:12)
[... lines matching original stack trace ...]
at processTicksAndRejections (internal/process/task_queues.js:85:21) {
errno: -48,
code: 'EADDRINUSE',
syscall: 'bind',
address: null,
port: 3000
}
Here it is running the first time I fire it up after say I reboot my machine and there are no processes running yet:
The only way I can manually kill it and temporarily move on is to use pkill node. lsof -i :3000 -t | xargs kill` doesn't work.
If I then Ctrl + C then run yarn dev I get:
Here are my scripts
"start": "PORT=3000 nodemon --trace-warnings dist/server/server.js",
"build": "NODE_ENV=production webpack -p --env=prod --watch && yarn compile-server && yarn start",
"dev": "NODE_ENV=development yarn lint && yarn copyData && yarn compile-server && yarn start & webpack-dev-server",
"compile-server": "tsc -b ./src/server",
"copyData": "mkdir -p dist/shared/data && cp src/shared/data/companies.json dist/shared/data && cp src/shared/data/countries.json dist/shared/data",
server.ts
const app = require('./api.js');
const cluster = require('cluster'),
os = require('os'),
port = process.env.PORT || 3000;
if (cluster.isMaster) {
for (let i = 0; i < os.cpus().length; i++) {
cluster.fork();
}
console.log('Ready on port %d', port);
} else {
app.listen(port, (err: string) => {
console.log(`express is listening on port ${port}`);
if (err) {
console.log('server startup error');
console.log(err);
}
});
}
I am trying to connect with redis(of docker instance) using express(node.js).
Here is my index.ts file in which i am trying to connect with redis
import mongoose from 'mongoose'
import express from 'express'
import session from 'express-session'
import connectRedis from 'connect-redis'
import Redis from 'ioredis'
import { MONGO_URI, MONGO_OPTIONS, REDIS_OPTIONS, APP_PORT } from './config'
import { createApp } from './app';
; (async () => {
await mongoose.connect(MONGO_URI, MONGO_OPTIONS)
const RedistStore = connectRedis(session)
const client = new Redis(REDIS_OPTIONS)
const store = new RedistStore({ client })
const app = createApp(store)
app.listen(APP_PORT, () => console.log('server running on port 3000'))
})()
Here is my REDIS_OPTIONS file in which i have all redis options.
import { RedisOptions } from 'ioredis'
const {
REDIS_PORT = 6379,
REDIS_HOST = 'localhost',
REDIS_PASSWORD = 'secret'
} = process.env
export const REDIS_OPTIONS: RedisOptions = {
port: +REDIS_PORT,
host: REDIS_HOST,
password: REDIS_PASSWORD
}
docker-compose.yml
version: '3'
services:
db:
user: $UID
image: mongo
ports:
- '27017:27017'
environment:
MONGO_INITDB_ROOT: ''
MONGO_INITDB_PASSWORD: ''
MONGO_INITDB_DATABASE: auth,
MONGO_USERNAME: ''
MONGO_PASSWORD: ''
volumes:
- ./data:/data/db
- ./mongo-init.sh:/docker-entrypoint-initdb.d/mongo-init.sh:ro
cache:
image: redis:alpine
ports:
- '6379:6379'
command: ['--requirepass "secret"']
package.json
{
"name": "node-auth",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"up": "docker-compose up -d",
"postup": "npm run dev",
"stop": "docker-compose stop",
"down": "docker-compose down",
"dev": "npm run dev --prefix api"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Now when i try to run my main app file so i am getting this error
[ioredis] Unhandled error event: Error: connect ECONNREFUSED 127.0.0.1:6379 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14) node.js
How can i get rid of this error?
Looks like it might be a problem with the Redis container. What command you used to run the container? Try adding -p 6379:6379 with your docker command. Also, check whether port 6379 is used by something else or blocked by antivirus or firewall.
change host in index.ts from 'localhost' to 'redis'