Easy Docker WordPress Setup with WP CLI

2021-08-15

The best WordPress docker setup with WP CLI

I have spent way too long trying to get a good docker setup for WordPress. Tried different images, setting up my own and just had so many issues.

I'm documenting this all to help others out and remind myself in the future.

One command for install and setup, ok two. Fine.

One of the things I really wanted to do and have had to let go of, is having my WordPress auto install with my theme and plugin setup with one command. Eg Run 'docker-compose up' and be ready to go.

But I have had to let go of that and settle with two commands. I know, two commands is fine.

Problems

One of the main issues I have been having is changes. You get a good setup and then you come back to it and it no longer works. When you have projects that you maintain occationally, the last thing you need is to be faffing with your docker setup everytime. Because something is broken, again, because something has changed, again. Then you have to hunt down what and why what was working no longer does.

One way is to just abandon using WordPress docker images and roll your own. But it's a lot of work and the folks running the WordPress docker image have put a lot of work into and they are going to shield you from some issues.

The second thing I could do was to try and make the least amount of modifications to the image and drop using my own Dockerfile and entry file.

The problem with using a entry point with the WordPress docker image

You can't run your entry point before the offical WordPress image because that's the script which installs WordPress. You can't run your entry point after because ( as far as I can tell ) there just isn't any way to do that.

You could replace it with your own but I really just want to use the offical images and not mess around. All I want to do is run some post install commands, the best way really is to let it install then run a command with WP CLI afterwards which runs your script.

The best repo I have found

The marjority of examples out there that I found do heavily modify the WordPress image to get things like WP CLI working. But then I found this really good example that gets things working without adding a new entry point or a Dockerfile.

The various repos I found were often:

  • not maintained
  • too complex
  • no longer working
  • don't have WP CLI

Then finally I came across this one: [https://github.com/chriszarate/docker-compose-wordpress]

Which is perfect as it's just mainly a docker-compose file pulling in offical images for WordPress, wp-cli and mariadb.

Making it

  • simple
  • low maintaince
  • no hacking

Really good work Chris Zarate - you've saved me quite a headache.

Setting up for your project

The README for setup is pretty good, the only thing I'll add is to set your WordPress username, pass etch in /bin/install-wp.sh. You can also add there any plugins for themes that you want to auto-install. You need to do this before running the install wp command docker-compose run --rm wp-cli install-wp

wp plugin install query-monitor --activate
wp theme activate mytheme

Latest WordPress and PHP version

Under services > wordpress you can change to

image: "wordpress:latest"

To just get the latest version

WP Volume

To make it work I changed the wp volume line to

- "./wp:/var/www/html:rw"

Without this change, I couldn't add new plugins via the admin.

Adding your theme

In docker-compose.yml, assuming your theme is in the root folder, add in the section wordpress under volumes:

volumes:
- "./wp:/var/www/html:rw"
- "./mytheme:/var/www/html/wp-content/themes/mytheme:ro"

Needs the ./ at the beginning to work

This adds the local folder mytheme to the wordpress themes directory that docker creates making it available to your WordPress install. It works like a link so any changes you make to the theme locally will be seen in the running WordPress instance.

All the volumes you add to the WordPres image also need to be added to the WP CLI image in the exact same way as above.

Any volume you add must allso be added under volumes near the bottom of the file:

volumes:
wp: {}
mytheme: {}

Then in bin/install-wp.sh before Activate plugins add these lines:

# Activate themes
wp theme activate mytheme

When you run this install script inside docker it will activate your theme.

Ports

If you get an error that port 80 is in use then you either need to find out what is using it and kill it

sudo lsof -i tcp:80

or change the port under ports to something else. If you do this you need to add the port to the address and change it in all locations.

XDEBUG

Next I want to try and get XDEBUG running. Wish me luck!