When connecting to a MySQL container from another container, it's crucial to avoid using the IP address of the MySQL container.
Alternative Solution: Docker Network
Instead, connect using user-defined Docker networks. This method is more reliable and efficient than relying on IP addresses.
Create a network using the docker network create command:
docker network create my_network
Run both the MySQL container and the other container on the same network:
docker run -d --name php_container --network my_network my_php_image docker run -d --name mysql_container --network my_network my_mysql_image
Within the PHP container, connect to MySQL using the hostname of the MySQL container:
<code class="php">$mysqli = new mysqli("mysql_container", "mattia", "prova", "prova");</code>
This method ensures that the PHP container can always access the MySQL container, even if the IP addresses change. The hostname will always resolve to the correct IP address on the network.
The above is the detailed content of Here are a few title options, keeping in mind the question format and focusing on the solution: * **How to Connect to a MySQL Container from Another Container Without Using its IP Address?** * **What. For more information, please follow other related articles on the PHP Chinese website!