To
experiment with container services, I created a 2-container Wordpress app to
run on AWS ECS
- The first container “mysql” runs a mysql image from docker hub, and creates a database for use by WorkPress app
- The second container “wordpress” runs a WordPress image from docker hub. It has a link to “mysql” container since the app needs to access its database. In addition, the container defines port mapping which maps its port 80 to port 80 on the host, thus making the app available.
It works
nicely. ECS is fast. I can get a WordPress app up and running in minutes,
available on the internet. However, when I stop and restart the service, all
configuration and content is lost. This is expected, since by default,
container data is not persistent.
ECS supports
data persistency by way of container volumes. In container definition, the
volume is mounted to the “containerPath” which is the content to be preserved. In
volume definition, “sourcePath” references a location on the host to store the
volume data. With volume backed by sourcePath, even if the containers are shut
down, the app data on container volume is preserved.
In the
sample 2-container WordPress app, the database container saves app database in /var/lib/mysql
to /ecs/database on the host, the app container saves app configuration in
/var/www/html to /ecs/config on the host.
Volume name
|
sourcePath (stored location on host)
|
containerPath (stored content on container)
|
vol-database
|
/ecs/database
|
/var/lib/mysql
|
vol-config
|
/ecs/config
|
/var/www/html
|
Initially
this feature didn’t work. When configured from the GUI, the sourcePath variable
was not taken, and no error was given. I posted on the forum. If it was a bug,
it has since been corrected. When containers get restarted, the content of the
blog is preserved. It is a simple and powerful technique for preserve data with
apps on ECS.
Attached JSON code for the 2-container WordPress app.
Here is the
original AWS post on container volume: https://aws.amazon.com/blogs/aws/ec2-container-service-ecs-update-access-private-docker-repos-mount-volumes-in-containers/
Attached JSON code for the 2-container WordPress app.
{
"family":
"ECS-webapp-test-persistency",
"containerDefinitions": [
{
"name": "mysql",
"image":
"seanxwang/mysqlwp:v1",
"cpu": 10,
"memory": 300,
"entryPoint": [],
"environment": [
{
"name":
"MYSQL_DATABASE",
"value":
"wordpress"
},
{
"name":
"MYSQL_PASSWORD",
"value":
"wordpresspwd"
},
{
"name":
"MYSQL_USER",
"value":
"wordpress"
},
{
"name":
"MYSQL_ROOT_PASSWORD",
"value":
"wordpressdocker"
}
],
"command": [],
"portMappings": [],
"volumesFrom": [],
"links": [],
"mountPoints": [
{
"sourceVolume":
"vol-data",
"containerPath":
"/var/lib/mysql",
"readOnly": false
}
],
"essential": true
},
{
"name": "wordpress",
"image":
"seanxwang/wordpress:v1",
"cpu": 10,
"memory": 300,
"entryPoint": [],
"environment": [],
"command": [],
"portMappings": [
{
"hostPort": 80,
"containerPort": 80,
"protocol": "tcp"
}
],
"volumesFrom": [],
"links": [
"mysql"
],
"mountPoints": [
{
"sourceVolume":
"vol-config",
"containerPath":
"/var/www/html",
"readOnly": false
}
],
"essential": true
}
],
"volumes": [
{
"name": "vol-data",
"host": {
"sourcePath":
"/ecs/database"
}
},
{
"name": "vol-config",
"host": {
"sourcePath":
"/ecs/config"
}
}
]
}
No comments:
Post a Comment