Reputation: 77
I am trying to deploy my node js application with docker but when I am running the docker compose it giving an error at the end. I tried some solutions like using wait-for-it.sh file but issue is still there.
docker-compose.yml
version: '3'
services:
db:
build:
context: .
dockerfile: ./docker/Dockerfile-mysql
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
- MYSQL_DATABASE=dbautokab
- MYSQL_USER=root
- MYSQL_PASSWORD=
networks:
- helicopter-network
healthcheck:
test: "exit 0"
helicopter-api:
build:
context: .
dockerfile: ./docker/Dockerfile-api
depends_on:
- db
networks: ['helicopter-network']
environment:
- PORT=3000
- DATABASE_HOST=db
- DATABASE_PASSWORD=
- EGG_SERVER_ENV=local
- NODE_ENV=development
ports:
- "3000:3000"
networks:
helicopter-network:
driver: bridge
Dockerfile-api
FROM node:10-slim
USER node
RUN mkdir -p /home/node/app
WORKDIR /home/node/app
COPY --chown=node package*.json ./
RUN npm install
COPY --chown=node . .
COPY wait-for-it.sh /
ENV HOST=0.0.0.0 PORT=3000
EXPOSE ${PORT}
# Run when the container launches
#CMD [ "node", "." ]
CMD /wait-for-it.sh db:3306 -- npm start
Dockerfile-mysql
FROM mysql
COPY ./docker/init_db.sql /docker-entrypoint-initdb.d/
init_db.sql
CREATE DATABASE IF NOT EXISTS dbautokab;
GRANT ALL PRIVILEGES on dbautokab.*
TO 'root'@'%'
WITH GRANT OPTION;
db.js
var mysql = require('mysql');
var db_config = {
host : '127.0.0.1', // Your host - either local or cloud
user : 'root', // your username
password : '', // your password
database : 'dbautokab' // database name
};
var connection;
function handleDisconnect() {
connection = mysql.createConnection(db_config); // Recreate the connection, since
// the old one cannot be reused.
connection.connect(function(err) { // The server is either down
if(err) { // or restarting (takes a while sometimes).
console.log('error when connecting to db:', err);
setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,
}
// to avoid a hot loop, and to allow our node script to
}); // process asynchronous requests in the meantime.
// If you're also serving http, display a 503 error.
connection.on('error', function(err) {
console.log('db error', err);
if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
handleDisconnect(); // lost due to either server restart, or a
} else { // connnection idle timeout (the wait_timeout
throw err; // server variable configures this)
}
});
}
handleDisconnect();
module.exports = connection;
Error:
helicopter-api_1 | Server is up on 3000
helicopter-api_1 | error when connecting to db: { Error: connect ECONNREFUSED 127.0.0.1:3306
helicopter-api_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
helicopter-api_1 | --------------------
helicopter-api_1 | at Protocol._enqueue (/home/node/app/node_modules/mysql/lib/protocol/Protocol.js:144:48)
helicopter-api_1 | at Protocol.handshake (/home/node/app/node_modules/mysql/lib/protocol/Protocol.js:51:23)
helicopter-api_1 | at Connection.connect (/home/node/app/node_modules/mysql/lib/Connection.js:119:18)
helicopter-api_1 | at handleDisconnect (/home/node/app/db.js:16:16)
helicopter-api_1 | at Object. (/home/node/app/db.js:35:1)
helicopter-api_1 | at Module._compile (internal/modules/cjs/loader.js:776:30)
helicopter-api_1 | at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
helicopter-api_1 | at Module.load (internal/modules/cjs/loader.js:653:32)
helicopter-api_1 | at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
helicopter-api_1 | at Function.Module._load (internal/modules/cjs/loader.js:585:3)
helicopter-api_1 | errno: 'ECONNREFUSED',
helicopter-api_1 | code: 'ECONNREFUSED',
helicopter-api_1 | syscall: 'connect',
helicopter-api_1 | address: '127.0.0.1',
helicopter-api_1 | port: 3306,
helicopter-api_1 | fatal: true }
db_1 |
db_1 | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init_db.sql
db_1 |
db_1 |
Upvotes: 1
Views: 5622
Reputation: 31564
Your nodejs app
& db
not in the same container, so db.js
cannot visit db with ip 127.0.0.1
.
Add container_name: mydb
to your db service like next:
services:
db:
build:
context: .
dockerfile: ./docker/Dockerfile-mysql
container_name: mydb
Then, in db.js
use the container name to visit db:
var db_config = {
host : 'mydb', // Your host - either local or cloud
user : 'root', // your username
password : '', // your password
database : 'dbautokab' // database name
};
Upvotes: 3