Setup WordPress WP-CLI on Docker ( the simple way )

In this post I’ll run over how to setup WP-CLI on a Docker web server image ( so you can easily use it todo a Database find and replace , would is handy when you need to migrate a site from one place to another ).

This presumes you have PHP setup on the command line (which my image did have ). Anyway here goes:

  1. First in your Docker folder run ‘docker ps’ ( to get the name of the web server image CONTAINER ID )
  2. Pop that into this command e.g. ‘docker exec -it fdghye5742252 bash’
  3. You’ll be in a shell on the web server now
  4. navigate to your web root of the site you want ( my image puts me there already eg /var/www/html )
  5. download wp-cli and install see https://make.wordpress.org/cli/handbook/guides/installing/
    1. you can also do this on a volume if you have your webroot setup as a volume linked to your local hard drive
  6. I didn’t bother with moving it to the bin dir (which would put it on the global path)
  7. I just did ‘mv wp-cli.phar wp’
  8. you can then run eg
    1. ‘./wp –info’
    2.  do your find and replace ‘./wp search-replace ‘https://www.somesite.co.uk’ ‘http://localhost’ –allow-root’

update recently I had a problem with the allow root switch not working, I wrote an alias to allow it. Which is fine as I’m only running docker locally (dont do this on a live site).

alias wp='wp --allow-root'

update update, seems there’s a bug with wp search replace, so went old skool and used https://interconnectit.com/search-and-replace-for-wordpress-databases/ locally ( best not to use this on a live server unless you know the security risks and how to mitigate)

update update update: seems theres an issue with interconnect web script running on docker, so next up is try and use the interconnect command line script.

Disclaimer – Obviously with anything like this be careful and make sure you have a backup, use at your own risk.

 

 

Leave a Comment