How to setup WordPress cli in Docker ?
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.
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.
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.
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.]
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.