Bobcares

How to setup WordPress cli in Docker ?

by | Apr 1, 2019

Docker has been always a favorite choice for WordPress lovers. It’s a quick way to replicate your custom WordPress settings.

Luckily, WordPress CLI helps to interact and manage your WordPress sites from the command line. And, it offers immense possibility to customize the Docker containers.

That’s why, we often get requests from server owners to configure WordPress CLI in Docker containers as part of our Infrastructure Management Services.

Today, we’ll see how we make use of WordPress Cli Docker containers to get the best WordPress hosting.

 

Why Docker?

Firstly, let’s have a look on the benefits of choosing Docker for WordPress websites.

Docker provides a stable environment for WordPress. And, it allows creating identical containers quickly. This comes handy when you need to offer the same services to your customers.

Additionally, Docker makes it really easy to move WordPress websites from one platform to another.

 

Why WordPress CLI?

WordPress comes with a good management interface that help in easy website edits. Then, why do we need WordPress CLI?

Suppose, there are 100 WordPress websites and you need to do add a post to each of them. When using WordPress panel, we need to login to each WordPress. Unlike the panel method, things are pretty easy with command line tools.

WordPress command line tool aka WP-CLI make interacting with and managing your WordPress sites easy. It allows you to update plugins, configure multi-site installs and much more, from the command-line itself.

That’s why, our Dedicated Engineers configure containers with docker-compose files with wp-cli tool to meet server specific requirements.

 

Steps to enable WordPress CLI in Docker

Now, let’s check on the steps to enable WordPress CLI in docker containers.

 

Using images

Docker provides a list of preset images available at hub.docker.com. And, the easiest way to add cli support is to use WordPress images that ships with CLI option. The image wordpress:cli just contains the WP-CLI tool.

Or you can add the WordPress CLI to the docker container and then create an image based on it.

For instance, we created a container ‘wp1’ and then installed wp cli in it. Later, we created a new Docker image ‘wordpress_with_cli‘ based on the existing container. Thus, the new image showed up in the list of images.

wordpress cli docker

Another way to use it with an existing WordPress Docker container would be:

$ docker run -it --rm \
    --volumes-from some-wordpress \
    --network container:some-wordpress \
    wordpress:cli user list

 

Here, for WP-CLI to interact with the WordPress install in the Docker, our Support Engineers provide access to the WordPress files and access to the database.

 

With Docker compose

Similarly, we can set up WordPress CLI using the docker compose file too. Luckily, Compose uses a YAML file to configure your application’s services. Therefore it helps to create and start all the services from the preset configuration using a single command.

To enable wp-cli, our Support Engineers add a section in the docker-compose file with the following contents.

cli:
image: wordpress:cli
volumes:
- ./site:/var/www/html/

After we saving the docker-compose file in its folder, we run it with the following command:

docker-compose up -d

And, when the site is up and running, we run the following command to connect to the container with WordPress CLI:

docker-compose run --rm cli bash

Finally, we can switch to the WordPress directory and then run WP-CLI.

 

Usability issues with WordPress CLI and fixes

From our experience in managing Dockers, we often see customer experiencing problems with WordPress CLI. Let’s take a look at the top usability issues and how we fix them.

 

1. wordpress:cli container cannot modify files

Recently, one of our customers had problems where the WP-CLI in cli container was not able to manipulate files on the  main WordPress container. When wp-cli sets up the installation, he was getting a warning:

Warning: Unable to create directory wp-content/uploads/201x/03. Is its parent directory writable by the server?

Here, the problem happened as the user ids were out of sync in the Docker container. Therefore, to fix the problem, we had to issue the Docker run commands as the specific user.

 

2. Failing to fetch updates

Similarly, we often see WordPress cli creating problems while creating new WordPress websites. On detailed checking, our Dedicated Engineers could see that site creation failed in command line. Additionally, the custom script showed the following error about missing wp-config file.

grep: /home/xxx/files/staging.abc.com.au/wp-config.php: No such file or directory

In this case, the WordPress cli problem was caused due to the outdated WordPress release. Somehow, the WordPress version used in the image was old. Therefore, to fix the problem, our Support Engineers configured the docker image to install the latest WordPress. Also, we included the latest PHP version in the docket image too.

[Finding problems with WordPress cli ? We can fix it for you.]

 

Conclusion

In short, WordPress cli in Docker containers helps greatly in automating WordPress maintenance tasks. Today, we saw how our Support Engineers enable WordPress CLI in Docker and fix common errors with it.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF