Deploy Wordpress with Docker Compose on Amazon ECS

In this post we’ll use a technique I showed in a previous post: use the Amazon ECS CLI with Docker Compose. And we’ll use it to deploy a Wordpress web application on Amazon ECS.

Let’s create a wordpress-container.yml file:

version: '2'
services:
wordpress:
image: wordpress
cpu_shares: 100
mem_limit: 524288000
ports:
- "80:80"
links:
- mysql
mysql:
image: mysql
cpu_shares: 100
mem_limit: 524288000
environment:
MYSQL_ROOT_PASSWORD: password

We can now deploy our config to our cluster with the ecs-cli compose up command.

ecs-cli compose --file wordpress-docker-compose.yml --project-name wordpress_on_ecs up

By default, the command ecs-cli compose up looks for a file called with the standard name docker-compose.yml in the current directory, but we can specify a different file with the — file option. By default, the resources created by this command have the current directory as the title, but we can override that behavior with the — project-name option. In my example I used a compose file named wordpress-docker.compose.yml and a project named wordpress_on_ecs.

Running the ecs-cli ps command we can check the containers and also retrieve the wordpress IP address:

Name State Ports TaskDefinition
5ec4516c-9c7d-45d8–9305–4645556a9d1d/wordpressRUNNING 54.194.37.13:80->80/tcp ecscompose-wordpress_on_ecs:1
5ec4516c-9c7d-45d8–9305–4645556a9d1d/mysql RUNNING ecscompose-wordpress_on_ecs:1

Navigating with our browser to the Wordpress IP, we should see the standard install page from the Wordpress CMS

Scale the Tasks on a Cluster

Now that we have our wordpress up and running we can also experiment with the scale command, in order to increase the count of our application. For example, we could use:

$ ecs-cli compose --file wordpres-docker-compose.yml scale 2

to increase the count to two. Check with ecs-cli ps to see the new status:

Name State Ports TaskDefinition
5ec4516c-9c7d-45d8–9305–4645556a9d1d/wordpressRUNNING 54.194.37.13:80->80/tcp ecscompose-wordpress_on_ecs:1
5ec4516c-9c7d-45d8–9305–4645556a9d1d/mysql RUNNING ecscompose-wordpress_on_ecs:1
afd03a36–41c1–4b8d-9c52-a66efb172b3c/wordpressRUNNING 54.154.216.119:80->80/tcpecscompose-ecs-cli:2
afd03a36–41c1–4b8d-9c52-a66efb172b3c/mysql RUNNING ecscompose-ecs-cli:2

Use ECS Service to replace failed tasks

The ECS service scheduler ensures that the specified number of tasks are constantly running and reschedules tasks when a task fails (for example, if the underlying container instance fails for some reason).

Before starting a service, let’s stop the containers from the compose file with the ecs-cli compose down command so that we’ll have an empty cluster to work with.

ecs-cli compose --file wordpress-docker-compose.yml down

Now, we can create a service in a very easy way:

$ ecs-cli compose --file wordpress-docker-compose.yml --project-name wordpress_on_ecs service up

Again, you can check the status of our containers with ecs-cli ps , retrieve the wordpress IP and use it in your browser.

Show your support

Clapping shows how much you appreciated Stefano Sandrini’s story.