wordpressdockerdocker-composewp-cli

wp-cli in Docker fetches wrong database login credentials from wp-config


Here is my docker-compose.yml file:

version: '3.9'

services:
  db:
    image: mysql:5.7
    restart: always
    command: [
        '--disable-partition-engine-check',
        '--default_authentication_plugin=mysql_native_password',
        '--character-set-server=utf8mb4',
        '--collation-server=utf8mb4_unicode_ci',
        '--max_allowed_packet=100M'
    ]
    volumes:
      - ./db_data:/var/lib/mysql
    environment:
      MYSQL_DATABASE: wordpress
      MYSQL_ROOT_PASSWORD: pswd4!


  pma:
    image: phpmyadmin/phpmyadmin
    environment:
      PMA_HOST: db
      PMA_PORT: 3306
      MYSQL_ROOT_PASSWORD: pswd4!
      UPLOAD_LIMIT: 1G
    ports:
      - 8080:80
    depends_on:
      - db

  wp:
    image: wordpress:php8.0
    ports:
      - 80:80
    volumes:
      - ./config/php.conf.ini:/usr/local/etc/php/conf.d/conf.ini
      - ./:/var/www/html
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: pswd4!
      WORDPRESS_DEBUG: 1

    depends_on:
      - db
    links:
      - db:mysql
    hostname: test.localhost

  wpcli:
    image: wordpress:cli-php8.0
    volumes_from:
      - wp
    depends_on:
      - db
      - wp
    links:
      - db:mysql
    entrypoint: wp
    command: "--info"



volumes:
  db_data:

When I try to use wp-cli in Docker (e.g. docker-compose run --rm wpcli plugin list), it gets an error that it cannot connect to the database:

Error: `Access denied for user 'username_here'@'192.168.32.5' (using password: YES)`
Error establishing a database connection
This either means that the username and password information in your `wp-config.php` file is incorrect or we can’t contact the database server at `mysql`. This could mean your host’s database server is down.

Are you sure you have the correct username and password?
Are you sure you have typed the correct hostname?
Are you sure the database server is running?

If you’re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the WordPress Support Forums. `Access denied for user 'username_here'@'192.168.32.5' (using password: YES)
`

It looks like wp-cli is seeing bad database credentials (username_here instead of root)

Result of executing docker-compose run --rm wpcli config list command: enter image description here

What could be wrong? I've searched all over the internet, lost several hours and still haven't found the cause of the problem.


Solution

  • You should specify the same set of environment variables as in your wp container for your wpcli container. If not, default variables from a php file in wordpress are used.

    Do be careful : volumes_from and link options are deprecated in compose v3. For the link option, docker-compose creates a network automatically (or you can specify one if you prefer) and the embed docker dns automatically attributes aliases to your containers based on their names (or in compose the service name). More info on that here

    For volumes, you can find more info here