0

I'm trying to use the WordPress local development environment, which sets up a bunch of docker containers (and I'm new to Docker). One of them is a WordPress CLI, which I presume is running some scripts to do some configuration, but that particular container doesn't stay running (I believe this is intentional). I'm guessing that a script it's running is failing, and that's happening because of some configuration error, but I can't figure out how to tell what it's doing when it executes (what scripts it's running, what environment variables are set, etc...).

Is there any way to somehow "trace" what the container is trying to do?

$docker image ls
REPOSITORY                 TAG       IMAGE ID       CREATED       SIZE
wordpressdevelop/phpunit   latest    8ebb6f73d762   2 days ago    732MB
wordpress                  latest    6c2c086d9173   2 days ago    554MB
wordpress                  <none>    408627ce79b1   2 days ago    551MB
composer                   latest    ff854871a595   9 days ago    173MB
wordpress                  cli       ee6de7f71aa0   9 days ago    137MB // I want to see what this does
mariadb                    latest    e27cf5bc24fe   12 days ago   401MB

$docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED          STATUS          PORTS                     NAMES
19007b991e08   wordpress   "docker-entrypoint.s…"   48 minutes ago   Up 48 minutes   0.0.0.0:8888->80/tcp      e6bc9159b910bda3d9b0dae2e230eabd_wordpress_1
26ac5c7ec782   wordpress   "docker-entrypoint.s…"   48 minutes ago   Up 48 minutes   0.0.0.0:8889->80/tcp      e6bc9159b910bda3d9b0dae2e230eabd_tests-wordpress_1
8ae0a4dc4f77   mariadb     "docker-entrypoint.s…"   48 minutes ago   Up 48 minutes   0.0.0.0:54989->3306/tcp   e6bc9159b910bda3d9b0dae2e230eabd_mysql_1

This is on MacOS 11.2.2 running Docker Desktop, Docker version 20.10.5.

I'm not sure if it's relevant, but for completeness' sake, I've included the docker-compose.yml which wp-env generates, below.

Thanks!

Background

This used to Just Work, but I think I broke something in my environment, and am trying to diagnose that. I initially asked about that on WordPress StackExchange, but have since dug deeper. The Docker compose step fails thusly:

... ⠏ Configuring WordPress.Creating e6bc9159b910bda3d9b0dae2e230eabd_cli_run ... done ⠹ Configuring WordPress.mysqlcheck: Got error: 1045: Access denied for user 'username_here'@'172.19.0.5' (using password: YES) when trying to connect

The database container is left up and running, and if I go look in the database, I see it's configured to have root connect with no password:

MariaDB [(none)]> select user, host, password from mysql.user;
+-------------+-----------+----------+
| User        | Host      | Password |
+-------------+-----------+----------+
| mariadb.sys | localhost |          |
| root        | localhost |          |
| root        | %         |          |
+-------------+-----------+----------+
3 rows in set (0.003 sec)

In the main WordPress container, the wp-config.php file contains this little snippet:

...
// a helper function to lookup "env_FILE", "env", then fallback
function getenv_docker($env, $default) {
        if ($fileEnv = getenv($env . '_FILE')) {
                return file_get_contents($fileEnv);
        }
        else if ($val = getenv($env)) {
                return $val;
        }
        else {
                return $default;
        }
}
...

/** MySQL database username */
define( 'DB_USER', getenv_docker('WORDPRESS_DB_USER', 'username_here') );

/** MySQL database password */
define( 'DB_PASSWORD', getenv_docker('WORDPRESS_DB_PASSWORD', 'password_here') )

Given the error message, I assume the CLI container is trying to do something similar, but the environment variables WORDPRESS_* aren't set, and so it's using the defaults, which ... aren't working. What I think I need to do is track down something that's failing to set those variables earlier in the run process.


docker-compose.yml

version: '3.7'
services:
  mysql:
    image: mariadb
    ports:
      - '3306'
    environment:
      MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
    volumes:
      - 'mysql:/var/lib/mysql'
  wordpress:
    build: .
    depends_on:
      - mysql
    image: wordpress
    ports:
      - '${WP_ENV_PORT:-8888}:80'
    environment:
      WORDPRESS_DB_NAME: wordpress
    volumes: &ref_0
      - 'wordpress:/var/www/html'
      - '/Users/cwr/src/cwra/foo:/var/www/html/wp-content/plugins/foo'
  tests-wordpress:
    depends_on:
      - mysql
    image: wordpress
    ports:
      - '${WP_ENV_TESTS_PORT:-8889}:80'
    environment:
      WORDPRESS_DB_NAME: tests-wordpress
    volumes: &ref_1
      - 'tests-wordpress:/var/www/html'
      - '/Users/cwr/src/cwra/foo:/var/www/html/wp-content/plugins/foo'
  cli:
    depends_on:
      - wordpress
    image: 'wordpress:cli'
    volumes: *ref_0
    user: '33:33'
  tests-cli:
    depends_on:
      - tests-wordpress
    image: 'wordpress:cli'
    volumes: *ref_1
    user: '33:33'
  composer:
    image: composer
    volumes:
      - '/Users/cwr/src/cwra/foo:/app'
  phpunit:
    image: 'wordpressdevelop/phpunit:latest'
    depends_on:
      - tests-wordpress
    volumes:
      - 'tests-wordpress:/var/www/html'
      - '/Users/cwr/src/cwra/foo:/var/www/html/wp-content/plugins/foo'
      - 'phpunit-uploads:/var/www/html/wp-content/uploads'
    environment:
      LOCAL_DIR: html
      WP_PHPUNIT__TESTS_CONFIG: /var/www/html/phpunit-wp-config.php
volumes:
  wordpress: {}
  tests-wordpress: {}
  mysql: {}
  phpunit-uploads: {}
philolegein
  • 1,099
  • 10
  • 28
  • That article seems a little over complicated to me, are you just looking for a fast way to spin up wordpress projects using docker on mac? Using docker-compose commands and configuring your own `docker-compose.yml` is super easy. See my answer her which might help if your looking to build themes locally with persistent db, plugins and uploads. Also including simple environment helper functions... https://stackoverflow.com/questions/66605148/local-development-guide-from-github-repo-messed-up/66619264#66619264 – joshmoto Mar 16 '21 at 21:24
  • I find using [homebrew](https://brew.sh/) to install all hidden packages globally on your mac through terminal easier for running commands like composer in your project. Once you've installed brew on your mac you run composer commands such as `composer require nesbot/carbon` in your theme dir to install the relative composer packages in a `vendors` folder and then include `require 'vendor/autoload.php';` in your theme `functions.php`. I find the less you run in your `.yml`, your local wp environment performance is better. – joshmoto Mar 16 '21 at 21:38
  • Looks like brew also includes [`wp-cli`](https://formulae.brew.sh/formula/wp-cli#default)... https://imgur.com/a/VB3o9ta ...brew is super handy, like you can run `brew upgrade` in terminal to update all your packages for example. See [brew docs](https://docs.brew.sh/FAQ#how-do-i-update-my-local-packages). – joshmoto Mar 16 '21 at 21:51

1 Answers1

0

docker logs could help you here as it is also showing logs of exited containers (Source).

UPDATE: Acc. to the OP, the actual issue is still unclear but starting with a fresh docker & node installation did the trick & got wp-env running.

von_court
  • 152
  • 2
  • 10
  • `docker ps -a` doesn't list the exited container, and if I try and use the image id, it says "No such container". – philolegein Mar 16 '21 at 08:03
  • @philolegein, do you `docker run ` with the `--rm` option? If yes, pls omit that. `--rm` deletes the container after exit ([src](https://docs.docker.com/engine/reference/run/#clean-up---rm)) – von_court Mar 16 '21 at 15:12
  • OK. The docker run is automatically done by wp-env; however, I re-ran the container from Docker desktop. The logs are ... not exactly verbose :) `% docker logs bb73f6ba8045 Error: This does not seem to be a WordPress installation. Pass --path='path/to/wordpress' or run 'wp core download'.` Which still doesn't tell me which scripts are being run on startup. Can I maybe "mount" the image without running it? – philolegein Mar 16 '21 at 15:32
  • 1
    @philolegein, when using wp-env, it seems like [db-env.js](https://github.com/WordPress/gutenberg/blob/f8052e179a740642b56653968135990bb298d5b3/packages/env/lib/config/db-env.js) is the file which is ought to be used/applied - and in your case is not. Have you tried [resetting the database](https://github.com/WordPress/gutenberg/tree/f8052e179a740642b56653968135990bb298d5b3/packages/env#5-reset-the-database) or doing the other subsequent steps mentioned there? – von_court Mar 17 '21 at 08:31
  • 1
    @philolegein, PS, in case you want to dig deeper: You could check the [image layers](https://hub.docker.com/layers/wordpress/library/wordpress/cli/images/sha256-c442c49e6534cd9be22569f4174094b77d883f5fef3a6659d902c2680e2324f3?context=explore) of the cli image and then check the referenced files in wordpress' GitHub repo – von_court Mar 17 '21 at 09:17
  • I did the database reset, and everything else on that page. I think you're onto something with the image layers. I suspect there might be a synchronization issue between some cached images. I have an [open question](https://stackoverflow.com/questions/66669224/how-do-i-access-the-docker-ce-virtual-machine-on-macos-bigsur) on stack overflow to try and figure out how to get in there and see. – philolegein Mar 17 '21 at 09:51
  • 1
    This seems to have been it; or rather, there was some cacheing going on between wp-env and docker. I had previously nuked my entire node environment, and then nuked docker. This time I nuked both at the same time, and things just started working again. :( – philolegein Mar 18 '21 at 07:45
  • 1
    Nice that you have got it running in the end! In case that specific issue comes up again or you figure out the cause someday (also interested in that a bit :P), pls let me know. – von_court Mar 18 '21 at 08:10