Running Wordpress In Docker For The First Time
A simple start into the world of containerized development with WordPress and Docker.
You probably have stumbled over Docker multiple times already. People use it to deploy all kinds of web apps, using it for neat development processes and everybody has been pretty excited about it for quite a while.
But what is it? Is it any use when developing WordPress sites, plugins and themes? What’s the use?
If you’re completely new to Docker or would like to learn why it’s popular explained without the hype, check out this post to brush up on the basics.
In short: it’s a way to run apps on a machine, without them leaving a trace or getting in the way of others. Think lightweight, portable, very efficient virtual machines - which are technically speaking not VMs at all, container is the term. Docker is lightweight and has less resource hunger compared to virtual machines
When developing locally, you can use different Docker container stacks for each project without much overhead, or jumping through confusing setup hoops. You can use Docker for deploying multiple WordPress sites on a single machine and update them independently without risking one of them breaking while you push an update for the other.
Here is a list benefits, which I enjoy when using Docker for web development projects:
- Containers don’t make your development machine feel cluttered - no new apps and dependencies for each project. Everything is tidy and cleanly contained in individual containers.
- Several containerized deployments can’t break each other casually. Ever gotten one of your sites down while fixing the main Apache php.ini for another? Can’t happen here.
- Moving dev machines and servers is both really easy - you can be sure it will work out of the box. Or at least it’s not the host’s fault :)
- It’s reproducible. You can go to another computer which has Docker installed, and get your stack running without caring whether it’s Windows, Mac or some Linux flavor. Like the best “getting started” documentation you have ever written.
- Collaborating on a single project and getting help is easy. If people can reproduce your complete environment with one command, it’s easier to get help online on public projects as there’ll be no “works on my machine” answers.
- One more sharp tool in your tool belt, which you’ll be able to use for great production deployments eventually. No more fighting between multiple WordPress sites on your server.
Interested? I thought you might be. It’s a great tool to know about. This post is about starting small. Let’s not jump into gory details and make everything perfect. The aim is to dip a toe, get something functional and understand what’s going on.
The following outlines simple steps you can follow, to get your first WordPress setup running in Docker on your local machine. We can build on this later regarding learning, using the knowledge to get into other projects like the wordpress-starter or wp-local-docker quicker and with confidence, or to build the right thing for your personal workflow.
The goal is to understand the tool and be able to judge for yourself if it’s something you need and would like to use.
Installing Required Tools
Getting started with Docker, used to be challenging. It’s not anymore. You can get it to run on most systems in a breeze.
First you’ll need to get the Docker Engine running on your development machine. Just choose the right option from below, depending on your OS:
Whatever your system, the installation is very simple and almost works out of the box. Make sure that Docker is running and you can type:
$ docker ps
and get an output.
The next and last one, is Docker Compose. It’s a nifty tool to bring up groups of containers, which work together. In our case, we want the database to be in one container, and the WordPress site in another. Both need to be able to see each other, and be configured correctly. Docker Compose makes all of this really simple.
Follow the installation instructions and you’re all set to get going.
Writing A Docker Compose File
Almost there. Here’s what’s left to do.
Create a new folder for the project. Create a file called docker-compose.yml in it. It will be used by docker-compose to bring up Docker containers as specified within.
Once again: this setup is meant to get started with Docker and WordPress. It’s not yet tuned to be really good for an enjoyable local development environment, neither is it suitable to run a site on anything resembling a production server. But it’s not far from both.
That said, let’s take the leap and run a functional WordPress setup without littering around your local development machine.
Here is what you need to copy into your docker-compose.yml file:
version: "3" services: db: image: mariadb # uncomment the following two lines for data persistence #volumes: # - ./db_data:/var/lib/mysql environment: - MYSQL_ROOT_PASSWORD=simplepw # uncomment the following lines, to be able to access the database on your machine #ports: # - "3306:3306" wp: image: wordpress volumes: - ./wordpress:/var/www/html ports: - "8080:80" environment: - WORDPRESS_DB_HOST=db - WORDPRESS_DB_PASSWORD=simplepw
Using this will result in running a MariaDB in a single container, and an Apache server with WordPress and PHP all set up in another. Both can communicate with each other.
For this, we are defining two services in this group of Docker containers: one called “db” and one called “wp”. Both names are arbitrary. Both are using the default images of WordPress and MariaDB, provided directly by the companies - no need to build our own. They are configured using environment variables.
“Volumes” tells Docker to take a local directory called “wordpress” and to make it available to the container under /var/www/html. The wordpress image puts WordPress files there when it’s started. This way, we can edit files locally but still execute them in the container.
The “ports” part, makes it possible to access the WordPress installation on port :8080 of your local machine once the containers are up and running.
Bringing The New Containers up
With this file in place, all that’s left is navigating to the folder in a terminal, and typing
$ docker-compose up
We can see that both containers have starting and are working while writing out lots of lines. Neato.
If you’re curious about what the Docker-side looks like, open another terminal and take a look at the container overview with
$ docker ps
You should see the two containers, and a bunch of info on them.
One more step before you can start tinkering around and changing files: If you’re working on Mac or Ubuntu, you’ll need to make sure that you can read and write the files which were created with
$ sudo chmod -R +rw wordpress
Right now, you’ll need to do this every time you restart the container stack, as the image takes ownership of the data. That’s fixable, but not needed right now.
Now you can finally navigate to the address and take a look at our new WordPress installation, running completely in containers.
You can edit the WordPress files in the new directory, and see the results in the browser. Meanwhile, they are executed by a PHP version inside the started container which has nothing to do with your local system, while the data is saved in a temporary MariaDB database. Clean and tidy!
When you’re done, simply “ctrl+c” the terminal command and wait for the containers to shut down.
To make sure they’re down, you can also use:
$ docker-compose down
A First Setup With Few Steps
All done. Using Docker can be a daunting task, with a lot of confusion, but when starting right, focusing on getting something to work and getting the basics straight, it is a very powerful and useful tool for any WordPress developer.
This setup is only for getting started. The database does not “remember” data in between container restarts (except if you uncomment the respective two lines in the yml file), as all data on a container is lost if it’s not saved to a volume or local directory.
We’d need to change a few things so it’s not starting from scratch each time around. That’s a topic for another time. From here, we can work our way towards a Docker configuration focused on WordPress development, while learning useful about useful features and advanced techniques.
I’m glad you went ahead and tried Docker with WordPress for the first time! If you’re interested in learning more, drop me your email below to be notified about future articles about Wordpress and Docker.