I create a app that uses mysql database but When I Dockerize My Services I cant Access to my containers from each other even i create a same network and put my service name in my database config
here is my docker compose and database config
version: "3.8"
services:
db:
container_name: mydb
image: mysql:latest
restart: unless-stopped
cap_add:
- SYS_NICE
environment:
- MYSQL_ROOT_PASSWORD=12345678
- MYSQL_DATABASE=something
- MYSQL_USER=something
- MYSQL_PASSWORD=12345678
volumes:
- db:/var/lib/mysql
network_mode: "host"
nodeserver:
depends_on:
- db
build:
context: ./app
network_mode: "host"
volumes:
db:
# pull the Node.js Docker image
FROM node:alpine
# create the directory inside the container
WORKDIR /usr/src/app
# copy the package.json files from local machine to the workdir in container
COPY package*.json ./
# run npm install in our local machine
RUN npm install
# copy the generated modules and all other files to the container
COPY . .
# our app is running on port 5000 within the container, so need to expose it
EXPOSE 5000
# the command that starts our app
CMD ["node", "server.js"]
erver_1 | ConnectionRefusedError [SequelizeConnectionRefusedError]: connect ECONNREFUSED 127.0.0.1:3306
nodeserver_1 | at ConnectionManager.connect (/usr/src/app/node_modules/sequelize/dist/lib/dialects/mysql/connection-manager.js:92:17)
nodeserver_1 | at processTicksAndRejections (node:internal/process/task_queues:96:5)
nodeserver_1 | at async ConnectionManager._connect (/usr/src/app/node_modules/sequelize/dist/lib/dialects/abstract/connection-manager.js:216:24)
nodeserver_1 | at async /usr/src/app/node_modules/sequelize/dist/lib/dialects/abstract/connection-manager.js:174:32
nodeserver_1 | at async ConnectionManager.getConnection (/usr/src/app/node_modules/sequelize/dist/lib/dialects/abstract/connection-manager.js:197:7)
nodeserver_1 | at async /usr/src/app/node_modules/sequelize/dist/lib/sequelize.js:303:26
nodeserver_1 | at async MySQLQueryInterface.createTable (/usr/src/app/node_modules/sequelize/dist/lib/dialects/abstract/query-interface.js:94:12)
nodeserver_1 | at async Function.sync (/usr/src/app/node_modules/sequelize/dist/lib/model.js:913:5)
nodeserver_1 | at async Sequelize.sync (/usr/src/app/node_modules/sequelize/dist/lib/sequelize.js:377:9)
nodeserver_1 | at async initialize (/usr/src/app/_helpers/db.js:36:5) {
nodeserver_1 | parent: Error: connect ECONNREFUSED 127.0.0.1:3306
nodeserver_1 | at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1187:16) {
nodeserver_1 | errno: -111,
nodeserver_1 | code: 'ECONNREFUSED',
nodeserver_1 | syscall: 'connect',
nodeserver_1 | address: '127.0.0.1',
nodeserver_1 | port: 3306,
nodeserver_1 | fatal: true
nodeserver_1 | },
nodeserver_1 | original: Error: connect ECONNREFUSED 127.0.0.1:3306
nodeserver_1 | at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1187:16) {
nodeserver_1 | errno: -111,
nodeserver_1 | code: 'ECONNREFUSED',
nodeserver_1 | syscall: 'connect',
nodeserver_1 | address: '127.0.0.1',
nodeserver_1 | port: 3306,
nodeserver_1 | fatal: true
nodeserver_1 | }
after run my projects with docker-compose up
i got this error
2
Answers
The error is caused by not providing the hostname into the Sequelize options and it defaults to localhost if you don’t. You need to do something like the following:
Reference (see Option #3): https://sequelize.org/master/manual/getting-started.html#connecting-to-a-database