Hi,
I am starting for the first time to try to adopt docker and all the goodness of containerization for local development with WordPress and WP-CLI and I am clearly missing something or not fully understanding how I need to configure this.
Here is my current docker-compose.yml file
version: "3.8"
# Services
services:
# Website Database
db:
container_name: database
image: mysql:5.7
restart: always
ports:
- 3306:3306
volumes:
- db_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress
MYSQL_USER: phil
MYSQL_PASSWORD: password
networks:
- wordpress
# WordPress
wordpress:
container_name: wordpress
working_dir: /var/www/html
image: wordpress:latest
depends_on:
- db
ports:
- 8000:80
restart: always
environment:
WORDPRESS_DB_NAME: wordpress
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_PASSWORD: password
WORDPRESS_TABLE_PREFIX: wp_
WORDPRESS_DB_USER: user
WORDPRESS_DEBUG: 1
volumes:
- ./public:/var/www/html # WordPress root
- ./log/:/var/log # Server logs
- ./custom.ini:/usr/local/etc/php/conf.d/custom.ini # Custom php.ini config
networks:
- wordpress
# WP CLI
wp:
container_name: wpcli
working_dir: /var/www/html
restart: always
image: wordpress:cli
stdin_open: true
tty: true
environment:
WORDPRESS_DB_NAME: wordpress
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_PASSWORD: password
WORDPRESS_TABLE_PREFIX: wp_
WORDPRESS_DB_USER: user
depends_on:
- db
- wordpress
user: xfs
entrypoint: ["wp", "--allow-root"]
command: >
/bin/sh -c '
sleep 10;
wp core install --path="/var/www/html" --url="http://localhost:8000" --title="Testing" --admin_user=admin --admin_password=password --admin_email=something@email.co.uk
'
volumes:
- wp_data:/var/www/html
- db_data:/var/lib/mysql
networks:
- wordpress
# Volumes
volumes:
db_data:
wp_data:
# Networks
networks:
wordpress:
I am using the official latest WordPress docker image, along with the official WordPress CLI image.
When I run docker-compose up -d from the root directory with my yaml file it runs fine and the containers start up and If I go to localhost:8000 I see the WordPress installation page as expected so that seems to be running and working fine.
The issue I seem to be having is around WP CLI an the wp-config.php file.
Once the initial set up is run I cd into my local âpublicâ directory as listed above for the WordPress installation and if I run something like âwp config listâ using WP CLI, it is seeing the fallback values from the WordPress images wp-config.php file:
+------------------+------------------------------------------+----------+
| name | value | type |
+------------------+------------------------------------------+----------+
| table_prefix | wp_ | variable |
| configExtra | | variable |
| DB_NAME | wordpress | constant |
| DB_USER | example username | constant |
| DB_PASSWORD | example password | constant |
| DB_HOST | mysql | constant |
| DB_CHARSET | utf8 | constant |
| DB_COLLATE | | constant |
| AUTH_KEY | | constant |
| SECURE_AUTH_KEY | | constant |
| LOGGED_IN_KEY | | constant |
| NONCE_KEY | | constant |
| AUTH_SALT | | constant |
| SECURE_AUTH_SALT | | constant |
| LOGGED_IN_SALT | | constant |
| NONCE_SALT | | constant |
| WP_DEBUG | | constant |
+------------------+------------------------------------------+----------+
As you can see none of the environment variables from my docker-compose.yml file are showing here as I would expect.
This is preventing WP CLI to be able to run commands on the database and wp core install etc which is a huge pain.
If I run âWP config pathâ to see where it is reading these values from it is the correct path in my mind:
C:\docker-sites\wp-test\public\wp-config.php
In the public directory mapped to the Docker container that has just run and put the files there.
If I run âwp core installâ to see the response back I get the following:
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.
On other forums and sites I have heard something about having to map the same WordPress DB Environment variables from the WordPress service in my docker-compose file which you can see above I have done but this doesnât seem to help.
I have tried adding other settings which are supposed to help as well such as TTY: true and user: xfs but nothing seems to help.
Looking inside the wp-config.php file generated by the WordPress image it seems to me that the getenv_docker() functions are not finding the environment variables for some reason from my docker-compose file:
e.g.
define( 'DB_HOST', getenv_docker('WORDPRESS_DB_HOST', 'mysql') );
The WordPress image uses this function for all wp-config.php CONSTS and to the right has a fallback value which is what âwp config listâ seems to be picking up.
If I change these fallback values manually then of course it works as well as WP CLI then sees the correct values from these fallbacks.
I am started to lose hope of getting this set up and would really appreciate any help and advice on where I am going wrong.
Any help would be greatly appreciated.
Thanks
Phil