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:

Get your container name:

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


Define DB_HOST Environmental Variable

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

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:

To backup your WordPress database just type:

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.

Dunzip – Download and Unzip in Linux With One Command

Tired of downloading zip or tar files, decompressing, and then having to delete the compressed file ?

Do it all in one step with this Tip

  1. Copy and paste this function into your .bashrc (or in my case as I used .zsh, .zshrc):

  2. Then type the following in your terminal:

    If you are using zsh, substitute “.zshrc” for .bashrc.

Works for zip and gzipped tar files.


For details on this command see this stack exchange answer to How to redirect output of wget as input to unzip? by ruario.

Regarding functions and how to use them see with arguments see Make a Bash alias that takes a parameter?.

Backing Up an SD Card or Hard Drive to A VHD File (Virtual Hard Disk)


The Microsoft Virtual Hard Disk – An Ideal Backup Image Format

Microsoft’s Virtual Hard Disks (VHD) are files that look like hard drives to a computer’s operating system. They are typically used for installing file systems on when working with virtual machines (including Microsoft’s own Hyper-V or Oracle’s Virtualbox) but can also be attached and used on a physical Windows 10 box so the Windows machine treats the file as a hard drive. As such, they are ideal to clone physical hard drives to as a method of backup. They work particularly well with multi-partitioned drives or SD cards, such as those used with Raspberry Pis. A full clone image works well over just copying files as it preserves the partitions and bootable formatting.

Backing Up A Raspberry Pi Drive to A VHD Image – Step By Step

If you have a Windows 10 computer, here’s a relatively simple way you can backup SD cards to a browserable VHD disk image that is not proprietary to your backup software.

  1. Attach the hard drive you are duplicating to your Windows computer. In my case I have a 150 GB external USB hard drive which serves as my Pi’s bootable drive. I detach that from the PI and insert the USB connection to my Windows computer.

    If you have an SD card you would need to attach the SD Card to you Windows computer using an SD Card reader.

  2. Open up Disk Management (right click on the Windows Icon in lower left corner->Disk Management). Alternatively press Windows Key-R, then type “diskmgmt.msc” and press enter.

  4. Select “Action” in the menu, then “Create VHD”. If the Action menu items are grey, click first on something in the Disk Management window and then try again.

  5. When the “Create and Attach Virtual Hard Disk” window opens, click “Browse” to name your Virtual Hard Drive file and select the folder where it will be located. In addition, select the options shown below (VHD hard disk format, Dynamic/Expanding). The Dynamic disk option will save space on your physical hard drive . Fill in the “Virtual Hard Disk Size” with the size of the drive you are duplicating. Below I am duplicating my Raspberry PI’s 149.05 GB external USB drive that has a 256 MB bootable Fat 32 partition along with a 148.80 GB linux partition. If you are duplicating your PI’s SD card it will look similar but will be sized differently – it will have a FAT32 partition, as well as “Healthy (Primary Partition”) that is your Linux partition. Below is my example – the drive on the left is the source drive. The new Virtual Hard Disk we are creating will be the target cloned drive.

  6. When done select “OK”. You will see new drive in your Disk Management showing an “Unallocated” partition. On my system it shows up as Disk 8:

  7. Now open up your drive cloning software. I use the free MiniTool ShadowMaker Free 3.5. It is an excellent free backup tool for windows and includes a cloning tool.
  8. In Shadowmaker, I open the program and select Tools->Clone.

  9. I then select the Source (USB drive) and the target Destination (the unallocated Virtual Disk):

  10. Then Press “Ok” and confirm that the target Disk will be destroyed:

  11. ShadowMaker will then start cloning the disk (my clone took about an hour and a half):

  12. When done, ShadowMaker will ask if you want to shutdown, say “NO”:

  13. Now open up Disk Management again and confirm that the Virtual Drive is now correctly formatted and shows the partitions:

  14. Now, right click on the disk number (i.e., in the example, “Disk 7”) and select “Detach Virtual Drive”.

  15. You are done. The virtual drive file is now on your backup media having the extension “VHD”.

Restoring Entire Drive or Retrieving Backed Up Files

Anytime you need to restore it you just need to re-attach it and repeat the process, but reverse the cloning so the VHD is cloned to the physical hard disk. If you just need to copy some files off the VHD image, you can attach it and use it like any other hard drive, copying files off using Explorer. As an additional bonus, you can also attach the virtual drive to most virtual machines, including Oracle’s Virtual Box. You can also attach to a WSL session (at least WSL2) by attaching the disk in Disk Manager, assigning a letter to the Linux partition using Ext2 Filesystem Driver for Windows, and then opening WSL and typing “cd /mnt/[drive letter]”.