Install WordPress with Docker

Using Docker WordPress Cli to Manage WordPress Websites

The WordPress Cli (or WP-CLI) is a command line tools for managing WordPress websites through the command line. In this tutorial, you will learn:

  • What WP-CLI is,
  • how to install WP-Cli using Docker and
  • Overview of the basic commands of this tool.

WP-CLI will be particularly useful if you are a WordPress developer, System Administrator or run a business built around WordPress. This command line tool will greatly help you do more in less time. For example, taking backups, updating WordPress and plugins, publishing content and querying databases can be accomplished relatively quickly. For example, you can use this plugin to automatically install Woocommerce in WordPress.



Contents:

Requirements of WP-Cli

  1. SSH access to your server
  2. PHP 5.3.2 or later.
  3. WordPress 3.4 or later.
  4. UNIX like environment like Linux.

Install WP-Cli

You can either 1) directly install WP-Cli on your system or install it using Docker.

Install directly on your system

# 1/ Download wp-cli
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
# 2/ Make it executable
chmod +x wp-cli.phar
# 3/ Move it into /usr/local/bin/wp
sudo mv wp-cli.phar /usr/local/bin/wp
# Check whether the installation worked
wp --info

Now, you can simply type wp in your terminal to call WP-Cli. By the way, remember to run wp from the WordPress root directory.

Install WordPress using docker-compose and WP-ClI

See our previous tutorial to automatically install and configure wordpress using docker-compose and WP-CLI.

You can automatically deploy a local docker WordPress site in 5 minutes using the following commands:

# Download a wordpress docker-compose example
git clone https://github.com/kassambara/wordpress-docker-compose
cd wordpress-docker-compose
# Automatic installation of wordpress
make autoinstall

Visit your site at http://localhost and your database via phpMyAdmin at http://localhost:8080.

Website

phpMyAdmin

Default identification for your wordpress website admin:

  • Username: wordpress and
  • Password: wordpress

Default identification for the phpMyAdmin interface:

  • Username: root and
  • Password: password

This installation includes also the WP-ClI tool. In the next sections, we’ll use the docker-compose WP-ClI commands. So, start by creating an alias to simply call the WP-CLI docker-compose commands:

wp="docker-compose run --rm wpcli"

Update WordPress

# Upgrade to the latest version
$wp core update
# Go back to an older version
wp core update --version=4.9.6 --force

Plugin commands

Name Description
wp plugin activate Activates one or more plugins.
wp plugin deactivate Deactivates one or more plugins.
wp plugin delete Deletes plugin files without deactivating or uninstalling.
wp plugin get Gets details about an installed plugin.
wp plugin install Installs one or more plugins.
wp plugin is-active Checks if a given plugin is active.
wp plugin is-installed Checks if a given plugin is installed.
wp plugin list Gets a list of plugins.
wp plugin path Gets the path to a plugin or to the plugin directory.
wp plugin search Searches the WordPress.org plugin directory.
wp plugin status Reveals the status of one or all plugins.
wp plugin toggle Toggles a plugin’s activation state.
wp plugin uninstall Uninstalls one or more plugins.
wp plugin update Updates one or more plugins.

Plugin list

$wp plugin list
+---------+----------+--------+---------+
| name    | status   | update | version |
+---------+----------+--------+---------+
| akismet | inactive | none   | 4.1.3   |
| hello   | inactive | none   | 1.7.2   |
+---------+----------+--------+---------+

Delete plugin

$wp plugin delete akismet hello

Install and activate plugin

# Install and activate new plugin
$wp plugin install woocommerce --activate
# deactivate a plugin
$wp plugin deactivate woocommerce
# Only activate a plugin
$wp plugin activate woocommerce
# Update plugins
$wp plugin update woocommerce
$wp plugin update --all
If you want to install and activate multiple WordPress plugins at once, you can mention multiple plugins in the same command.


Plugin version and details

