Maab S.
6min Read

How To Install WordPress on Docker: Windows and Linux

WordPress install on Docker

Install WordPress on Docker: Windows Server 2019, Ubuntu 20, Debian 11, CentOS Stream

One of the easiest and quickest ways to set up and test WordPress is by installing it inside a Docker container. Whether you want to run WordPress on your personal laptop, or want to configure isolated testbed environments on a virtual server, containerized WordPress instances are the way to go.

Docker’s super lightweight containers are perfect for WordPress developers who want to run several test environments without maxing out server resources.

In the following article, we will show you how to install WordPress inside a Docker container on Windows Server 2019, Ubuntu 20, Debian 11, and CentOS Stream.

Installing WordPress on Docker: Ubuntu 20 and Debian 11

Your Docker containers will only be as good as the machine it’s running on. If you need more resources and security than your personal computer can provide, or simply need a machine that’s built to run 24/7/365, then it’s best to buy a cloud (remote) virtual server:

Check out our cloud-based Linux virtual machines, then follow the steps below to get Docker and WordPress installed.

Step 1. Install Docker

To run a containerized WordPress instance on Ubuntu 20 or Debian 11, we need to first install Docker. Follow these steps:

Update your package list:

sudo apt-get update

Now we need to install some packages to allow apt to access repositories over HTTPS:

sudo apt-get install \
ca-certificates \
curl \
gnupg \
lsb-release

For Ubuntu add the following GPG key:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg 

For Debian use this key:

curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

On Ubuntu finalize setting up the Docker repository using this command:

echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

For Debian use this command:

echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Update your package index, and then install the Docker engine and CLI:

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli

Step 2. Install Docker Compose

Docker compose allows us to specify the entire WordPress configuration via one .yml file. Follow these steps to install it:

Visit the official GitHub repository to find the latest stable version of Docker compose.

Then, replace the version number in the following command before executing it: (as of 3 February 2022 ,the latest version is 2.2.3)

sudo curl -L "https://github.com/docker/compose/releases/download/v2.2.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

Make the docker-compose command executable:

sudo chmod +x /usr/local/bin/docker-compose

Step 3. Set up WordPress

Create a new directory to keep the YAML file for WordPress.

mkdir ~/wordpress/
cd ~/wordpress/

Next up, create a new YAML file called docker-compose.yml and paste the following contents into it:

Note: You may choose different versions of MySQL or WordPress, based on your preferences. Also, don’t forget to set appropriate database name and credentials

version: '3.3'
services:
database:
image: mysql:5.7.37
volumes:
- database_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: myrootpassword
MYSQL_DATABASE: wp_db
MYSQL_USER: admin
MYSQL_PASSWORD: admin
wordpress:
depends_on:
- database
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: database:3306
WORDPRESS_DB_USER: admin
WORDPRESS_DB_PASSWORD: admin
WORDPRESS_DB_NAME: wp_db
volumes:
database_data: {}

Inside the same directory, execute the following command to start the database and WordPress containers:

docker-compose up -d

Visit localhost:8000 from your browser, and follow the on-screen instructions to finalize the installation.

WordPress installation GUI

Installing WordPress on Docker: CentOS Stream

CentOS 8 reached end-of-life in early 2022. In order to run yum update, and install the Docker engine, you’ll first need to upgrade it to CentOS Stream.

If you want Docker to run on a secure, super fast CentOS cloud (remote) server that’s always on, you can buy our CentOS 8 virtual machine, then upgrade it to CentOS stream with the instructions below:

Step 1. Upgrade to CentOS Stream

First off, run a simple yum update.

yum update

Now restart the server.

shutdown -r now

Install the CentOS release file.

dnf in centos-release-stream

Replace the CentOS Linux repos with the CentOS stream repos.

dnf swap centos-linux-repos centos-stream-repos

Execute the following command to complete the upgrade.

dnf distro-sync

Finally, restart your server.

shutdown -r now

You may run more /etc/centos-release to verify the upgrade:

Output

CentOS Stream release 8

Step 2. Install Docker

Follow these steps to install Docker engine:

Install the yum-utils package:

sudo yum install -y yum-utils

Set up the stable Docker repository:

sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo

Install the Docker engine and CLI:

