Error Establishing Database Connection in Dockerized WordPress Setup

I'm encountering an issue with my Dockerized WordPress setup. When I try to access the WordPress site, I get the error: Error establishing a database connection. What I've Tried MySQL Container Status: The MySQL container is running and accessible. Connecting to MySQL DB Environment Variables: The WordPress container is correctly loading the .env variables. Docker Compose Configuration Here's my docker-compose.yml: version: '3.8' services: nginx: image: nginx:stable-alpine container_name: nginx ports: - 80:80 - 443:443 volumes: - ./nginx/conf.d:/etc/nginx/conf.d - ./nginx/certs:/etc/nginx/certs/self-signed - wordpress_data:/var/www/html networks: - wordpress-network mysql: image: mysql:latest container_name: wordpress_db restart: always env_file: .env environment: MYSQL_ROOT_PASSWORD: '${MYSQL_ROOT_PASSWORD}' MYSQL_DATABASE: '${MYSQL_DATABASE}' MYSQL_USER: '${MYSQL_USER}' MYSQL_PASSWORD: '${MYSQL_PASSWORD}' volumes: - ./mysql:/var/lib/mysql - ./confs/mysql/conf.d/my.cnf:/etc/mysql/conf.d/my.cnf networks: - wordpress-network php: build: context: . dockerfile: php.dockerfile container_name: php-8.2 restart: always volumes: - wordpress_data:/var/www/html - ./confs/php/conf.d/php.ini:/usr/local/etc/php/conf.d/custom-php.ini networks: - wordpress-network wordpress: image: wordpress:latest container_name: wordpress_app restart: always ports: - "8080:80" env_file: .env environment: WORDPRESS_DB_HOST: '${WORDPRESS_DB_HOST}' WORDPRESS_DB_USER: '${WORDPRESS_DB_USER}' WORDPRESS_DB_PASSWORD: '${WORDPRESS_DB_PASSWORD}' WORDPRESS_DB_NAME: '${WORDPRESS_DB_NAME}' depends_on: - mysql volumes: - wordpress_data:/var/www/html networks: - wordpress-network wpcli: image: wordpress:cli container_name: wpcli volumes: - wordpress_data:/var/www/html entrypoint: ["sh", "-c"] command: ["wp --allow-root"] depends_on: - wordpress networks: - wordpress-network phpmyadmin: image: phpmyadmin/phpmyadmin:latest container_name: phpmyadmin restart: always environment: PMA_HOST: mysql PMA_PORT: 3306 UPLOAD_LIMIT: 512M ports: - "8081:80" volumes: - ./confs/php/conf.d/php.ini:/usr/local/etc/php/conf.d/custom-php.ini networks: - wordpress-network volumes: wordpress_data: mysql_data: backups: networks: wordpress-network: Debugging I've added the following debugging statement to wp-config.php to check if the .env variables are being loaded: echo '<pre>'; echo 'DB_NAME: ' . getenv('WORDPRESS_DB_NAME') . '<br>'; echo 'DB_USER: ' . getenv('WORDPRESS_DB_USER') . '<br>'; echo 'DB_PASSWORD: ' . getenv('WORDPRESS_DB_PASSWORD') . '<br>'; echo 'DB_HOST: ' . getenv('WORDPRESS_DB_HOST') . '<br>'; echo '</pre>'; The output shows empty values, which indicates that the .env variables aren't being loaded properly. wp-config.php File Here's the relevant part of my wp-config.php: <?php // ... (other configurations) // a helper function to lookup "env_FILE", "env", then fallback if (!function_exists('getenv_docker')) { function getenv_docker($env, $default) { if ($fileEnv = getenv($env . '_FILE')) { return rtrim(file_get_contents($fileEnv), "\r\n"); } else if (($val = getenv($env)) !== false) { return $val; } else { return $default; } } } // ** Database settings - You can get this info from your web host ** // define( 'DB_NAME', getenv_docker('WORDPRESS_DB_NAME', 'wordpress') ); define( 'DB_USER', getenv_docker('WORDPRESS_DB_USER', 'example username') ); define( 'DB_PASSWORD', getenv_docker('WORDPRESS_DB_PASSWORD', 'example password') ); define( 'DB_HOST', getenv_docker('WORDPRESS_DB_HOST', 'mysql') ); // ... (other configurations) Question How can I resolve the issue of .env variables not being loaded in my Dockerized WordPress setup? Is there something specific in my configuration that could be causing this problem? Thank you for your help!

Comment (0)

You’ll be in good company