Photo by Justin Morgan on Unsplash

Creating a WordPress Environment within Docker

Carl Hayes

--

If you’ve ever launched a wordpress site, or know someone that has, there is a level of difficulty associated with getting it up and running. However, if you’re like me– you search for easier ways to do the same tedious tasks.

Instead of traditional ways (launching a server, installing wordpress, etc.) I am going to conduct a walk-through on creating a local wordpress environment with MYSQL,Phpmyadmin and docker, more specifically leveraging a docker compose file.

Disclaimer: It would benefit you to have some prerequisite knowledge of Linux, Docker and a little WordPress, but it’s not mandatory.

What is Docker?

Docker is a container-based platform, for highly portable workloads. It’s lightweight, fast and provides cost-effective alternatives to traditional virtual machines(VM).

The difference is traditional VMs have host OS and guest OS inside, whereas docker containers host on a single physical server with a host OS, which shares amongst them. The benefit is with sharing the host OS between other containers, allows them to maintain a lightweight state and increases boot.

Virtual Machine vs Docker Architecture

Prior to getting started, I want you to be familiar with the docker lingo that’s going to be casually mentioned.

Terminology

docker run: creates a writable container layer over whatever image that’s specified.

docker pull: pulls an image or a repository from a registry

docker images: shows all top level images, their repository, tag and size.

docker ps: by default, this command only runs containers, if you want a modified version, use a sub-command.

docker network: this command manages networks.

docker-compose: by itself, it’s useless– but, when you leverage sub-commands, you can build and manage multiple services in docker containers.

If you’re interested in conducting a deep dive on docker CLI lingo, visit Docker CLI Lingo.

Lets start the Process

Before getting into the nitty-gritty, we need to visit docker hub and grab the image information for WordPress,MySQL, and Phpmyadmin.

Visit hub.docker.com and search for wordpress, MySQL, and phpmyadmin (follow screenshots below)

Docker Hub Wordpress Image
docker pull wordpress
Docker Hub MySQL Image
docker pull mysql
Docker Hub PhpMyAdmin Image
docker pull phpmyadmin

I know there is a code editor revolution going on currently, but I am extremely fond of VSCode. It’s free and has a plethora of extensions, so that’s going to be what I use for this demo.

If you’re interested in learning more about VS code and its capabilities, click here.

First, lets make a directory to host our docker file. We will create a directory with the name wordpress-docker-site.

mkdir wordpress-docker-site

Next, we need to create the docker-compose.yaml file, by using the touch command.

touch docker-compose.yaml

Here are the contents of our docker-compose.yaml file

If you’re running an Apple M1 Chip, you will receive the following error.

The work around for the time being is to run the following command from the terminal.

docker pull --platform linux/x86_64 mysql

Or you can add services for the particular linux edition under DB.

 platform: linux/x86_64
Run docker-compose up -d

Running the docker compose command above will run the created container in detached mode.

At this moment, the services are starting to run, networks and volumes are also being created.

We are going to run docker PS command to confirm our two containers are running.

docker ps

If you look closely, notice our wordpress container is mapped to port 8000. The next action is to visit localhost:8000 in our browser to verify everything was done properly.

From here, we can proceed with the basic setup of wordpress.

Selecting a Language
Creating Site — Username — Title and Password

Upon completion, we will receive confirmation that it was successfully done, then we can sign into the admin portal with our credentials and BAM!

WordPress Dashboard

We have successfully launched a wordpress site with a docker image!

From here we need to go back and configure our .yaml file to finish to include phpmyadmin. The following needs to be added.

#phpmyadmin
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- '8080:80'
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: password
networks:
- wpsite-network

Lastly, run docker compose to run the container in detached mode.

docker-compose up -d

Now, let’s visit localhost:8080 and now we should be able to visit the phpmyadmin login.

Enter “root” for the username, and “password” for the password. Then you should have access to the phpmyadmin console.

To tear our lab down, please be sure to use the following command:

docker-compose down –-volumes

If you’ve successfully reached the end of the lab, congratulations!

If you experienced difficulties while following along, I encourage you to start the lab from the beginning to ensure you didn’t miss any required steps.

The link to the code used in this lab can be found on my GitHub, by selecting the image below.

Sincerely,

Carlintheclouds

--

--

Carl Hayes

An avid techie, hip-hop music enthusiast and photo taker. If it involves a terminal, there is a high chance you will find me in it.