Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli doesn't get env vars from wordpress image #594

Closed
jmmendivil opened this issue Mar 24, 2021 · 7 comments · Fixed by docker-library/docs#1911
Closed

cli doesn't get env vars from wordpress image #594

jmmendivil opened this issue Mar 24, 2021 · 7 comments · Fixed by docker-library/docs#1911

Comments

@jmmendivil
Copy link

Hey guys, great work here...

I wonder if we do need to update the CLI docs to include environment variables if it's used with docker-compose?

I had a composer file with wordpress and wordpress:cli that worked fine until the recent update (with getenv_docker). Thanks to #578 and #574 I was able to reconfigure it and make it work but maybe we should specify it somewhere in the docs for the others with the same problem (or maybe it is already written and I didn't find it).

I can do a PR if necessary.

@tianon
Copy link
Member

tianon commented Apr 5, 2021

I think it's probably worth adding a note somewhere, sure -- maybe something like "If you want to use the WP-CLI variants with an existing wp-config-docker.php installation, you'll need to also include the configuration environment variables." ?

Or perhaps we should instead make it more clear that we do not modify wp-config.php once installed, and that the wp-config.php we provide for a clean install references the runtime variables directly?

@tmuka
Copy link

tmuka commented Apr 7, 2021

I found this issue after finding my wp-cli based docker workflow is broken now. Thanks for linking to issues that helped you figure out how to fix it @jmmendivil I agree it would be nice if the documentation had an example of how to set up the environment to work with wp-cli. thanks for all the hard work!

tmuka added a commit to tmuka/docker-cuaws-wpcua that referenced this issue Apr 8, 2021
# temporarily pinned to the 5.5.3 release to work around docker-library/wordpress#594
FROM  wordpress:5.6.2-php7.4-apache
@tmuka
Copy link

tmuka commented Apr 8, 2021

I don't think it's the "right" solution, but wanted to note that as a temporary workaround to get my team workflow going for today I reverted to using image: wordpress:5.6.2-php7.4-apache that handles the wp-config.php the old way.

My use case is basically to spin up a temporary docker container for local dev work using wp-cli/ssh to import the remote db and search/replace prod/dev URLs. The container is then destroyed after.

My inclination is to use sed to replace the DB_NAME, DB_USER, DB_PASSWORD wp-config.php lines with the values from docker-compose.yml as part of my init script process. I'd welcome feedback on a better way to approach getting wp-cli working with the new getenv_docker() config. Thanks!

@yosifkit
Copy link
Member

yosifkit commented Apr 8, 2021

Does docker-library/docs#1911 help? Let me know how I can further improve the documentation.

@tmuka
Copy link

tmuka commented Apr 9, 2021

Does docker-library/docs#1911 help? Let me know how I can further improve the documentation.

Thanks. After reading back the new doc and a handful of related linked issues it's still unclear to me if or how wp-cli is intended to work inside the wordpress container. I do have the envars configured in my docker-compose.yml, and they do correctly get passed to the container and do show when i run env inside the container. I was confused by mentions of wp-config-docker.php, but it seems that was a dev version that is now merged into wp-config.php.

should wp-cli be picking up the defined envar values through wp-config.php when wp-cli is used?

If it helps, i'll include a bit of output to show what's happening (with redacted password).

root@203e8f7359a0:/var/www/html# env | egrep DB
WORDPRESS_DB_HOST=mysql
WORDPRESS_DB_PASSWORD=db-pass
WORDPRESS_DB_USER=wordpress_user
WORDPRESS_DB_NAME=wordpress_db

root@203e8f7359a0:/var/www/html# wp user list
PHP Warning:  mysqli_real_connect(): (HY000/1045): Access denied for user 'example username'@'192.168.144.3' (using password: YES) in /var/www/html/wp-includes/wp-db.php on line 1653
Warning: mysqli_real_connect(): (HY000/1045): Access denied for user 'example username'@'192.168.144.3' (using password: YES) in /var/www/html/wp-includes/wp-db.php on line 1653
Error: Error establishing a database connection.

root@203e8f7359a0:/var/www/html# egrep DB_ wp-config.php
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') );
❯ cat docker-compose.yml
version: '3.1'
services:
  wordpress:
    #image: wordpress:php7.4-apache
    image: cuaws/wpcua:latest
    ports:
      - 80:80
      - 8080:80
      - 443:443
    volumes:
      - "./:/var/www/html/wp-content"
    environment:
      WORDPRESS_DB_HOST: mysql
      WORDPRESS_DB_USER: wordpress_user
      WORDPRESS_DB_NAME: wordpress_db
      WORDPRESS_DB_PASSWORD: db-pass

currently my workaround to fix wp-cli is running this via script inside the container.

sed -i "s;getenv_docker('WORDPRESS_DB_NAME', 'wordpress');'wordpress_db';g" wp-config.php
sed -i "s;getenv_docker('WORDPRESS_DB_USER', 'example username');'wordpress_user';g" wp-config.php
sed -i "s;getenv_docker('WORDPRESS_DB_PASSWORD', 'example password');'db-pass';g" wp-config.php

I'm happy to learn, thanks for your patience and expertise!

@tianon
Copy link
Member

tianon commented Apr 9, 2021

Perhaps messing around with wp config list could help figure out why your setup isn't working?

I tried the following, and was successful (so I'm certain WP-CLI isn't stripping the variables before parsing the config file):

bash-5.1$ env | grep -i wordpress_db
bash-5.1$ wp config list | grep DB_
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
bash-5.1$ export WORDPRESS_DB_HOST='foo' WORDPRESS_DB_NAME='bar' WORDPRESS_DB_USER='baz' WORDPRESS_DB_PASSWORD='buzz'
bash-5.1$ wp config list | grep DB_
DB_NAME	bar	constant
DB_USER	baz	constant
DB_PASSWORD	buzz	constant
DB_HOST	foo	constant
DB_CHARSET	utf8	constant
DB_COLLATE		constant

@tmuka
Copy link

tmuka commented Apr 9, 2021

Perhaps messing around with wp config list could help figure out why your setup isn't working?

I tried the following, and was successful (so I'm certain WP-CLI isn't stripping the variables before parsing the config file):

Thank you!!! you've helped me figure out my problem... I had previously wrapped wp-cli.phar in a script named wp so i could run as www-data user. Now I had to add --preserve-env for it to work correctly.

#!/bin/sh
# This is a wrapper so that wp-cli can run as the www-data user so that permissions remain correct
sudo --preserve-env -u www-data /bin/wp-cli.phar $*

thanks so much for your patience in helping me sort out my mistake!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants