I created a NodeJS backend server connected to MySQL. Using Docker, I created an image to run my nodeJS and package.json files (which includes MySQL) using the following commands. Here is my Dockerfile:
FROM node WORKDIR /app COPY package.json . RUN npm install COPY . /app EXPOSE 3000 CMD ["node", "app.js"]
I have another file to create a connection to MySQL using NodeJS:
const mysql = require("mysql"); const con = mysql.createConnection({ host: "localhost", user: "root", password: "ilovestackoverflow", database: "db830", port: "3306" }); con.connect(function (err, rows) { if (err) throw err; console.log("Database is connected!"); }); module.exports = con;
I tried to run Dockers (docker run -p 3000:3000 help:help), but got the following error (don't know how to solve it, please help!!):
Server is listening on Port: 3000 /app/config/database.js:12 if (err) throw err; ^ Error: connect ECONNREFUSED 127.0.0.1:3306 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1247:16) -------------------- at Protocol._enqueue (/app/node_modules/mysql/lib/protocol/Protocol.js:144:48) at Protocol.handshake (/app/node_modules/mysql/lib/protocol/Protocol.js:51:23) at Connection.connect (/app/node_modules/mysql/lib/Connection.js:116:18) at Object.<anonymous> (/app/config/database.js:11:5) at Module._compile (node:internal/modules/cjs/loader:1120:14) at Module._extensions..js (node:internal/modules/cjs/loader:1174:10) at Module.load (node:internal/modules/cjs/loader:998:32) at Module._load (node:internal/modules/cjs/loader:839:12) at Module.require (node:internal/modules/cjs/loader:1022:19) at require (node:internal/modules/cjs/helpers:102:18) { errno: -111, code: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.1', port: 3306, fatal: true }
In a multi-container environment, there are multiple networks in the docker container, and each container has its own network namespace. When you need to access other Docker containers, networking in Docker is done through service names. In the docker-compose settings, you will reference the name of the service. When using regular docker containers, it's a bit more difficult. you need to
docker network creates awesomeNetwork
docker network connectionnodebackend AwesomeNetwork
docker network connection db AwesomeNetwork
You can now reference another container within the container network using the name
db/nodebackend
respectively.In a development/local
docker-compose
environment, I highly recommend usingdocker-compose
and writing your compose manifest. Then you don't need to create the network as it will be created for you every time. The setup looks like thisNow you can connect to
db
in your node application using the hostname
db