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”.

d

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.

Add Random Post with Shortcodes Ultimate

Wordpress Plugins

Generating a Random Post in WordPress

There are many plugins out there that an generate a random post to be inserted into your website. An example of such a random post is shown to the right of this entry in the box-set off. Some of the plugins come with their own shortcodes. But there’s no need to install another plugin if you have Shortcodes Ultimate installed.

Shortcodes Ultimate by Vladimir Anokhin is a popular plugin for WordPress supplying numerous helpful shortcodes to add various features to your posts.

Using Shortcodes Ultimate to Generate a Random Post

This method permits you to customize the random listing so that it can include only the title, the title and the featured image, or even the entire content.

  1. Install the Shortcodes Ultimate plugin if you haven’t already.
  2. Customize Your Shortcodes Ultimate Template

  3. On your server, in the “wp-content/plugins/shortcodes-ultimate/” folder, create a new file called “random-post.php”. You can do this through the file manager on CPanel or other frontend on your host (or through SSH if you have command line access).
  4. Grab This Code for Your Shortcodes Ultimate Template

  5. Edit the “random-post.php” to add code for your random post – use (and modify if you wish) from one of the following:
     

    Template Code to Show Random WordPress Post Showing Just the Title and Featured Image:

    Template Code for Random Post To Show Full Content

    Random Template Code To Show The First Few Paragraphs of WordPress Post

    NOTE: This option is not recommended as it will arbitrarily truncate your post content including HTML tags which may mess up the formatting on the rest of the page where this code is inserted) .

  6. After pasting in one of the three above, save the file.

Try Out The Random Post Short Code

  1. Click on the Shortcodes Ultimate button in your editor:

    Shortcodes Ultimate Insert Shortcode Button

  2. A dialog will open. Click on the “Posts” code:

    Shortcodes Ultimate Posts Button

  3. Edit the Template name to read “templates/random-post.php” to match the name of the template you just created.
  4. Screenshot Template Name

  5. Complete the rest of the dialog options. When finished, click “Insert” to insert the Posts short code.
  6. Preview the post to verify that a random post was inserted.