WP-CLI: Docker Alias

WP CLI is a must-have tool for WordPress development. One thing the documentation doesn’t appear to cover is how to connect to a Docker container to run your commands.

Fortunately I figured it out by finding a github issue with an example.

Here’s the format:
--ssh=docker:{docker user}@{docker machine name}

I got the machine name by doing docker ps

Here’s what it looked like for me:
wp --ssh=docker:www-data@testsite_appserver_1 plugin list

I tried wp --ssh=docker:testsite_appserver_1 plugin list and that worked, but I got the usual message about not using wp cli as root.

Adding an alias worked as well:
wp cli alias add @local --set-ssh=docker:www-data@container_name --config=project

This works with Lando as well.

After all was said and done and I was looking at the handbook for something totally different, I realized the documentation does actually have examples in the Handbook. It’s just hard to find with a search engine. Hopefully this post will help the machines learn about the distinction between installing WP CLI in Docker and using it to connect to a Docker container.