sudo yum install docker-ce docker-ce-cli

Finally, start the Docker service:

sudo systemctl start docker

Step 3. Install Docker Compose

We will use Docker compose to specify the configuration for the WordPress and MySQL containers. Follow these steps to install Docker compose:

Find the latest stable version of Docker compose from the official GitHub repository.

Then, replace the version number in the following command before executing it: (as of 3 February 2022, the latest version is 2.2.3)

sudo curl -L "https://github.com/docker/compose/releases/download/v2.2.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

Make the docker-compose command executable:

sudo chmod +x /usr/local/bin/docker-compose

Step 4. Set up WordPress

Create a new directory to store the YAML file for WordPress.

mkdir ~/wordpress/
cd ~/wordpress/

Next up, create a new YAML file called docker-compose.yml and insert the following:

Note: You may choose different versions of MySQL or WordPress, based on your preferences. Also, don’t forget to set appropriate DB name and credentials)

version: '3.3'
services:
database:
image: mysql:5.7.37
volumes:
- database_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: myrootpassword
MYSQL_DATABASE: wp_db
MYSQL_USER: admin
MYSQL_PASSWORD: admin
wordpress:
depends_on:
- database
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: database:3306
WORDPRESS_DB_USER: admin
WORDPRESS_DB_PASSWORD: admin
WORDPRESS_DB_NAME: wp_db
volumes:
database_data: {}

Inside the same directory, execute the following command to start the database and WordPress containers:

docker-compose up -d

Visit localhost:8000 from your browser, and follow the on-screen instructions to finalize the installation.

WordPress installation GUI

Installing WordPress on Docker: Windows Server 2019

Your Docker containers will only be as good as the machine it’s running on. If you need more resources and security than your personal computer can provide, or simply need a machine that’s built to run 24/7/365, then it’s best to buy a cloud (remote) virtual server:

Check out Windows cloud servers, then follow steps below:

Step 1. Install Docker

Let’s begin by configuring and installing Docker on Windows 2019.

Run PowerShell as an administrator, and execute the following command to install the DockerMsftProvider package from the PowerShell gallery. When prompted, enter Y.

Install-Module -Name DockerMsftProvider -Repository PSGallery -Force
Powershell output for docker package command

Install the Docker client and engine using the following command. When prompted, enter Y.

Install-Package -Name docker -ProviderName DockerMsftProvider

Step 3. Install Docker Compose

Start a PowerShell session as an administrator.

Set TLS1.2 as the protocol, as GitHub requires it.

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

Run the following command to install the current latest version of Docker Compose (2.2.3).

Note: If there’s a newer version available at the official GitHub repository, feel free to replace the version number in the command:

Invoke-WebRequest "https://github.com/docker/compose/releases/download/v2.2.3/docker-compose-Windows-x86_64.exe" -UseBasicParsing -OutFile $Env:ProgramFiles\Docker\docker-compose.exe

Docker compose should now be installed on your system.

Step 4. Set up WordPress

Create a new folder to store the YAML file for WordPress.

Next up, create a new YAML file called docker-compose.yml and put the following contents into it:

Note: You may choose different versions of MySQL or WordPress, based on your preferences. Also, don’t forget to set appropriate database name and credentials.

version: '3.3'
services:
wordpress:
depends_on:
- database
image: wordpress:latest
volumes:
- ./wordpress_files:/var/www/html
ports:
- "80:80"
restart: always
environment:
WORDPRESS_DB_HOST: database:3306
WORDPRESS_DB_USER: admin
WORDPRESS_DB_PASSWORD: admin
database:
image: mysql:5.7
volumes:
- ./database_data:/var/lib/mysql
restart: always
environment:
MYSQL_DATABASE: wordpress
MYSQL_USER: admin
MYSQL_PASSWORD: admin
MYSQL_ROOT_PASSWORD: root_password
volumes:
database_data:
wordpress_files:

Open PowerShell, navigate to the WordPress folder, and run the following command to start the database and WordPress containers:

docker-compose up -d

Visit localhost:8000 from your browser, and follow the on-screen instructions to finalize the installation.

WordPress installation GUI

The Author

Maab S.

Maab is an experienced software engineer who specializes in explaining technical topics to a wider audience.

More posts from Maab