Technology Encyclopedia Home >The intranet address of MySQL referenced in Dockerfile does not take effect. How to solve it?

The intranet address of MySQL referenced in Dockerfile does not take effect. How to solve it?

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:

Possible Causes and Solutions:

  1. Incorrect MySQL Host Address

    • The Dockerfile or application code may reference a hardcoded IP or hostname that doesn't resolve correctly in the container network.
    • Solution: Use Docker's built-in DNS or service names instead of hardcoding IPs. For example, if MySQL runs in another container, reference it by its service name (e.g., mysql).
  2. Container Network Isolation

    • If MySQL and the application are in different networks, they can't communicate.
    • Solution: Ensure both containers are in the same Docker network. Create a custom network and connect both containers to it:
      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
      
  3. MySQL Service Not Ready When Application Starts

    • The application may try to connect to MySQL before it's fully initialized.
    • Solution: Add a retry mechanism in the application or use a script to wait for MySQL to be ready before starting the app. For example, a simple bash script can check MySQL's availability:
      until mysqladmin ping -h mysql --silent; do
        sleep 1
      done
      
  4. Environment Variables Not Passed Correctly

    • The application may rely on environment variables for the MySQL address, but they are not set properly.
    • Solution: Verify that the correct environment variables are passed to the container. For example:
      docker run -e MYSQL_HOST=mysql -e MYSQL_PORT=3306 -d your_app_image
      
  5. Docker Compose Configuration

    • If using Docker Compose, ensure the services are correctly linked and the network is shared.
    • Example 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
      

Using Tencent Cloud Services for MySQL in Containers:

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.

  • TKE: Simplifies container orchestration and networking, ensuring seamless communication between containers.
  • Tencent Cloud Database for MySQL: Provides a managed MySQL service with high availability and scalability. You can connect your application containers to it using private network addresses or VPC peering.

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.