In this tutorial, we will show you how to use Docker to set up a WordPress-powered website.
WordPress is the backbone of many websites, providing a robust content management system that is incredibly easy to get started with. We even use it for our websites, as it makes managing and writing new articles a much nicer experience.
One of the ways that you can self-host a WordPress website is to utilize their Docker container. This container comes with a few things already set up for you. This includes PHP, and the Apache web server.
While this isn’t the way we personally host WordPress, it can be a great starting place for those who are just looking to get started with self-hosting a WordPress powered website. While we do use Docker containers for our hosting, we use customized ones rather than the ones from Docker Hub that we are using in this guide.
There are a few things you will want before proceeding any further. The first is a domain name. You can get these relatively cheaply by using providers like Cloudflare, which doesn’t charge extra fees on top of the registrars. There are also registrars like Namecheap (Affiliate Link) that often-run sales to get your first year relatively cheap.
Secondly, you will want a place to host your WordPress website. We personally use DigitalOcean (Affiliate Link) to host our websites as we have found them incredibly reliable and decently priced. You can even get away with the cheapest droplet as long as you have proper caching set up.
Of course, you can always self-host your WordPress website from your home, coupled with Cloudflare or a CDN provider like Bunny.net, and you can get away with it.
Setting up and Running WordPress using Docker
The following sections will walk you through setting up WordPress using a Docker container.
Ubuntu server is often a great choice if you still need to choose an operating system. Just ensure you stick with the LTS releases. These releases have significantly longer support times, so you don’t have to stress about doing a large operating system update.
To get WordPress up and running, we will use three additional containers. One is the database server, for which we will be using the MySQL container, and the other is the NGINX proxy container with a companion container that will automatically grab an SSL certificate from Let’s Encrypt.
SSL is an integral part of the modern web and key to helping make a secure more private, secure, and performant. Modern HTTP protocols have been designed with the expectation that there will be a valid SSL certificate. Best of all it doesn’t cost anything to use HTTPS thanks to services like Let’s Encrypt.
Preparing your Linux System for the WordPress Docker Container
1. Before you start, you must install the Docker runtime on your machine.
We highly recommend using our guide on installing Docker on Linux before continuing any further.
https://pimylifeup.com/linux-docker-install/
2. After installing Docker, we must create a directory to store the Compose file for the WordPress container.
You can create a directory at “/opt/stacks/wordpress
” by using the mkdir command within the terminal.
sudo mkdir -p /opt/stacks/wordpress
3. Once we have created a place to store the WordPress Docker compose file, change to that directory by using the cd command.
We will need to be in this location to make called to the Compose file after we have written it.
cd /opt/stacks/wordpress
Writing the Docker Compose File for WordPress
4. With this next step, we can begin writing the Compose file for our WordPress Docker container by running the following command,
You can use whichever text editor you feel comfortable with, but for this guide, we will be assuming that you are using Nano. Nano is a great starting text editor for those new to terminal-based editors.
sudo nano compose.yaml
5. You will want to enter the following lines within this file. These lines define the four containers we will use to run your WordPress Docker container.
While typing out these lines, there are three placeholders you must replace.
<DBPASSWORD>
: You must replace this with a secure password. This will be used for your WordPress Docker container to communicate with the database container.<DOMAINNAME>
: Swap this placeholder with the domain name that you want to use for your WordPress installation.<EMAIL>
: The provider we are using to provide SSL is Let’s Encrypt. This service requires you to have an email for it to notify you of an expiring or compromised certificate. Set an email address where you can be reached.
services:
wordpress:
name: wordpress
image: wordpress
restart: always
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: wpuser
WORDPRESS_DB_PASSWORD: <DBPASSWORD>
WORDPRESS_DB_NAME: wordpress
VIRTUAL_HOST: <DOMAINNAME>
LETSENCRYPT_HOST: <DOMAINNAME>
volumes:
- wordpress:/var/www/html
db:
name: db
image: mysql:8.0
restart: always
environment:
MYSQL_DATABASE: wordpress
MYSQL_USER: wpuser
MYSQL_PASSWORD: <DBPASSWORD>
MYSQL_RANDOM_ROOT_PASSWORD: '1'
volumes:
- db:/var/lib/mysql
nginx-proxy:
name: nginx-proxy
image: nginxproxy/nginx-proxy
restart: always
ports:
- "80:80"
- "443:443"
volumes:
- certs:/etc/nginx/certs
- html:/usr/share/nginx/html
- /var/run/docker.sock:/tmp/docker.sock:ro
labels:
- com.github.nginx-proxy.nginx
nginx-proxy-acme:
name: nginx-proxy-acme
image: nginxproxy/acme-companion
restart: always
depends_on:
- "nginx-proxy"
environment:
DEFAULT_EMAIL: <EMAIL>
volumes:
- certs:/etc/nginx/certs
- html:/usr/share/nginx/html
- /var/run/docker.sock:/var/run/docker.sock:ro
volumes:
wordpress:
db:
html:
certs:
6. Once you have finished filling out this file, everything should now be ready to go.
You can save and quit out of the WordPress Docker Compose file by pressing CTRL + X, Y, and then ENTER.
Starting up the WordPress Docker Compose Stack
7. Now that we have written a Docker Compose file for WordPress, we can start the whole stack by running the following command.
This process can take a few minutes as it must download images for four containers. However, subsequent updates should be faster.
Since we are using the “-d
” option, Docker will detach from the current terminal and continue to run in the background.
docker compose up -d
Accessing your WordPress Website
8. Now that you have the WordPress container up and running, you should be able to load it up in your favorite web browser and run through the initial setup steps.
One of the key features of WordPress is that it is incredibly easy to get started with.
https://<DOMAINNAME>
9. The first step will ask you to select the language you want to use for your WordPress installation (1.). WordPress is neat in that it features support for a huge range of languages.
Once you are happy with your selection, click the “Continue
” button (2.).
10. For this next step we need to fill out information that will be used to set up your WordPress website. We don’t have to worry about setting things like the database, which was already configured when the WordPress Docker container started.
- The first thing you need to set is a title for your website. This should be what you intend to call your site. For example, our website’s title is “
Pi My Life Up
“. - Next, you will want to set the username you want to use to log into your WordPress website.
- With the user specified, you will see a randomly generated secure password. You can choose to use this password or type in your own.
- Next, you must set an email to be assigned to your WordPress user. Ensure this is correct, as it is where the password recovery email will be sent.
- The final option here allows you to turn search engine visibility on or off. Only restrict search engine access if you don’t want search engines to index your website.
Please note that these are simply suggestions, and search engines are known to ignore them completely.
Once everything has been configured, click the “Install WordPress
” button (6.) to finish setting up your WordPress Docker container.
11. If WordPress was successfully installed, you should see a message telling you your username and showing that you have set a password.
Click the “Log In
” text to be taken to the login page.
12. On this next page you can type in the username and password you set during the setup steps (1.).
After entering your username and password, click the “Log In
” button (2.).
13. You have now successfully installed WordPress on your server using a Docker container.
Now is a good time to dive into WordPress and learn how to install plugins, write posts, and more. You will find the whole process straightforward once you get started.
Updating your WordPress Docker Stack
In this section, we will be showing you how to update your entire WordPress Docker stack. While WordPress itself will now be updated through its own interface, many of the containers we use get important updates occasionally.
Luckily, Docker makes updating all the containers you set within a Compose file a very simple process. These next few steps will show you how easy this process is.
1. Your first step is to change to the directory where you wrote the Docker Compose file for WordPress.
Assuming you followed our guide exactly, you should be able to swap to the correct directory by using the cd command as shown below.
cd /opt/stacks/wordpress
2. After swapping to the correct directory, we can use the Docker Compose pull command to get it to download the latest release of all of our containers.
This process might take a few minutes to complete if all four containers happen to have updates available.
docker compose pull
3. While Docker will download the updates using the previous command, it won’t update the already running containers.
Luckily getting Docker to move your existing containers over to the new release is as simple as using the following command. You may have a brief downtime as your containers are stopped and started back up.
docker compose up -d
Conclusion
If you have reached this point, you should have successfully managed to get a WordPress website up and running using Docker.
To get WordPress to work, we had to use four different Docker containers. One is WordPress itself, another is the database where all your posts will be stored, and the final two give you HTTPS support for your website.
Please feel free to comment below if you have had any issues setting up the WordPress container.
If you liked this quick tutorial, we recommend taking a look at some of our many other Docker tutorials.