Hi and thanks for the great work.
I'm trying to create a docker-compose file for phpunit, which currently has two services: wordpress_phpunit and db:
version: "3.1"
services:
wordpress_phpunit:
build:
context: .
dockerfile: Dockerfile-phpunit
volumes:
- "./my-plugin:/app"
- "testsuite:/tmp"
depends_on:
- mysql_phpunit
environment:
WORDPRESS_DB_HOST: mysql_phpunit:3306
WORDPRESS_DB_NAME: wordpress
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WP_PLUGIN_FOLDER: app
PHPUNIT_DB_HOST: "mysql_phpunit"
restart: always
mysql_phpunit:
image: mariadb:10.2
restart: always
environment:
MYSQL_ROOT_PASSWORD: rootpass
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
volumes:
testsuite: {}
This is the Dockerfile, referenced above:
#1. Docker base image
FROM wordpress:php7.4
#2. Install WP-cli and dependencies to run it
RUN apt-get update \
&& apt-get install -y \
less \
vim \
subversion \
sudo \
default-mysql-client-core \
&& curl https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar -o /usr/local/bin/wp \
&& chmod +x /usr/local/bin/wp
#3. Create the files for the testing environment
RUN \
#3.1 Install phpunit
curl -L https://phar.phpunit.de/phpunit-7.phar -o /tmp/phpunit \
&& chmod a+x /tmp/phpunit \
#3.2 Install wordpress
&& cp -r /usr/src/wordpress /tmp/wordpress \
&& curl https://raw.github.com/markoheijnen/wp-mysqli/master/db.php -o /tmp/wordpress/wp-content/db.php \
#3.3 Install the testing libraries
&& svn co --quiet https://develop.svn.wordpress.org/tags/5.3.2/tests/phpunit/includes/ /tmp/wordpress-tests-lib/includes \
&& svn co --quiet https://develop.svn.wordpress.org/tags/5.3.2/tests/phpunit/data/ /tmp/wordpress-tests-lib/data \
#3.4 set owner and permissions
&& chown -R www-data:www-data /tmp/wordpress \
&& chown -R www-data:www-data /tmp/wordpress-tests-lib
#4. Copy the script to create the testing environment when the container is started
COPY init-testing-environment.sh /usr/local/bin/
#5. Run the script and send as an argument the command to run the apache service
ENTRYPOINT ["init-testing-environment.sh"]
CMD ["apache2-foreground"]
The tests init file is this:
#!/bin/bash
cd /var/www/html/
#1. check if wordpress is already installed/configured
if (sudo -u www-data -- wp core is-installed)
then
#2. check if the database is ready
if ! (sudo -u www-data -- wp db check)
then
# wait a moment for the database container
sleep 1
exit 1;
fi
#3. init the testing instance
sudo -u www-data -- wp scaffold plugin-tests $WP_PLUGIN_FOLDER --force
cd wp-content/plugins/$WP_PLUGIN_FOLDER && sudo -u www-data -- bash -c "./bin/install-wp-tests.sh $WORDPRESS_DB_NAME $WORDPRESS_DB_USER $WORDPRESS_DB_PASSWORD $WORDPRESS_DB_HOST latest true"
fi
#4. back to the root WP folder
cd /var/www/html/
#5. execute the entrypoint of the parent image
bash docker-entrypoint.sh "$@"
Am able to run docker-compose up -d without error, however, within the container,
docker-compose -f docker-compose.phpunit.yml exec wordpress_phpunit bash
Trying to manually run the command:
sudo -u www-data wp core is-installed
It returns the db connection error.
Contents of wp-config.php include:
define( 'DB_NAME', getenv_docker('WORDPRESS_DB_NAME', 'wordpress') );
define( 'DB_USER', getenv_docker('WORDPRESS_DB_USER', 'example username') );
define( 'DB_PASSWORD', getenv_docker('WORDPRESS_DB_PASSWORD', 'example password') );
define( 'DB_HOST', getenv_docker('WORDPRESS_DB_HOST', 'mysql') )
And my understanding is that the getenv_docker() calls will return my env variables from the compose file:
WORDPRESS_DB_HOST: mysql_phpunit:3306
WORDPRESS_DB_NAME: wordpress
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
I had seen that there might be some issues with getenv_docker() and hoping for some insights on what I'm missing here.
Hi and thanks for the great work.
I'm trying to create a docker-compose file for phpunit, which currently has two services:
wordpress_phpunitanddb:This is the Dockerfile, referenced above:
The tests init file is this:
Am able to run
docker-compose up -dwithout error, however, within the container,Trying to manually run the command:
It returns the db connection error.
Contents of
wp-config.phpinclude:And my understanding is that the
getenv_docker()calls will return myenvvariables from the compose file:I had seen that there might be some issues with
getenv_docker()and hoping for some insights on what I'm missing here.