nezhar / wordpress-docker-compose

Easy Wordpress development with Docker and Docker Compose
MIT License
1.83k stars 1.31k forks source link

WP-CLI: Error establishing a database connection #69

Open joelsmith80 opened 3 years ago

joelsmith80 commented 3 years ago

Not sure what I'm doing wrong here. I'm following all of the instructions, not changing any default variables, and while WordPress and PHPMyAdmin work perfectly, I can't get wp-cli to work. Any time I run the command in the instructions, I get Error establishing a database connection. Any time I try to restart it via the Docker dashboard, I get the same error. What gives? What database credentials is wp-cli using? What else can I do to configure this?

If it makes a difference, I'm on a new MacBook Pro with the M1 chip. (This has caused problems with other programs; don't know if it's an issue here.)

Snk74 commented 3 years ago

Hey, Same here, just wait few minutes and it works fine for me

satcar77 commented 3 years ago

Since March 2021, WordPress images use a customized wp-config.php that pulls the values directly from the environment variables defined above (see wp-config-docker.php in docker-library/wordpress#572 and docker-library/wordpress#577). As a result of reading environment variables directly, the cli container also needs the same set of environment variables to properly evaluate wp-config.php.

Pass in the same environment variables from wordpress service to the wpcli and it should work.

nguyenlamlll commented 3 years ago

@satcar77 May you please explain a little bit more? I don't get the part "pass in the same environment variables". I think I have cloned the branch master, simply docker-compose up -d and then docker-compose run --rm wpcli plugin list as the readme file says, but it does not work. Got the same database connection error.

nguyenlamlll commented 3 years ago

Ok, silly me. I'm sorry. But I think I got what is required. The wpcli section in my docker-compose.yml does not have the matching environment section as wp's. So, I ensure everything is matching.

services:
  wp:
    image: wordpress:5
    ports:
      - ${IP}:80:80
    volumes:
      - ./config/php.conf.ini:/usr/local/etc/php/conf.d/conf.ini
      - ./wp-app:/var/www/html # Full wordpress project
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_NAME: "${DB_NAME}"
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: "${DB_ROOT_PASSWORD}"
    depends_on:
      - db
    links:
      - db

  wpcli:
    image: wordpress:cli
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_NAME: "${DB_NAME}"
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: "${DB_ROOT_PASSWORD}"
    volumes:
      - ./config/php.conf.ini:/usr/local/etc/php/conf.d/conf.ini
      - ./wp-app:/var/www/html
    depends_on:
      - db
      - wp

Then, docker-compose run --rm wpcli user list. It works!

By the way, I've just seen your PR #70. I see it now. Thank you a lot!

bondansebastian commented 11 months ago

Hi there, I'm experiencing the same issue on Windows WSL 2, using Ubuntu-20.4 distro. Here's my .docker-compose.yml:

version: '3'

services:
  wp:
    image: wordpress:latest # https://hub.docker.com/_/wordpress/
    ports:
      - ${IP}:${PORT}:80 # change ip if required
    volumes:
      - ./config/php.conf.ini:/usr/local/etc/php/conf.d/conf.ini
      - ./wp-app:/var/www/html # Full wordpress project
      - ./joy-booking-plugin/:/var/www/html/wp-content/plugins/joy_booking # Plugin development
      #- ./theme-name/trunk/:/var/www/html/wp-content/themes/theme-name # Theme development
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_NAME: "${DB_NAME}"
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: "${DB_ROOT_PASSWORD}"
      WORDPRESS_DEBUG: "${WORDPRESS_DEBUG}"
    depends_on:
      - db
    links:
      - db

  wpcli:
    image: wordpress:cli
    volumes:
      - ./config/php.conf.ini:/usr/local/etc/php/conf.d/conf.ini
      - ./wp-app:/var/www/html
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_NAME: "${DB_NAME}"
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: "${DB_ROOT_PASSWORD}"
    depends_on:
      - db
      - wp

  pma:
    image: phpmyadmin/phpmyadmin
    environment:
      # https://docs.phpmyadmin.net/en/latest/setup.html#docker-environment-variables
      PMA_HOST: db
      PMA_PORT: 3306
      MYSQL_ROOT_PASSWORD: "${DB_ROOT_PASSWORD}"
      UPLOAD_LIMIT: 50M
    ports:
      - ${IP}:8080:80
    links:
      - db:db

  db:
    image: mysql:latest # https://hub.docker.com/_/mysql/ - or mariadb https://hub.docker.com/_/mariadb
    ports:
      - ${IP}:3306:3306 # change ip if required
    command: [
        '--default_authentication_plugin=mysql_native_password',
        '--character-set-server=utf8mb4',
        '--collation-server=utf8mb4_unicode_ci'
    ]
    volumes:
      - ./wp-data:/docker-entrypoint-initdb.d
      - db_data:/var/lib/mysql
    environment:
      MYSQL_DATABASE: "${DB_NAME}"
      MYSQL_ROOT_PASSWORD: "${DB_ROOT_PASSWORD}"

volumes:
  db_data: