[英]Docker Compose connect ECONNREFUSED 172.18.0.4:3306
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:然后我通过这个 Docker-Compose 配置下载 mysql 的镜像:
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然后,项目将通过Sequelize ORM连接 MySQL
I have this error:我有这个错误:
Unhandled rejection SequelizeConnectionRefusedError: connect ECONNREFUSED 172.18.0.4:3306
How can I resolve this?我该如何解决这个问题?
The Sequelize config is this: Sequelize 配置是这样的:
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:如果有人需要完整的代码,请访问:
wait-for-it.sh
by default waits for 15 seconds and returns, even if the target isn't ready yet. wait-for-it.sh
默认等待15 秒并返回,即使目标尚未准备好。 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.使
wait-for-it.sh
等待更长时间,可能使用-t 90
等待 90 秒或使用-t 0
使其永远等待。
(In my experience the Docker database containers routinely take 30-60 seconds to start up, especially the first time.) (根据我的经验,Docker 数据库容器通常需要 30-60 秒才能启动,尤其是第一次。)
David already said that the db is not ready at this moment.大卫已经说过,此时数据库尚未准备好。 You can use depends_on in your docker-compose file as well.
您也可以在docker -compose 文件中使用depends_on 。 This will start and initialize the db first and then start the "web" container.
这将首先启动并初始化数据库,然后启动“web”容器。
services:
web:
...
depends_on:
- db
db:
image: mariadb:latest
...
db:
container_name: mysqldb //or whatever you want <<Add This
image: mysql
restart: always
ports:
- "8999:3306"
networks:
- webnet
environment:
MYSQL_DATABASE: slack
MYSQL_ROOT_PASSWORD: admin
And Then assign the value of process.env.DB_HOST
to Mysql Container name然后将
process.env.DB_HOST
的值赋给Mysql Container name
Like: process.env.DB_HOST=mysqldb
像:
process.env.DB_HOST=mysqldb
Assign the value of process.env.DB_HOST to Mysql Container name将 process.env.DB_HOST 的值赋给 Mysql Container name
Like: process.env.DB_HOST=mysqldb像:process.env.DB_HOST=mysqldb
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.