Today I’m going to show you how Docker and Docker Compose can be used to host a wordpress site within a portable docker container.
- Docker installation (with docker compose) on any x64 host.
- Internet access 🙂
I assume that you already know the advantages of containerizing your applications via docker. However, if not, here I’ve listed some of the greatest benefits of using docker containers over VMs.
1. Navigate to the directory where you want the container’s configuration YAML-file to be located at.
mkdir -p ~/docker/wordpress/ cd ~/docker/wordpress/
2. Create docker-compose.yml
sudo nano docker-compose.yml
If you are not fimiliar with using the nano commandline-editor you can read this short tutorial.
Docker Compose Files
In this tutorial I will skip explaining all the details about docker-compose.yml files. If you want to learn how to write your own docker-compose configuration consider reading my blog post that topic.
For short: We define the attributes of our containers within one YAML file called docker-compose.yml.
The installed docker-compose program can then create docker containers from then by simply executing one command.
- MariaDB (OpenSource version of MySQL) Database container
- Adminer web interface for the MySQL database (More powerful alternative to phpMyAdmin)
- WordPress webserver
version: '3.5' services: db: image: mariadb:10.6 volumes: - ./db/:/var/lib/mysql restart: always environment: MYSQL_ROOT_PASSWORD: "mysqlRootPassword_KEEP_SECRET" MYSQL_DATABASE: wpDb MYSQL_USER: dbAdmin MYSQL_PASSWORD: "passwordForUserDbAdmin_KEEP_SECRET" networks: - backend adminer: image: adminer restart: always ports: - 8080:8080 wordpress: depends_on: - db image: wordpress:5.7-php7.4-apache restart: always environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_NAME: wpDb WORDPRESS_DB_USER: dbAdmin WORDPRESS_DB_PASSWORD: "passwordForUserDbAdmin_KEEP_SECRET" working_dir: /var/www/html volumes: - ./wp-content:/var/www/html/wp-content - ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini networks: - backend - proxy-network ports: - 80:80 networks: backend: driver: bridge proxy-network: driver: bridge name: proxy-network
This certain network configuration will allow us to put a reverse proxy container in front of our web server in order to host different sites on our machine.
If you want to configure your containers differently, read my article on docker-compose.
Here we are using the docker images (container templates) for MariaDB, Adminer, and WordPress from hub.docker.com. There you can also find different configuration tweaks. If you want to update your containers later on (e.g. new WordPress version) you only have to “docker-compose down” (see further below), change the version string of the WordPress image (see WordPress DockerHub) and composing the containers again.
Generating and starting the containers
Now, after we’ve saved the YAML file with Nano’s command “CTRL+O & ENTER”, we are ready to generate our docker containers from this certain docker-compose.yml file. For that, we only need to run
sudo docker-compose up # or for detached console sudo docker-compose up -d
If you are doing that for the first time, I would recommend the command without the “-d” option first to see what happens when docker-compose generates our containers.
Once the containers are up and running we will see them in the list of containers which can be displayed via
sudo docker ps
If everything worked as expected we will now be able to access our WordPress web server via the browser at localhost on port 80 (default web port, see our port linking in docker-compose.yml): http://localhost or http://127.0.0.1. The port can also be specified explicitly with http://localhost:80/ but since 80 is the default web port, it isn’t necessary.
Tipp: While writing this article and testing the commands I noticed that my browser will only display the Nginx default page from a previous container (which isn’t running anymore). This type of error can be quite frustrating since you’ve done nothing wrong. The browser is just displaying a cached version of the site. Clearing the browser cache will solve that issue.
Since this tutorial focuses more on the docker side of things I just want to refer to my upcoming tutorial on setting up WordPress.
Terminating the containers
If you want to stop the container you can simply CTRL+C (cancel) out of the script. If you’ve generated the containers with the “-d” option you can also
sudo docker-compose down
In contrast to CTRL+C which only stops the containers “docker-compose down” also removes the containers from docker and only keeps the linked volumes (directory linkings between containers and host). In our case that is no problem since we have stored all permanent data in directories relative to the docker-compose.yml file.
Working with domains
I highly encourage you to use your production domain from the begging on since it is quite messy to change all links after the site has been fully constructed.
If you already have your domain you can simply let it point to your current IP address (be aware that DNS changes can take up to 24h to fully take effect). Because of this and since your most likely don’t have a static IP address (there’s still the possibility of DynDNS), I recommend modifying your system’s host file. You can thereby simply tell you’re computer that “mydomain.com” is on this computer, even though in reality the domain does not even need to be owned by you. You can find more details on that here.
You’ve now learned how to quickly install WordPress with only one configuration file. With the corresponding docker containers, you are now able to configure your WordPress site.
But be aware that you should not just make the WordPress installation public after you’ve configured everything. Remember to remove your host file entry and use real domain A-records.
It is also quite important to install an HTTPS certificate for your website to be secure and law conform.
If you plan to host your production server yourself you also have to allow HTTP(S) requests through your routers (and potentially your computers) firewall. You can surely find suitable resources for your router on the internet. Just search for the terms “Port forwarding” in combination with your router’s product name.
To install HTTPS certificates and host different sites on one machine, it is convenient to use a reverse proxy docker container. You can find out more about that here.