同Bridge网络下Node.js容器无法连接MySQL容器的解决求助
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.1tomysql, keep the port as 3306. - If your config is baked into the
ownimage, 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.1with172.17.0.3in your database host setting. - Restart the
owncontainer 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
ipconfigin 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
owncontainer.
⚠️ 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

