This article is the continuation of WordPress with Docker, AWS (ECS, Code Pipeline, Load Balancer, RDS, EFS) Complete Series. For better understating, please start from the beginning of the series.
In this article, we will create an ECS task definition containing our WordPress Docker image reference from ECR, Environment variables such as Database credentials, EFS volume mount for our plugins storage (Don’t worry I will shed more light on this later) and some WordPress website constants such as WP_SITE_URL etc.
If you have used Docker Compose before, ECS Task Definition concept is quite similar to that.
Fasten your seat belts, there will be some steps involved to get the ECS Task Definition setup.
Before we began
In the task definition, we will pass some environment variables such as Database host, Database name, user, password, some WordPress constants such as website URL etc.
The first thing we need is the WEBSITE_URL. In the beginning, we can use the DNS name of our Load Balancer.
Go to your EC2 console and select Load Balancer. You will be able to see the DNS name of your Load Balancer here. Please take a note of this URL.
We can change this URL once our domain is linked to the Load Balancer. This can be easily done by using AWS route 53 later.
We also need our Database credentials. Go to RDS from service tab and select Database. Click on the database you have created. Under Connectivity & security section, you will find the URL there. We will be using this URL soon.
Create Task Definition
Go to ECS from your AWS Service tab and select Task Definitions, now click on Create New Task Definition
Step 1: Select launch type compatibility
We will choose EC2 on the next step
Step 2: Configure task and container definitions
We will add a container which have our Docker Project along with some configurations.
You can give any name to your container, in our case we have used web. In the image field, paste your ECR repository’s latest version link. Please refer to Build and Push wordpress project to AWS ECR Push to AWS ECR ! section.
The reason to add 0 -> 80 in the port mappings is to map any port from outside world (Load Balancer in our case) to container’s port 80.
This is the most important part of our container, we will pass the Database credentials to our Docker Image file along with some other configurations.
We will set the following environment variables:
- WORDPRESS_DB_HOST (RDS end point URL)
- Go to RDS from service tab and select Database. Click on the database you have created. Under Connectivity & security section, you will find the URL.
- The database name, user and password you have already set while creating the RDS DB
Replace wwordpress-elb-1695594798.ap-southeast-2.elb.amazonaws.com with your Load DNS URL
If your wordpress website requires setting up any constant, WORDPRESS_CONFIG_EXTRA is where you can append it.
Scroll down to the STORAGE AND LOGGING section and enable AWS logs. This will help to troubleshoot if there’s any issue with your website.
Click on Add and we’re almost done.
Volumes for Cache and Plugins
Now its time to use a centralized storage for plugins. This is one of the biggest challenges I have faced when it comes to develop a high traffic multi instance WordPress website which can scale up and scale down on demand.
Now its time to use our EFS file system we have created for Plugins storage and cache.
We will have to create two volumes i.e. efs-plugins and efs-cache within our container. for that, Scroll down and click on Configure Via JSON button
find “volumes”:  and add update this JSON array
Replace fs-565d626f.efs.ap-southeast-2.amazonaws.com with your EFS volume URL.
Volumes have been added now we need to mount these volumes. find “mountPoints”:  in the JSON and update it with the following.
Hit Save and then click on create. Our task definition is now created and ready to rock and roll very soon !!
3 thoughts on “Create Task Definition for WordPress Docker Project”
How does sessions work in this case. You are disabling php session using “define(‘WPINV_USE_PHP_SESSIONS’, false);“ yet some plugins heavily rely on this and you are not enabling session stickiness in your target group. This setup will get you stuck in a loop in the first login screen after the setup.
I believe Fargate now supports EFS. Is this why you used EC2 instances instead?
Thanks! Awesome write up. I followed this tutorial and ended up with this error.
Status reason: CannotStartContainerError: Error response from daemon: error while mounting volume ‘/var/lib/docker/volumes/ecs-wp-task-3-efs-cache-aebef3f2bc84ec806300/_data’: failed to mount local volume: mount :/cache:/var/lib/docker/volumes/ecs-wp-task-3-efs-cache-ae
any idea about this?