WordPress is the most important and used content management system. So I still love it no matter what they say :)
Let’s have a look at the statistics to see how popular it is.
- According to W3techs, WordPress has 61.8% of the CMS market share. It is more than all other systems combined.
- 35% of the Internet is Powered by WordPress
- etc etc
Sure, my aim is not convincing you about how WordPress is big or important. So let’s continue with our main focus for this article.
You know, to use WordPress, we need minimum requirements.
- A webserver (Apache, Nginx, IIS, etc)
- PHP module
- A MySQL Database (Mysql or MariaDB)
On Linux, this is easier but on Windows, it was a little bit terrible. But we are lucky, there is a great tool to handle both MySQL and PHP installation: XAMPP
Formerly, I was using XAMPP on my local but after meeting Docker, I have realized that a docker container is more logical.
In this article, I will try to explain how to set up my development environment.
We are lucky, WordPress has its official image on the repo: https://hub.docker.com/_/wordpress
This image allows you to create your WordPress container in seconds. Just missing thing is MySQL. If you want, you can use your container or MySQL instance. Or by using the below docker-compose YAML file, you can create your containers concretely.
As you can get it, firstly, a database container will be created with target database credentials and by using this database credentials, the second container for WordPress instance will be created.
It’s quite easy. Next, it is time to say what I added to this process. Normally, all WordPress files will be created on the container but we need to update them or add a new one, right?
But using Linux bash or any word processors on the command line like nano, vi, etc is not easy.
In my humble opinion, the easiest way is mounting a host folder to the container as a volume, so using a modern IDE like Atom, etc option will be available.
Simply I have mounted a folder from my host OS to WordPress container as shown below. The folder choice is completely arbitrary, you can use whatever you want.
But there is a tiny but so important clue.
If you are using Docker on Windows, you need to define path under Resources section on Docker Dashboard.
I faced below problem and fixed by above action.
Ok, if we are ready, go ahead:
- Firstly, put about code as the content into a YAML file. The file name should be
docker-compose.yml or docker-compose.yaml
- on cmd, go to the folder which contains this file
- run command for composer
docker-compose up -d
Compose will download related images and setup everything. I added the “d” option, because of no need to see bash logs, etc. But it is up to you.
If you see a result similar to below, yes, it’s done.
Go to your favorite browser and type:
If you don’t like to use 8000 or another application has already occupied that port, you can change it YAML file under “ports” part
Because we have already provided database credentials, WordPress welcomes site options page. Then you know the rest of the installation.
Let’s go back to an important point. Now we can browse our mounted folder on host OS:
Do you see lovely WordPress staff? wp-content is here and you can change its content. Then it will be reflected in WP directly.
Let’s add a sample template:
And just refresh “Themes” page on the browser:
Stop the containers
If you want to stop these containers, you can use kill command or use below:
But if you prefer to remove all data and images, this is another option:
docker-compose down -v --rmi all
--rmi type Remove images. Type must be one of:
'all': Remove all images used by any service.
'local': Remove only images that don't have a
custom tag set by the `image` field.
-v, --volumes Remove named volumes declared in the `volumes`
section of the Compose file and anonymous volumes
attached to containers.
Now, open your favorite IDE and start to develop great tools for WordPress.