At the moment I am writing this post, I have been working with WordPress for almost 8 years. Some of this time, as a freelancer, and back then 2015 I started to work using XAMPP to set up my developer environment.
XAMPP is a free and open-source cross-platform web server, primarily used to locally develop web applications. Turns out, that despite the easy setup and installation of files, XAMPP is limited in some aspects that Docker isn't.
After developing and testing a project with XAMPP on your machine, it will have to be deployed in a real server at some point. While working with XAMPP you'll need to do a lot of work to ensure that PHP versions, extensions, and configuration are the same on your machine as in the server.
This process can be very frustrating and can sometimes result in sites just not working as you expected.
The other problem is that XAMPP makes us lazy, as it is very easy to install, it gives you zero knowledge of how a web server is actually configured. I advocate for everyone to set PHP, MySQL, and all stuff apart, at least once, to understand how things work, but this is a subject for other articles.
On time, it's very common to find in forums online questions like "How do I use PHP 7.4 in XAMPP?, "How can I run two different PHP versions?" and etc. But if you want to work in a more modern and straightforward way, with remote teams for example, the right question should be "Is XAMPP the right tool?".
It was making this question that I discovered Docker. By definition, Docker is an open platform that enables developers and system administrators to create distributed applications. With it, you can upload the entire application, code, and configurations, and the site will run exactly the same on the server as it does on your development machine. It is the end of the "it works on my machine" problem.
Docker can handle lots of repetitive configuration tasks and is used throughout the development lifecycle for fast and easy application development.
Below is my current Docker WordPress environment, with WP-Cli and phpMyAdmin. The complete repository available here.
//docker-compose.yml
version: '3.9'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: ${MYSQL_DATABASE_NAME}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_RANDOM_ROOT_PASSWORD: 'root'
volumes:
- ../db:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
restart: always
depends_on:
- db
ports:
- "8080:80"
environment:
PMA_HOST: db
PMA_USER: ${MYSQL_USER}
PMA_PASSWORD: ${MYSQL_PASSWORD}
wordpress:
build: .
restart: always
depends_on:
- db
ports:
- ${WORDPRESS_PORT}:80
environment:
PHP_EXTENSION_XDEBUG: 1
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: ${MYSQL_USER}
WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD}
WORDPRESS_DB_NAME: ${MYSQL_DATABASE_NAME}
volumes:
- ../wp-core:/var/www/html/
- ../plugins:/var/www/html/wp-content/plugins
- ../themes:/var/www/html/wp-content/themes
- ./xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini
wpcli:
image: wordpress:cli
depends_on:
- wordpress
- db
volumes_from:
- wordpress
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: ${MYSQL_USER}
WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD}
WORDPRESS_DB_NAME: ${MYSQL_DATABASE_NAME}
user: xfs
entrypoint: wp
command: --info
Finally, the 5 reasons why I changed from XAMPP to Docker.
Isolation: Docker allows you to break your app down into separate microservices, each containing its own function. These services are independent and can be easily isolated from each other.
Portability: Developing inside containers brings independence from the OS (Mac, Windows, or Linux) you’re using and makes your apps portable.
Sharing: You can package the environment with the application, pack the app and its dependencies, and transfer it to another machine(s) with different hardware and OS setup, and still, the containers guarantee you’re running the same services (versions, configurations, etc).
Highly configurable: You can configure containers with docker-compose files to meet your particular needs (e.g., I added phpMyAdmin and WP-CLI to my WP environment).
Open source: there is a lot of support in the community.
I am not saying that you must change the whole workflow you are used to, but I believe you really should give Docker a try.
Be free to clone my environment, or even suggest improvements trough pull requests on Github, or in the comments below.
Top comments (4)
Nice article! You could check out the awesome open source tool DDEV for Docker. Sharing project configurations is very simple with it and it provides helpful tooling like built-in WPCLI support: ddev.com Cheers!
Thanks Mathias, I will check it!
I felt the same way about XAMPP. I needed to level up from it. I tried Docker using Compose like you've done. But I wanted to spend making stuff, getting stuff done but it can get pretty complicated pretty quickly, in my opinion. And if you want something like local SSL, yikes!
Instead I looked at the tools built on top of Docker to simplify it and my life. Like @mandrasch I looked at DDEV. But then I found Lando, which I think is even better, even simpler yet more powerful and flexible (not just for PHP based containers).
Thanks Trevor, I will check it too!