Docker Compose 连接 ECONNREFUSED 172.18.0.4:3306

Docker Compose connect ECONNREFUSED 172.18.0.4:3306(Docker Compose 连接 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

基础教程推荐