Docker容器应用连接同主机MySQL容器失败,报CommunicationsException
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.1refers to the container’s own loopback interface, not the host machine or the MySQL container. Even with--link mysql, using127.0.0.1in 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.cnfand changebind-address=127.0.0.1tobind-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
youruserandyourpasswordwith 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 withdocker network inspect bridgeto see if both containers are listed).
- 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
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

