Josph
Josph

Reputation: 75

Enabling Wordpress debug mode in docker-compose environment

I'm having problems understanding how to enable wordpress debug mode through docker-compose. image of wp-config.php debug section

define( 'WP_DEBUG', !!getenv_docker('WORDPRESS_DEBUG', '') );

This is the part of the wp-config.php file where I should be able to enable it. The file is read only and it seems to be designed to enable this feature through docker-compose.yaml file but I cant figure out how. I have tried adding WORDPRESS_DEBUG: 'true' to the environment section of wordpress service but it throws error with true/fals data type and 'true' as a string does not seem to work. The only solution I can come up with is to edit the file as superuser.

Upvotes: 7

Views: 10070

Answers (1)

joshmoto
joshmoto

Reputation: 5118

Ok this tripped me up big time a year or so ago. See my original problem question which led onto wp_debug mode being defined twice causing a constant blank php error in the admin...

php-error body class in admin after updated wordpress 5.6.2 to 5.7 via docker pull wordpress

A few rules when using wp_debug with the docker wordpress image.

  1. You cant use define('WP_DEBUG', true); in the wp-config.php/WORDPRESS_CONFIG_EXTRA and WORDPRESS_DEBUG: 1 in the docker-compose.yml at the same time. Using them simultaneously will always give you a blank php error in the admin dashboard.
  2. When deploying wordpress with docker using docker-compose.yml to configure your environment, you should never really have to manually change the permissions of your project using chmod 777 to make debug mode work.
  3. You should never edit your core wordpress docker container files either, all environment configs should be done through your docker-compose.yml file, so that you can spin up and update your docker wordpress container image safely.

So now we know these basic rules, this is how to enable WP_DEBUG in docker-compose.yml the right way. See the environment block.

Please note if you try to spin up my example docker-compose.yml you must also have uploads.ini and error-logging.ini files present in the same directory before running docker-compose up -d.

See public gist here which has the .ini files code which you need to implement before dockering up...

https://gist.github.com/joshmoto/ab7c09ca45291f7e6f9a14cf070fa70c

Your empty project directory should like this...

enter image description here

Here is the full docker-compose.yml config...

version: '3.7'

networks:
  wordpress:
    ipam:
      config:
        - subnet: 172.25.0.0/16

services:

  # here is our mysql database
  db:
    image: mysql:5.7
    volumes:
      - ./db:/var/lib/mysql:delegated
    ports:
      - "3306:3306"
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    networks:
      - wordpress

  # here is our wordpress server
  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    volumes:
      # our persistent local data re routing
      - ./themes/yourtheme:/var/www/html/wp-content/themes/yourtheme:delegated
      - ./plugins:/var/www/html/wp-content/plugins
      - ./mu-plugins:/var/www/html/wp-content/mu-plugins
      - ./uploads:/var/www/html/wp-content/uploads
      - ./logs:/var/www/html/wp-content/logs
      - ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
      - ./error-logging.ini:/usr/local/etc/php/conf.d/error-logging.ini
    ports:
      - "80:80"
    restart: always
    networks:
      - wordpress
    environment:

      # debug mode
      WORDPRESS_DEBUG: 1

      # docker wp config settings
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_TABLE_PREFIX: wp_
      WORDPRESS_AUTH_KEY: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb
      WORDPRESS_SECURE_KEY: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb
      WORDPRESS_LOGGED_IN_KEY: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb
      WORDPRESS_NONCE_KEY: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb
      WORDPRESS_SECURE_AUTH_SALT: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb
      WORDPRESS_LOGGED_IN_SALT: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb
      WORDPRESS_NONCE_SALT: 5f6ede1b94d25a2294e29eeba929a8c80a5ac0fb

      # our local dev environment
      WORDPRESS_CONFIG_EXTRA: |

        /* development parameters */
        define('WP_CACHE', false);
        define('ENVIRONMENT', 'local');

        /* do not re-define WP_DEBUG here or it will throw a blank error in the admin */
        /* you can remove this entirely, this is just to show you what not to do */
        // define('WP_DEBUG', true);

        if (!defined('WP_HOME')) {
          /* force our home url */
          define('WP_HOME', 'http://localhost');
          define('WP_SITEURL', WP_HOME);
        }

Now you have this docker-compose.yml set, along with the .ini files, you can now safely run this command in your project directory to build your local wordpress environment...

docker-compose up -d

This docker-compose.yml example also uses persistent volume mapping to your project directory, so once docker has finished building your environment, your local project directory will now look like this...

enter image description here

If you now visit http://localhost/ after initial dockering up has finished, you will now see the install page...

enter image description here

Error log files will be saved to your logs directory and all other necessary wordpress directories are now mapped to your local project.

So next time you run docker-compose down and then re-run docker-compose up -d, your local environment will be exactly the same as when you last dockered down.

If you are using git, then you will probably want to exclude heavy directories such as db, plugins, uploads, logs etc using a .gitignore file...

/db
/plugins
/uploads
/logs

I have also added .gitignore to the gist. 👍🏼

Good luck, hope this helps.

Upvotes: 17

Related Questions