[英]ECONNREFUSED error on sequelize migration with docker-compose
I'm trying to up a nodejs application that uses sequelize to manage a MySQL database.我正在尝试启动一个使用 sequelize 来管理 MySQL 数据库的 nodejs 应用程序。 To do this, I used sequelize-cli.
为此,我使用了 sequelize-cli。
When I run docker-compose, it builds the images correctly, but, at the time it starts the containers, the sequelize-cli cannot connect into MySQL database and make the migrations.当我运行 docker-compose 时,它会正确构建图像,但是在启动容器时,sequelize-cli 无法连接到 MySQL 数据库并进行迁移。
Dockerfile文件
FROM node:10-alpine
RUN mkdir /usr/app
WORKDIR /usr/app
COPY package.json yarn.lock ./
RUN yarn
COPY . ./
EXPOSE 3333
CMD yarn start
docker-compose.yml docker-compose.yml
version: '3'
services:
database:
image: mysql:5.7
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
- MYSQL_ROOT_PASSWORD=123123
- MYSQL_DATABASE=test
app:
build: .
ports:
- '3333:3333'
command: yarn start
environment:
- DB_HOST=database
- DB_USER=root
depends_on:
- database
database.js数据库.js
module.exports = {
username: process.env.DB_USER || 'test',
password: process.env.DB_PASSWORD || '123123',
database: process.env.DB_NAME || 'test',
host: process.env.DB_HOST || '127.0.0.1',
}
package.json scripts package.json 脚本
"scripts": {
"start": "npx sequelize db:migrate && npx sequelize db:seed:all && node src/app/index.js"
},
When I run docker-compose up -d --build
I expect to see the containers running without problems.当我运行
docker-compose up -d --build
我希望看到容器运行没有问题。 The database container created by docker-compose runs correctly, but the app container don't. docker-compose 创建的数据库容器运行正常,但应用程序容器没有。
Running docker logs <app_container_id>
, I have the following output:运行
docker logs <app_container_id>
,我有以下输出:
yarn run v1.17.3
$ npx sequelize db:migrate && npx sequelize db:seed:all && node src/app/index.js
Sequelize CLI [Node: 10.16.3, CLI: 5.4.0, ORM: 5.8.5]
Loaded configuration file "src/config/database.js".
ERROR: connect ECONNREFUSED 172.27.0.2:3306
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Docker-compose injects the database container IP into my app container environment variables, so why am I still get connection refused? Docker-compose 将数据库容器 IP 注入到我的应用程序容器环境变量中,为什么我的连接仍然被拒绝?
我知道它有点晚了,但是您要做的就是将主机从“172.27.0.2”更改为您的数据库容器名称“数据库”
are you linking your docker containers?您是否正在链接您的 docker 容器? or are they part of the same network?
或者他们是同一个网络的一部分? you need yo verify the link between your database and your app container.
您需要验证数据库和应用程序容器之间的链接。 make sure that you are connecting your 2 containers before running migrations.
确保在运行迁移之前连接 2 个容器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.