You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

同Bridge网络下Node.js容器无法连接MySQL容器的解决求助

How to Connect Node.js Docker Container to MySQL Docker Container on Windows

Hey there, let's break down why you're hitting that ECONNREFUSED error and fix it step by step.

The Root Cause

When you run your Node.js app locally (in CMD), 127.0.0.1 points to your Windows host machine, which has port 3306 mapped to the MySQL container—so that works. But inside your own Docker container, 127.0.0.1 refers to the container's own network stack, not your host or the MySQL container. That's why your app can't find the MySQL service there.

Fixes (Ordered by Recommendation)

1. Use the MySQL Container Name as the Hostname (Best Practice)

Docker's default bridge network has built-in DNS resolution for container names. This means you can use mysql (the name you gave your MySQL container) directly as the database host in your Node app's config, instead of 127.0.0.1.

Here's what to do:

  • Update your Node app's database connection settings: change the host from 127.0.0.1 to mysql, keep the port as 3306.
  • If your config is baked into the own image, rebuild the image with the updated config. If you're using a mounted config file, just edit the file and restart the container:
    docker restart own
    

This is the most reliable method because container names stay consistent, even if their internal IPs change after restarts.

2. Use the MySQL Container's Internal IP Address

You already found the MySQL container's IPv4 address (172.17.0.3) via docker inspect bridge. You can use this IP directly in your Node app's connection config:

  • Swap 127.0.0.1 with 172.17.0.3 in your database host setting.
  • Restart the own container or rebuild the image if needed.
    ⚠️ Heads up: If you delete and recreate the MySQL container later, its internal IP might change, so you'll have to update the config again. This is less flexible than using the container name.

3. Use Your Windows Host's IP Address (Not Recommended)

If you really want to use the host's port mapping, you need to use your Windows machine's actual local IP (not 127.0.0.1) inside the own container:

  • Run ipconfig in CMD to get your host's IPv4 address (look for the adapter connected to your network, e.g., 192.168.1.105).
  • Update your Node app's database host to this IP, port 3306.
  • Restart the own container.
    ⚠️ This method isn't ideal because your host's IP might change (e.g., if you switch networks), and container-to-container communication via the host adds unnecessary overhead.

Verify the Fix

After updating your config, restart the own container and test accessing localhost:80 again. You can also test network connectivity from inside the own container to confirm:

docker exec -it own ping mysql

If the ping succeeds, the network path is working—double-check your app's connection config if you still have issues.

内容的提问来源于stack exchange,提问作者A1x1onlive

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:43:48