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

Docker容器应用连接同主机MySQL容器失败,报CommunicationsException

Troubleshooting Docker Container-to-Container MySQL Connection Issues

Hey there, I’ve walked through your scenario and can spot a few key issues that are preventing your app container from connecting to MySQL. Let’s break down the fixes and troubleshooting steps:

Core Problem Breakdown

The main issue here is how you’re addressing the MySQL container from your app container:

  • Inside a Docker container, 127.0.0.1 refers to the container’s own loopback interface, not the host machine or the MySQL container. Even with --link mysql, using 127.0.0.1 in your JDBC URL makes the app try to connect to its own 3306 port (which isn’t running MySQL).
  • Additionally, your MySQL configuration is set to only listen on its internal loopback (bind-address=127.0.0.1), blocking connections from other containers.

Step-by-Step Fixes & Troubleshooting

1. Fix MySQL’s Network Listening Configuration

You’ll need to update MySQL to accept connections from any network interface (so other containers can reach it):

  • Enter your MySQL container: docker exec -it mysql bash
  • Edit /etc/my.cnf and change bind-address=127.0.0.1 to bind-address=0.0.0.0
  • Save the file and restart the MySQL container: docker restart mysql

2. Update Your App’s JDBC URL to Use the Linked Container Name

When you use --link mysql, Docker adds a hostname entry for mysql in your app container’s /etc/hosts file, pointing directly to the MySQL container’s IP. Update your app’s startup command to use this hostname instead of 127.0.0.1:

docker run -d ... -e DB_URL=jdbc:mysql://mysql:3306/mydatabase --name myApp --restart=always --link mysql myApp_t:latest

3. Verify MySQL User Permissions

Even if you set up permissions for local access, you need to ensure your MySQL user is allowed to connect from other containers (which are on a different network address):

  • Enter the MySQL container and log into MySQL: mysql -u root -p
  • Run these commands (replace youruser and yourpassword with your actual credentials):
    GRANT ALL PRIVILEGES ON mydatabase.* TO 'youruser'@'%' IDENTIFIED BY 'yourpassword';
    FLUSH PRIVILEGES;
    

The % wildcard allows the user to connect from any address, which works for container-to-container communication.

4. Test Container-to-Container Network Connectivity

To confirm the network link is working:

  • Enter your app container: docker exec -it myApp bash
  • Ping the MySQL container using the linked hostname: ping mysql
    • If you get responses, the network is working. If not, check that both containers are on the same Docker network (the default bridge network should work with --link, but you can verify with docker network inspect bridge to see if both containers are listed).

5. Check Firewall/Port Forwarding (If Needed)

Since your local Workbench can connect via 127.0.0.1:3306, you likely have port forwarding set up for the MySQL container, but this isn’t needed for container-to-container communication. Just ensure you’re relying on Docker’s internal network for the app-to-MySQL connection, not the host’s port.

After making these changes, restart your app container and check the logs again—this should resolve the communications link failure.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 07:27:31