Create Task Definition for WordPress Docker Project
Tags: aws, docker, ecr, ecs
Published: 2020-02-07 01:12:38

---
title: Create Task Definition for WordPress Docker Project
published: true
date: 2019-12-27 12:44:21 UTC
tags: AWS,Docker,ECR,ECS
canonical_url: 
---

> **This article is the continuation of [WordPress with Docker, AWS (ECS, Code Pipeline, Load Balancer, RDS, EFS) Complete Series](https://dev.to/saluminati/high-traffic-wordpress-website-with-docker-aws-ecs-code-pipeline-load-balancer-rds-efs-complete-series-43id/). 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.

[![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/27132803/008-1-1024x447.png)](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/27132803/008-1.png)

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**

![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/27125238/001-6-1024x286.png)

### Step 1: Select launch type compatibility 

We will choose EC2 on the next step

[![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/27125404/002-6-1024x498.png)](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/27125404/002-6.png)

### Step 2: Configure task and container definitions                 

[![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/27125721/003-6-1024x873.png)](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/27125721/003-6.png)

[![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/27125806/004-6-1024x384.png)](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/27125806/004-6.png)

We will add a container which have our Docker Project along with some configurations.

![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/27125904/005-4-1024x219.png)

[![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/27130401/006-5-1024x648.png)](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/27130401/006-5.png)

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](https://dev.to/saluminati/build-and-push-wordpress-project-to-aws-ecr-2h7c) **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.

### Environment

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.
- WORDPRESS\_DB\_NAME 
- WORDPRESS\_DB\_USER
- WORDPRESS\_DB\_PASSWORD
  - The database name, user and password you have already set while creating the RDS DB

WORDPRESS\_CONFIG\_EXTRA 

```shell
define( 'WP_HOME', 'http://wwordpress-elb-1695594798.ap-southeast-2.elb.amazonaws.com
' ); define( 'WP_SITEURL', 'http://wwordpress-elb-1695594798.ap-southeast-2.elb.amazonaws.com
' ); define('CONCATENATE_SCRIPTS', false); define('WPINV_USE_PHP_SESSIONS', false);define('FS_METHOD', 'direct');
```

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.

[![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/27133408/009-1-1024x770.png)](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/27133408/009-1.png)

### AWS Logs

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.

[![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/27133643/010-1-1024x685.png)](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/27133643/010-1.png)

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

```yaml
 "volumes": [
        {
            "name": "efs-plugins",
            "host": null,
            "dockerVolumeConfiguration": {
                "autoprovision": null,
                "labels": null,
                "scope": "task",
                "driver": "local",
                "driverOpts": {
                    "type": "nfs",
                    "device": ":/",
                    "o": "addr=fs-565d626f.efs.ap-southeast-2.amazonaws.com,nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport"
                }
            }
        },
        {
            "name": "efs-cache",
            "host": null,
            "dockerVolumeConfiguration": {
                "autoprovision": null,
                "labels": null,
                "scope": "task",
                "driver": "local",
                "driverOpts": {
                    "type": "nfs",
                    "device": ":/cache",
                    "o": "addr=fs-565d626f.efs.ap-southeast-2.amazonaws.com,nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport"
                }
            }
        }
    ],
```

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.

```json
 "mountPoints": [
                {
                    "readOnly": false,
                    "containerPath": "/var/www/html/wp-content/plugins",
                    "sourceVolume": "efs-plugins"
                },
                {
                    "readOnly": false,
                    "containerPath": "/var/www/html/wp-content/cache",
                    "sourceVolume": "efs-cache"
                }
            ],
```

Hit Save and then click on create. Our task definition is now created and ready to rock and roll very soon !!

![](https://s3-ap-southeast-2.amazonaws.com/sal-blog/wp-content/uploads/2019/12/27134925/yass.gif)

#### Previous

**[Create Load Balancer for WordPress website](https://dev.to/saluminati/create-load-balancer-for-wordpress-website-4f8m)**

#### Next

**[Putting all things together using AWS Task Definition](https://dev.to/saluminati/putting-all-things-together-using-aws-task-definition-4n7o/)**