Spencer Pollock | Blog

Wordpress in a Docker Container on an AWS EC2 Instance

February 05, 2020

The example site that was created with these settings resides at: http://example.spollock.xyz

Lately, I've been messing around with Docker. I've wanted to work on setting up a Jenkins server (and it's coming) with a Docker Swarm on a single machine. The goal is to have the single machine (also the master node) running the Jenkins server and running the slaves. All behind a reverse proxy. I'm at the stages now where Jenkins pipelines run and builds are done, but I haven't got the swarm configured and working with Jenkins. Meanwhile, while I'm testing that, I've had a few interviews and calls with recruiters, so trying to remain busy and able to show what I've been actively learning, I set out to build a Wordpress server.

As I said, since I'm learning Docker and running more AWS EC2 instances to run them on, I want this to be similar to that. As I built this yesterday, I'm working out issues here and there, but as of now 48 hours after running the first, I have had no issues as of yet. Keep in mind, I'm only running 3 docker containers on the one host, and still learning more about the setup as I go so I will come back to modify this. My version templates also continue to grow, but I digress.

In this post, I will show you how I set about building my setup, and how you can do it yourself faster. You will be able to run this in the AWS free tier, as my server is also running on a t2.micro instance. I assume you have some knowledge in AWS console, but I'm also going to go through step by step to the best of my abilities.

1. Creating the first instance.

Head into AWS console and straight into the EC2 Dashboard. Go ahead and launch a new instance. With that instance, setup the following: 1. Ubuntu server 16.04 LTS AMI 2. t2.micro (1 core, 1G RAM) 3. (Keep everything that is the default) 4. 20GB SSD memory 5. Tags (for personal referencing) 6. Security group with ports (22, 80, 443) open to all. Name it "Wordpress"

Review and Launch. Be sure to set up a new .PEM key (again, "Wordpress") or you won't be able to access the instance and future instances.

2. First Machine Setup

Once the machine is set up and running, SSH into the server, run the following to install Docker and necessary tools for the generation and running.

sudo apt-get install docker docker-compose git -y

You can add whatever other tools you think the machine may need, but let's try to keep things small.

Then clone my GitHub repo with the docker-compose.yml file you need: https://github.com/srepollock/docker-wordpress-generator. Put this either in the user directory (/home/ubuntu/) or a directory of your choice (/home/ubuntu/wp). Important: change the VIRTUAL_HOST for both the Wordpress site and database before continuing. Set it to the domain you would like to use for the site or the IP address of the EC2 instance. Now that we have the machine with the required installs (docker, docker-compose) we can begin running the Wordpress site.

docker-compose -p wordpress up -d

Now get the IP address of the EC2 instance, and enter that in a browser on your local dev machine (not EC2).

VOILÁ!

A new Wordpress site at your disposal! Very cool, and congratulations. Don't sign up quite yet for an admin account though! What if you want to make multiple sites without going through all those steps again. What if it were just a few clicks and BAM spin up more machines? If that's the case, then we can save everything we have right now (without an admin, as we can set up a new one for each instance) as a machine state in AWS Console.

3. From One to Many

Head back to EC2 Dashboard. Select your server, right-click > Image > Create Image. This will now allow us to save the state of the machine or all that we've done thus far. Name and describe the image as you see fit ("DockerWordpressImage", "A Wordpress instance behind a reverse proxy, all running in Docker containers") and save it.

Then, head to launch templates. We can set up a template that creates a new machine with the image we just made as it's AMI to just build the machine and run it for us! Create a new launch template; naming, labeling AND using the same settings as before but now with the AMI set to your saved image (it will be at the very bottom of the list, or you can search by your image name).

4. Finalé

This time, it's really voilá! We have a cloned state with a Wordpress site initialized and ready. To check that everything is working, create a new server from the template (to save from getting charged, stop the current running Wordpress site) and SSH into that machine (same key as before). Run in the same directory as before the docker-compose command. Now we can go to that machine's IP and we have another Wordpress site!

If now you choose that you would like this to be your new website, setup an Elastic IP and allocate it to the machine running the Wordpress server of your choice. If you have a domain on another host, setup the domain to point to the Elastic IP, or continue with using AWS Route 53 to connect a domain purchased there.

Congratulations, you now have your own Wordpress server that you can expand! You can play with more variables here (larger machine, different base (centos), more than one Wordpress per machine by changing the docker-compose to add more) because the sky is the limit!

I hope you learned something, or perhaps this sparks a passion to learn more about Docker, AWS Console and/or Wordpress.


Thanks for taking the time to read this and get to know me a little bit more. Looking forward to the road ahead.

All the best - Spencer


Spencer Pollock

Written by Spencer Pollock who lives in Vancover, BC, Canada. Computer developer. Working to make things simpler. I'll tell you my story as it unfolds. For more information, check out the main site here.