If you are looking for a way to create isolated environments to test your WordPress, then familiarize yourself with container technology. In this article, we’ll cover a step-by-step guide to install WordPress on Docker, the best-known container platform.
What Exactly Is Docker?
Docker is an open-source containerization software that creates isolated environments to run an application. Hence, you develop, test, and run multiple applications on the same machine.
In contrast to virtual machines, each container does not require its own OS and shares the host’s kernel. Thus, the machine’s workload is far more lightweight and a single server can run multiple containers at the same time.
For that reason, Docker is highly useful for WordPress developers. A WordPress test environment usually eats up a lot of system resources, but Docker helps them make a minimal environment without wasting server space and memory.
How to Install WordPress on Docker?
Follow the steps below and learn how to install WordPress on Docker.
Step 1: Installing Docker
Docker is available for Windows, macOS, and Linux. Here’s how you can install it on those three operating systems:
If you’re using Ubuntu 18.04 LTS, follow these steps:
- Update the package list:
sudo apt-get update
- Let apt access a repository over HTTPS:
sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
- Add the Docker’s GPG key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
- Add the Docker’s repository:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
- Update the Docker’s repository:
sudo apt-get update
- Install Docker:
sudo apt install docker-ce
Keep in mind that other Linux distributions (CentOS, Debian, or Fedora) have different installations steps. If you don’t use Ubuntu, see Docker’s official documentation.
macOS X Installation
Here are the procedures to install Docker on macOS:
- Download Docker for Mac and double-click the .dmg file you’ve saved. Then, drag and drop the Docker icon to your Applications folder. You can find download links here:
- Open your Applications folder and double-click docker.app. During the configuration process, you’ll be asked to enter your password.
- Once the installation process is finished, you’ll see the docker menu in your desktop’s status bar.
Here’s how you can install Docker on Windows 10 64-bit:
- Enable Hyper-V in your system.
- Download Docker Desktop for Windows and open the Docker for Windows Installer file.
- In the Configuration dialog window, check or uncheck the boxes based on your preferences. Click Ok.
- Once the installation is finished, hit Close. You’ll see the Docker icon in the taskbar.
Step 2: Setting-Up WordPress on Docker
Next, let’s set up WordPress on Docker. You can do this process with these two methods ‒ the CLI and Docker compose.
In this article, we’ll be using Docker compose, the cleaner and more systematic method. Here’s how:
- Check Docker Compose Installation:
- Create a new directory for WordPress:
mkdir ~/wordpress/ cd ~/wordpress/
- Create a new docker-compose.yml in the new directory and paste the content below. Don’t forget to change the credentials.
version: '3.3' services: db: image: mysql:5.7 volumes: - db_data:/var/lib/mysql restart: always environment: MYSQL_ROOT_PASSWORD: somewordpress MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: wordpress wordpress: depends_on: - db image: wordpress:latest ports: - "8000:80" restart: always environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: wordpress WORDPRESS_DB_NAME: wordpress volumes: db_data:
- Run this command in the directory to create the containers:
docker-compose up -d
- Your browser will enter localhost:8000 and display the WordPress setup screen.
Docker is a great containerization tool to experiment with WordPress. Its minimal environment helps you maintain the efficiency of your system resources.
In this tutorial, you’ve learned how to install Docker on Linux, macOS, and Windows. You’ve also learned how to set up WordPress on Docker using the Docker Compose utility.
We hope this simple tutorial is helpful. If have any further questions, share with us in the comments section below.