Skip to content

5.6-php7.2-apache + : Database Connection Issue WP-CLI #728

@briannezhad

Description

@briannezhad

docker-compose.yml file example:

version: "3.3"
services:
  db:
    image: mysql:5.7
    ports:
      - 3306:3306
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
  wordpress:
    depends_on:
      - db
    build: .
    ports:
      - 80:80
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_TABLE_PREFIX: "wp_"
      WORDPRESS_DEBUG: 1
      WORDPRESS_CONFIG_EXTRA: define( 'WP_DEFAULT_THEME', 'default' );
    volumes:
      - wp_data:/var/www/html
      - ./posttypes.txt:/var/www/html/posttypes.txt
      - ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
      - ./default:/var/www/html/wp-content/themes/default/
      - ./wp-content:/var/www/html/wp-content/
      - ./.htaccess:/var/www/html/.htaccess
  wordpress-cli:
    depends_on:
      - db
      - wordpress
    image: wordpress:cli
    user: xfs
    command: >
      /bin/sh -c '
      sleep 10;
      wp core multisite-install --path="/var/www/html" --url="http://localhost" --title="Intranet" --admin_user=admin --admin_password=admin --admin_email=admin@admin.admin --skip-email;
      wp core update-db --network --allow-root
      wp theme enable default --network --activate;
      wp site list --field=url | xargs -n1 -I {} sh -c "wp --url={} theme activate default";
      wp site list --field=url | xargs -n1 -I % wp --url=% relevanssi index
      wp plugin delete hello akismet hello-dolly;
      wp theme delete twentysixteen twentyseventeen twentynineteen twentytwenty twentytwentyone twentytwentytwo;
      wp plugin update --all;
      wp plugin activate --all --network;
      wp redirection database upgrade;
      wp option update permalink_structure "/%postname%/";
      wp config set DISALLOW_FILE_EDIT true --raw;
      wp config set ALLOW_UNFILTERED_UPLOADS true --raw;
      wp rewrite flush;
      '
    volumes:
      - wp_data:/var/www/html
      - ./posttypes.txt:/var/www/html/posttypes.txt
      - ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
      - ./default:/var/www/html/wp-content/themes/default/
      - ./wp-content:/var/www/html/wp-content/
      - ./.htaccess:/var/www/html/.htaccess
volumes:
  db_data:
  # vstm: add shared volume
  wp_data:

Dockerfile

#Base operating System
FROM library/wordpress:php7.4

#install wp-cli
RUN curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar && \
    chmod +x wp-cli.phar && \
    mv wp-cli.phar /usr/local/bin/wp

# Install Postfix.
RUN apt-get update -y && \
      echo "postfix postfix/main_mailer_type string Internet site" > preseed.txt && 
# Use Mailbox format.
      debconf-set-selections preseed.txt && \
      DEBIAN_FRONTEND=noninteractive apt-get install -q -y postfix

# Install net-tools
RUN apt-get install net-tools

# Install Ghostscript & ImageMagick
RUN apt-get update -y
RUN apt-get install -y ghostscript
RUN apt-get install -y imagemagick

RUN echo "sendmail_path=sendmail -t -i" >> /usr/local/etc/php/conf.d/sendmail.ini \
 && echo '#!/bin/bash' >> /usr/local/bin/docker-entrypoint-wrapper.sh \
 && echo 'service postfix restart' >> /usr/local/bin/docker-entrypoint-wrapper.sh \
 && echo 'exec docker-entrypoint.sh "$@"' >> /usr/local/bin/docker-entrypoint-wrapper.sh \
 && chmod +x /usr/local/bin/docker-entrypoint-wrapper.sh

# Grant postdrop permission to www-data
RUN usermod -aG postdrop www-data

# Install packages: wget
RUN apt-get install -y wget

#copy built files to WordPress files system
COPY default  /var/www/html/wp-content/themes/default
COPY ts-child  /var/www/html/wp-content/themes/ts-child

#copy wp plugin install script
COPY wp-plugins-install.sh  /var/www/html/

ENTRYPOINT ["docker-entrypoint-wrapper.sh"]
CMD ["apache2-foreground"]

The WordPress container and MySQL get created perfectly, but when we get to wordpress:cli we get the following error:

wordpress-cli_1  | Error: Error establishing a database connection. This either means that the username and password information in your `wp-config.php` file is incorrect or that contact with the database server at `mysql` could not be established. This could mean your host’s database server is down.
wordpress-cli_1  | Error: Error establishing a database connection.

This only happens in any image above 5.6-php7.2-apache. Any idea how to resolve this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions