Wordpress with Docker and AWS

Getting Started with Docker on AWS

In this post I'll show you how to get up and running with Docker on AWS. There are many tutorials online regarding getting started with Docker so we don't want to reinvent the wheel here. However, I'll show how to get up and running with Docker on an Ec2 instance. If Docker is a foreign concept to you just keep in mind it's a portable, scalable container technology that provides operating system level virtualization. In this tutorial we will get Docker up and running with Wordpress. In doing that that we will run a Docker container of MariaDb to support database functionality. This should serve as a basic illustration of a Docker use case. Instead of installing and running MariaDB on the instance where we want to install Wordpress we can pull the Docker Image and run it as a container. We will also run a Wordpress container in the same way and link the running containers to have a full blown Wordpress site that we can gain admin access to and even start writing blog posts. The Docker containers can be run in a detached state, which is to say they magically run in the background and alleviate most of the burden of system administration. Here I'll show you the basic Docker commands to get started and help you realize the power of Docker.

Basic Docker Vocabulary

The two fundamental pieces of Docker technology are the Docker images and containers. A good analogy of Docker images is pulling code from Github. If you jump over to dockerhub you can search for Docker images that you can run on your host. There are some official images like the Wordpress one that we will use, but there are also ones for Jenkins or even Neo4j.

Using Docker on AWS

It turns out that AWS has services specifically dedicated to running containers. There is the Elastic Container Service, but to make life easy we are just going to run Docker on an EC2 instance. You can start your instance as normal but I have also made a dedicated security group for this tutorial where I have opened port 8080 to use for Wordpress. Here is a screen shot of my security group

To get started install Docker with sudo yum install docker and then start the daemon with sudo service docker start

Pulling Images and Running Containers

We are going to start by setting up the database. We can pull the Docker Image for MariaDb and then run the container. Pull the image with sudo docker pull MariaDb and start the container with

sudo docker run --name blogsql -e MYSQL_ROOT_PASSWORD=pw -d -p 8081:3306 mariadb:10.3.6

The last command warrants a little bit of explanation. Docker run is the basic command to start the container but you can see in that command we are also naming it and passing an environmental variable via -e for the database password. The -p maps the ports from the running container to the host. For any official image like MariaDb you can consult their page in hub.docker.com for configuration instructions. For example, here is the one for MariaDb. Here is a screen shot of the container starting but also notice the command sudo docker ps -a command that displays currently running containers. At this point we are only running the database container.

Similarly we can start the Wordpress container and link it to the running database. The following command will start the Wordpress container.

sudo docker run --name blogwordpress --link blogsql:mysql -e WORD_PRESS_DB_PASSWORD=pw -d -p 8080:80 wordpress:4.9.5-php5.6-apache

In this case notice the -d parameter, which runs the container in a detached state.

Wordpress is up and running now. If we point the browser to http://publicIP:8080/wp-admin we will see the setup page for wordpress.

After some basic configuration your new blog is ready to use.

Editing Docker Containers

One black box aspect of Docker is that once a container is running it's hard to know what is going on within it. To demonstrate this I am going to load a new theme on to the Wordpress installation. However, I can't do this unless I edit files in the container. The magic here is to grab the container ID from the docker ps command and run an execute command on it. Specifically running sudo docker exec -it 23d9 bash will bring us to a bash prompt of the Wordpress Docker container. From here we actually need to install vim to update files. Just run

apt-get update  
apt-get install vim  
vim .htaccess  

Appending the following lines to the end of the file will allow you to upload a proprietary Wordpress theme from the interface.

php_value post_max_size 64M  
php_value memory_limit 400M  
php_value max_execution_time 180  
php_value max_input_time 180  

Once you are done, save the file and exit the container. You can see our new theme has been applied.

Docker for the Win

I hope you got some value out of this post but of course, we have only scratched the surface of what is possible with Docker. I bet Docker can solve some problems for you too.