Score:1

Can't connect Wordpress and MariaDB on LAMP stack with docker-compose

se flag

I'm new here and that's my 1st question so tell me if anything is wrong in the way I put it.

So here's the problem: I'm building a LAMP stack with 3 separate containers for nginx, maiadb and wordpress.

The whole nginx and php-fpm seems to work well, as I can access the site's index and process php pages. Mdb builds the wordpress db and creates a "wordpress@wordpress-php" user with all privileges on it.

It starts to go wrong when I access the site for installation, I get stuck with a : "Error establishing a database connection"

Here is my wp-config.php, that I copy in the wordpress dir inside the container:

 <?php                                                          

  4 define( 'DB_NAME', 'wordpress' );
  7 define( 'DB_USER', 'wordpress' );
 10 define( 'DB_PASSWORD', 'wordpress' );

 15 define( 'DB_HOST', 'mariadb' );                                                                                                                                                      
 16 define( 'DB_CHARSET', 'utf8' );                                                 
 17                                                                                              
 19 define( 'DB_COLLATE', '' );                                                     
 20 define('AUTHOR', 'yotillar');                                                   
 21                                                                                 
 22 // Authentication unique keys and salts.                                        
 23 define( 'AUTH_KEY',         'key' );
 24 define( 'SECURE_AUTH_KEY',  'key' );
 25 define( 'LOGGED_IN_KEY',    'key' );
 26 define( 'NONCE_KEY',        'key' );
 27 define( 'AUTH_SALT',        'salt' );                                    
 28 define( 'SECURE_AUTH_SALT', 'salt' );
 29 define( 'LOGGED_IN_SALT',   'salt' );
 30 define( 'NONCE_SALT',       'salt' );
 31                                                                                                               
 38 $table_prefix = 'wp_';       
        
 51 define( 'WP_DEBUG', true );                                    
 52 define( 'WP_DEBUG_LOG', true );                                
 53 define( 'WP_DEBUG_DISPLAY', false );                           
                                                                  
 59 if ( ! defined( 'ABSPATH' ) ) {                                
 60 »···define( 'ABSPATH', __DIR__ . '/' );                        
 61 }                                                                                            
 64 require_once ABSPATH . 'wp-settings.php';                      
 65 ?>                                            

Here's my docker-compose.yaml:

version: "3.5"                                                                  
  2                                                                                 
  3 networks:                                                                       
  4   front-network:                                                                
  5     driver: bridge                                                              
  6     attachable: false                                                           
  7   back-network:                                                                 
  8     driver: bridge                                                              
  9     attachable: false                                                           
 10                                                                                 
 11 volumes:                                                                          
 12   mdb-data:                                                                     
 13   wordpress:                                                                    
 14                                                                                 
 15 services:                                                                       
 16                                                                                 
 17   nginx:                                                                        
 18     depends_on:                                                                 
 19       - wordpress-php                                                           
 20     build: "./services/nginx"                                                   
 21     image: nginx:mytag                                                          
 22     volumes:                                                                    
 23       - wordpress:/var/www/myserver/wordpress:rw                                
 24       - "../logs/nginx_logs:/var/log/nginx:rw"                                  
 25     restart: on-failure                                                         
 26     ports:                                                                      
 27       - "80:80"                                                                 
 28       - "443:443"                                                               
 29     expose:                                                                     
 30       - 9000                                                                    
 31     networks:                                                                   
 32       - front-network                                                           
 33                                                                                 
 34                                                                                 
 35   wordpress-php:                                                                
 36     depends_on:                                                                 
 37       - mariadb                                                                 
 38     build: "./services/wordpress"                                               
 39     image: wordpress-php:mytag                                                  
 40     volumes:                                                                    
 41       - wordpress:/var/www/myserver/wordpress:rw
 42       - "../logs/php7.3-fpm.log:/var/log/php7.3-fpm.log:rw"                        
 43     restart: on-failure                                                         
 44     environment:                                                                
 45       - WORDPRESS_DB_HOST=mariadb:3306                                          
 46       - WORDPRESS_DB_NAME=wordpress                                             
 45       - WORDPRESS_DB_USER=wordpress                                             
 48       - WORDPRESS_DB_PASSWORD=wordpress                                                                                      
 50     expose:                                                                     
 51       - 3306                                                                    
 52       - 9000                                                                    
 53     networks:                                                                   
 54       - front-network                                                           
 55       - back-network                                                            
 56                                                                                 
 57                                                                                 
 58   mariadb:                                                                      
 59     build: "./services/mariadb"                                                 
 60     image: mariadb:mytag                                                        
 61     volumes:                                                                    
 62       - mdb-data:/var/lib/mysql:rw                                              
 63       - "../logs/mariadb_logs/error.log:/var/log/mysql/error.log"               
 64     restart: on-failure                                                         
 65     environment:                                                                
 66       - MYSQL_DATABASE=wordpress                                                
 67       - MYSQL_USER=wordpress                                                    
 68       - MYSQL_USER_PASSWORD=wordpress                                           
 69       - MYSQL_ROOT_PASSWORD=root                                                
 70     expose:                                                                     
 71       - 3306                                                                    
 72     networks:                                                                   
 73       - back-network                                                            
 74                                                                                 

I set the bind-address of mariadb on 0.0.0.0 in ./etc/mysql/mariadb.conf.d/50-se rver.cnf and ./etc/mysql/my.cnf to be sure that th db is indeed listening from every ip.

I think the problem comes from the wp-config.php , but as I understood in a docker-compose environment you can refer to each container with his name instead of ip, and I don't know how I could retrieve mariadb's IP from wordpress-php.

Please help me, thanks in advance to anyone that read that far! ^^

Michael Hampton avatar
cz flag
Check the php-fpm error log for any clues. You will probably have to enter the container to get access to it as you did not export it in a volume.
Youn avatar
se flag
It says :[25-Jul-2021 00:06:47] NOTICE: fpm is running, pid 1 [25-Jul-2021 00:06:47] NOTICE: ready to handle connections [25-Jul-2021 00:06:47] NOTICE: systemd monitor interval set to 10000ms

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.