Docker Compose connect ECONNREFUSED 172.18.0.4:3306(Docker Compose 连接 ECONNREFUSED 172.18.0.4:3306)
问题描述
当我用这个命令构建项目的容器时:
sudo docker build -t PROJECT_NAME .
然后我通过这个 Docker-Compose 配置下载了 mysql 的镜像:
数据库:图片:mysql重启:总是端口:- 8999:3306"网络:- 网络环境:MYSQL_DATABASE:松弛MYSQL_ROOT_PASSWORD:管理员
然后,项目将通过
我该如何解决这个问题?
Sequelize 配置是这样的:
const sequelize = new Sequelize(process.env.TEST_DB || 'slack', 'root', 'admin', {主机:process.env.DB_HOST ||'本地主机',运算符别名:Sequelize.Op,方言:'mysql',定义: {下划线:真}});
网络的配置是这样的:
网页:图片:松弛克隆服务器端口:- 8080:8080"网络:- 网络环境:DB_HOST:分贝REDIS_HOST:redis命令:[./wait-for-it.sh"、db:3306"、--"、npm"、run"、prod"]
名为等待它"的脚本是 这个.
如果有人需要完整的代码,给你:
续集配置
Docker Compose 配置
Dockerfile 配置
wait-for-it.sh
默认等待15 秒 并返回,即使目标尚未准备好.您也可以在输出中看到这一点.但是数据库还没有准备好.制作 wait-for-it.sh
等待更长时间,也许用 -t 90
等待 90 秒或 -t 0
让它永远等待.
(根据我的经验,Docker 数据库容器通常需要 30-60 秒才能启动,尤其是第一次.)
When I build the container of the project with this command:
sudo docker build -t PROJECT_NAME .
And then I download the mysql's image through this Docker-Compose config:
db:
image: mysql
restart: always
ports:
- "8999:3306"
networks:
- webnet
environment:
MYSQL_DATABASE: slack
MYSQL_ROOT_PASSWORD: admin
And then, the project will connect with MySQL through the Sequelize ORM
I have this error:
Unhandled rejection SequelizeConnectionRefusedError: connect ECONNREFUSED 172.18.0.4:3306
How can I resolve this?
The Sequelize config is this:
const sequelize = new Sequelize(process.env.TEST_DB || 'slack', 'root', 'admin', {
host: process.env.DB_HOST || 'localhost',
operatorsAliases: Sequelize.Op,
dialect: 'mysql',
define: {
underscored: true
}
});
The config of the web is this:
web:
image: slack-clone-server
ports:
- "8080:8080"
networks:
- webnet
environment:
DB_HOST: db
REDIS_HOST: redis
command: ["./wait-for-it.sh", "db:3306", "--", "npm", "run", "prod"]
The script called "wait for it" is this.
If someone needs the complete code, here you go:
Sequelize config
Docker Compose config
Dockerfile config
wait-for-it.sh
by default waits for 15 seconds and returns, even if the target isn't ready yet. You see that in your output too. But the database isn't ready yet. Make wait-for-it.sh
wait longer, maybe with -t 90
to wait for 90 seconds or -t 0
to make it wait forever.
(In my experience the Docker database containers routinely take 30-60 seconds to start up, especially the first time.)
这篇关于Docker Compose 连接 ECONNREFUSED 172.18.0.4:3306的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Docker Compose 连接 ECONNREFUSED 172.18.0.4:3306
基础教程推荐
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01