Categories: LinuxWordpress

Using WP-CLI to Access MySQL Running in A LAMP Docker Container

WP-CLI is a great command line tool to help you manage WordPress.

I had trouble figuring out how to use WP-CLI to access a MySQL database that was running in a Docker container. I’m running a Docker LAMP instance locally using this set of Docker and Docker-Compose files from sprintcube.

The MySQL instance holding my local WordPress database is running in a Docker container. However, my WordPress core files are located located in a directory on the host’s physical hard drive defined as a Docker volume.

The trick is defining your DB_HOST environment value to be the IP address of your MySQL Docker container. You can do this either explicitly by the command line or in your WP configuration files (for a typical install in your wp-config.php file, in a bedrock install in your .env files).

Getting the IP Address of the MySQL Docker Container

You can find the IP address of your docker container by executing the following command:

docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' CONTAINER-NAME 

Get your container name:

docker container ls

From this, I see that the Mysql container name is “lamp-database”.

d

Define DB_HOST Environmental Variable

Execute the following command substituting CONTAINER-NAME with your database container:


export DB_HOST=$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' CONTAINER-NAME 

Define the Path to Your WordPress Install in Your WP-CLI Configuration Files

Create wp-cli.yml file in the directory you will be using WP or, alternatively create ~/.wp-cli/config.yml

The local alias should point to the WordPress file on your Docker volume as shown in this example:



@prod:
  ssh: username@myproductionserver.com:22/home/username/bedrock/www

@local:
  path: /home/local-username/website/bedrock/web/wp  [<-- note this is path to WordPress core files ]


 

To backup your WordPress database just type:

wp @local db export  

Note that this method is intended to be used for a LAMP Docker install with the WordPress files being simply installed in the document root kept in a defined volume on the host's physical drive and WP-CLI installed on the physical docker host.

If you are using a WordPress container to run WordPress and/or WP-CLI, then you most likely want to use the ssh tag in wp-cli.yml. See the WP-CLI documentation regarding the ssh tag and brief discussion of the Docker scheme.

Charlie

Recent Posts

KeePassXC Password Manager – Use Auto-Type Instead of the KeePassXC Plugin

Avoiding Browser Plugins with KeePassXC KeePassXC is a popular free open source password manager. As…

3 years ago

A Simple Step to Delete Thousands of Useless Gmail Messages and Free Up Space

If you've had a Gmail account for years, like I have, at some point you…

3 years ago

Back Up Your Raspberry Pi Virtually

Here's a little trick I use to back up my Raspberry Pi without having to…

4 years ago

Unclutter Your GMail Without Missing Out On Your Newsletters

I try to keep my email inbox fairly clean but I do subscribe to some…

4 years ago

Windows Reinstall: Use Secret Migration Tool to Restore All Settings Even If Option Disabled; Plus Tips to Restore Programs After a Clean Install

I was recently faced with the dreaded prospect of re-installing Windows 10. While the standard…

4 years ago

Pi-hole & Unlimited Whitelist for OpenDNS, Cleanbrowsing, Etc.

Pi-Hole and Upstream DNS Providers Pi-hole is an excellent whole-home, self-hosted ad blocker and DNS…

4 years ago