【发布时间】:2020-01-03 20:02:42
【问题描述】:
我正在尝试创建一个使用 sequelize 来管理 MySQL 数据库的 nodejs 应用程序。为此,我使用了 sequelize-cli。
当我运行 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
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
数据库.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": {
"start": "npx sequelize db:migrate && npx sequelize db:seed:all && node src/app/index.js"
},
当我运行docker-compose up -d --build 时,我希望看到容器正常运行。 docker-compose 创建的数据库容器运行正常,而应用容器运行不正常。
运行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 将数据库容器 IP 注入到我的应用容器环境变量中,为什么我仍然会拒绝连接?
【问题讨论】:
-
你能发布你的sequlize代码吗?
标签: mysql node.js docker docker-compose sequelize.js