How to Easily Setup Wordpress Using Docker

Habib Ridho
3 min readMar 25, 2020

--

Wordpress is an open-source content management system (CMS) that has been around for almost 2 decades. It enables people to setup blogs even online stores easily. However, for those developing Wordpress themes, plugins, or those who needs to setup Wordpress from scratch, it may involves a tedious process which includes installing php, mysql database, etc. In this tutorial, we will utilize Docker to ease that process and have Wordpress up and running in minutes.

Prerequisite

First of all, we need to have Docker on our machine. We can download the installer from this page that is available for desktop and server. After it is installed, check if the cli has been installed properly by running this on the terminal.

$ docker version// Output:
Client: Docker Engine - Community
Version: 19.03.8
API version: 1.40
Go version: go1.12.17
Git commit: afacb8b
...

Setup Mysql

Now that we have docker installed, we can install mysql database using the official mysql docker image.

$ docker run --name local-mysql -e MYSQL_ROOT_PASSWORD=12345 -d mysql:5.7

Let’s chop down this command so that we know what is going on here. The first segment is docker run which exactly does what it says it does: running a docker container. Next, --name local-mysql sets the container name that we are creating. You can change the name with whatever you want. -e sets environment variable for running the container and here we set MYSQL_ROOT_PASSWORD which is mandatory to run this image. -d tells docker to run the container in background. Lastly, mysql:5.7 designate that we want to run version 5.7 of mysql image. We can also use mysql that will give us the latest version of the image.

Run docker container ls to check if the container is successfully created and running. If it does, let’s add a new database for our Wordpress by executing mysql in the container.

$ docker exec -it local-mysql mysql -u root -p

The above command is straightforward, ie. execute mysql -u root -p inside the local-mysql container. The -it flag tells docker to be interactive in executing the command and allocate pseudo-tty. After that, we will be inside mysql prompt and we can run mysql to create a new database.

mysql> create database wordpress;

Setup Wordpress

Just like setting up mysql, we can use official wordpress image to have wordpress up and running.

$ docker run --name local-wordpress -p 8080:80 -d wordpress

It is pretty much the same with when we run mysql, the difference is here we have -p 8080:80 which tells docker to have port mapping. Our machine’s 8080 port will be forwarded to the container’s 80 port. Now, we can access our wordpress by accessing http://localhost:8080 on the browser and complete the setup wizard there.

setup wordpress database step

Now, we will encounter an “error establishing a database connection” if we use localhost as our Database Host. That is because our mysql database is inside a separate docker container that is not accessible from the wordpress container.

To fix it, we need to create a new docker network and attach it to both of our containers.

$ docker network create --attachable wordpress-network

The above command tells docker to create a new network named wordpress-network that can be manually attached to containers. After the network is created, we can connect it to the containers.

$ docker network connect wordpress-network local-mysql
$ docker network connect wordpress-network local-wordpress

Now, let’s go back to the wordpress wizard in our browser and set local-mysql as the Database Host. We should be able to continue the wizard and have Wordpress up and running.

This tutorial shows that docker can help us simplify a wordpress setup. We don’t need to install a standalone php or mysql database which may conflict with other projects or applications. However, since docker containers are isolated from each other, there will be additional networking setup so that they can communicate. We can also use docker-compose to have a simpler and declarative setup which is covered in this article.

--

--

Habib Ridho
Habib Ridho

Written by Habib Ridho

Tech Enthusiast. Product Engineer at GoPay.

Responses (1)