WordPress is one of the most popular content management software (CMS) due to its multitude of features and ease of use. Simplifying the installation process to a few fast commands greatly reduce the time and effort required, this is where Docker comes in. Docker is a container platform that allows simple and fast software installations on any system and OS.

Install Docker

Installing Docker itself is already easy. Firstly run the usual update command for your system to make sure you have the latest source lists.

# Debian and Ubuntu

sudo apt-get update

# CentOS

sudo yum update

Check that you have the curl command line utility.

curl -V

If it is not there, install it manually with the appropriate command for your OS.

# Debian and Ubuntu

sudo apt-get install curl

# CentOS

sudo yum install curl

Use the command below to download and install Docker as a root user.

curl -fsSL https://get.docker.com/ | sh

you can check that the installation was successful with the following test program:

docker run hello-world

You should see an output similar to the example below.

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
...
Hello from Docker.
This message shows that your installation appears to be working correctly.
...

Docker should now be installed and working correctly. Continue on below with the rest of the WordPress setup.

MariaDB in a container

Before installing WordPress with Docker you will need to have somewhere to store the data. MariaDB is a community-developed relational database management system and a drop-in replacement for MySQL.Start off by making a new directory where you wish to store the files for WordPress and MariaDB for example in your home directory.

mkdir ~/wordpress && cd ~/wordpress

Downloading and installing a new MariaDB container can all be performed with a single command. Before jumping in check the required parameters.

MariaDB Environment variables are marked in the Docker command with -e:

  •  -e MYSQL_ROOT_PASSWORD= Set your own password here.
  •  -e MYSQL_DATABASE= Creates and names a new database e.g. wordpress.

Docker parameters:

  • –name wordpressdb – Names the container.
  • -v “$PWD/database”:/var/lib/mysql – Creates a data directory linked to the container storage to ensure data persistence.
  • -d – Tells Docker to run the container in daemon.
  • mariadb:latest – Finally defines what to install and which version.

Then run the command below while replacing the <password> with your own.

docker run -e MYSQL_ROOT_PASSWORD=<password> -e MYSQL_DATABASE=wordpress --name wordpressdb -v "$PWD/database":/var/lib/mysql -d mariadb:latest
You can confirm that the MariaDB container is running by using the following command:
docker ps

Check the status for your MariaDB install, it should show “Up” and the time it has been running like in the example output below.

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
14649c5b7e9a mariadb:latest "/docker-entrypoint.s" 12 seconds ago Up 12 seconds 3306/tcp wordpressdb

WordPress with Docker

WordPress is also made officially available on Docker Hub, pull the image using the command below. When the version to download is not specified Docker will fetch the latest available.

docker pull wordpress

WordPress container also takes environment variables and Docker parameters:

  • -e WORDPRESS_DB_PASSWORD= Set the same database password here.
  • –name wordpress – Gives the container a name.
  • –link wordpressdb:mysql – Links the WordPress container with the MariaDB container so that the applications can interact.
  • -p 80:80 – Tells Docker to pass connections from your server’s HTTP port to the containers internal port 80.
  • -v “$PWD/html”:/var/www/html – Sets the WordPress files accessible from outside the container. The volume files will remain even if the container was removed.
  • -d – Makes the container run on background.
  • wordpress – Tells Docker what to install. Uses the package downloaded earlier with the docker pull wordpress -command.

Run the command below while replacing the <password> as you did for the MariaDB container.

docker run -e WORDPRESS_DB_USER=root -e WORDPRESS_DB_PASSWORD=<password> --name wordpress --link wordpressdb:mysql -p 80:80 -v "$PWD/html":/var/www/html -d wordpress

Then open your server’s domain name or IP address in a web browser to test the installation. You should be redirected to the initial WordPress setup page at http://<public IP>/wp-admin/install.php. Go through the setup wizard and you are done.

Now we have  learned how to install docker with wordpress. At Iserversupport, we offer various server support services such as server management services, one time server management, cloud infrastructure management and outsourced web hosting support.