Docker Newbie: Need help getting up and running with WordPress + WP CLI using Docker-compose

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

It doesn’t seem to be correct at all. You forgot to mention the operating system you are using, but you shared a Windows path which indicates you are trying to use the wp cli command from your windows host where you installed Docker Desktop for Windows. That won’t work. The wpcli container is for running commands in that container, not on your host. The host will not know about any variables or config files that are inside the container.

1 Like

Thanks for the feedback and time to reply, that’s great and you were absolutely right. It turns out I was inadvertently using another global instance of WP CLI without even realising it on my machine. At some point in the frustration trying to get everything set up and working I started simply using the wp… commands instead of docker-compose run -rm wp…

Although another issue I have now run inError: Composer directory '/etc/X11/fs/.wp-cli/packages' for packages couldn't be created: mkdir(): Permission denied

I have tried researching how to get around this and to be honest I haven’t found anything of any help.

If you have any guidance on resolving permissions based errors in Docker like this it would be much appreciated.

Thanks again for your help so far.

Dockr is not magick so permission issues can be solved almost the same way as anywhere else. I don’t know why it wants to write that specific folder since /etc/X11 is not even expected to exist in a usual container and even if it exists it would be writable by the root user. If the cli container is not using as root it won’t be able to write it. Please, share how you ran the wp-cli command this time exactly.