$wp plugin activate woocommerce
+-------------+-----------------------------------------------------------------+
| Field       | Value                                                           |
+-------------+-----------------------------------------------------------------+
| name        | woocommerce                                                     |
| title       | WooCommerce                                                     |
| author      | Automattic                                                      |
| version     | 3.8.1                                                           |
| description | An eCommerce toolkit that helps you sell anything. Beautifully. |
| status      | active                                                          |
+-------------+-----------------------------------------------------------------+

Theme commands

Name Description
wp theme activate Activates a theme.
wp theme delete Deletes one or more themes.
wp theme disable Disables a theme on a WordPress multisite install.
wp theme enable Enables a theme on a WordPress multisite install.
wp theme get Gets details about a theme.
wp theme install Installs one or more themes.
wp theme is-active Checks if a given theme is active.
wp theme is-installed Checks if a given theme is installed.
wp theme list Gets a list of themes.
wp theme mod Sets, gets, and removes theme mods.
wp theme path Gets the path to a theme or to the theme directory.
wp theme search Searches the WordPress.org theme directory.
wp theme status Reveals the status of one or all themes.
wp theme update Updates one or more themes.

Theme list

$wp theme list
+-----------------+----------+--------+---------+
| name            | status   | update | version |
+-----------------+----------+--------+---------+
| twentynineteen  | inactive | none   | 1.4     |
| twentyseventeen | inactive | none   | 2.2     |
| twentysixteen   | inactive | none   | 2.0     |
| twentytwenty    | active   | none   | 1.1     |
+-----------------+----------+--------+---------+

Activate a theme

$wp theme activate twentyseventeen

Update theme

# Update one theme
$wp theme update twentyseventeen
# Update all theme
$wp theme update --all

Search and replace

This is command is particularly useful when you have changed the site’s URL or even added HTTPS to the site’s URL.

First make a dry run:

$wp search-replace {old URL} {new URL} --dry-run

The above command will just display the expected result once this query is run. It is done as a precautionary step to make sure the result will be as per the expectation. Once you are satisfied you can make actual replacement by removing –dry-run from the above command, as follows:

$wp search-replace {old URL} {new URL}

Content migration

  • wp db export: Exports the database to a file or to STDOUT.
  • wp db import: Imports a database from a file or from STDIN.
# Exports all the database to a file in wordpress directory
$wp db export db.sql

# Export certain tables
$wp db export --tables=wp_options,wp_users

# Import a database
$wp db import db.sql

It efficiently migrates all the data and information including posts, links, dates, authors, comments, etc, and match to the existing WordPress taxonomies accordingly.

Additional database management commands:

Name Description
wp db check Checks the current status of the database.
wp db clean Removes all tables with $table_prefix from the database.
wp db cli Opens a MySQL console using credentials from wp-config.php
wp db columns Displays information about a given table.
wp db create Creates a new database.
wp db drop Deletes the existing database.
wp db export Exports the database to a file or to STDOUT.
wp db import Imports a database from a file or from STDIN.
wp db optimize Optimizes the database.
wp db prefix Displays the database table prefix.
wp db query Executes a SQL query against the database.
wp db repair Repairs the database.
wp db reset Removes all tables from the database.
wp db search Finds a string in the database.
wp db size Displays the database name and size.
wp db tables Lists the database tables.

Manage users

# List user IDs
$wp user list 

# Create a new user.
$wp user create bob bob@example.com --role=author

# Update an existing user.
$wp user update 123 --display_name=Mary --user_pass=marypass

# Delete user 123 and reassign posts to user 567
$ wp user delete 123 --reassign=567



Version: Français

(Next Lesson) WordPress Docker Setup Files: Example for Local Development
Back to Install WordPress with Docker

No Comments

Give a comment

Want to post an issue with R? If yes, please make sure you have read this: How to Include Reproducible R Script Examples in Datanovia Comments

Teacher
Alboukadel Kassambara
Role : Founder of Datanovia
Read More