The issue where the intranet address of MySQL referenced in a Dockerfile does not take effect typically occurs due to incorrect network configuration, service discovery problems, or timing issues during container startup. Here's how to solve it:
Incorrect MySQL Host Address
mysql).Container Network Isolation
docker network create my_network
docker run --name mysql --network my_network -e MYSQL_ROOT_PASSWORD=123456 -d mysql:latest
docker run --name app --network my_network -d your_app_image
MySQL Service Not Ready When Application Starts
until mysqladmin ping -h mysql --silent; do
sleep 1
done
Environment Variables Not Passed Correctly
docker run -e MYSQL_HOST=mysql -e MYSQL_PORT=3306 -d your_app_image
Docker Compose Configuration
docker-compose.yml:version: '3'
services:
mysql:
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: 123456
app:
image: your_app_image
depends_on:
- mysql
environment:
MYSQL_HOST: mysql
MYSQL_PORT: 3306
If you're deploying this setup on Tencent Cloud, consider using Tencent Cloud Container Service (TKE) or Tencent Cloud Database for MySQL for managed MySQL instances.
For example, in a Tencent Cloud TKE cluster, you can deploy MySQL as a separate service and reference it by its service name in your application containers.