Search code examples
phpdockernginxdocker-composeamazon-ecs

ECS mount volume to nginx container


I am trying to set up ECS in order to run my php/nginx docker application.

It works locally using this docker-compose.yml file:

version: '2'
  services:

   nginx:
    image: NGINX-IMAGE
    ports:
        - 80:80
    links:
        - php
    volumes_from:
        - php
    environment:
        APP_SERVER_NAME: <ip>

   php:
    image: PHP-IMAGE
    ports:
        - 9000:9000
    volumes:
        - /var/www/html

The problem is that I can't get this working using ECS. I don't know how to create the web-data volume and let nginx grap it using volumes_from.

I am trying to create the volume using this JSON:

 volumes='[
   {
       "name": "webdata",
       "host": {
          "sourcePath": "/var/www/html"
       }
   }
 ]'

And then in my container-definitions to the php-container I add:

"mountPoints": 
 [
     {
       "sourceVolume": "webdata",
       "containerPath": "/var/www/html",
       "readOnly": false
     }
 ]

However, when I do this, it adds the content from the host's /var/www/html folder to the /var/www/html folder of the containers. My question is, how do I configure the volume to use the data from the php's /var/www/html container and let nginx access this data?


Solution

  • I managed to find a solution that suited the setup for ECS. I simply created a VOLUME in my php Dockerfile referencing /var/www/html.

    This means I longer need to reference the volume in the volumes section of the php container. And nginx will still be able to access the volume with volumes_from.

    Update

    This is my working task definition for ECS:

    task_template='[
    {
      "name": "nginx",
      "image": "NGINX_IMAGE",
      "essential": true,
      "cpu": 10,
      "memoryReservation": 1000,
      "portMappings": [
        {
          "containerPort": 80,
          "hostPort": 80
        }
      ],
      "environment" : [
          { "name" : "APP_SERVER_NAME", "value" : "%s" }
      ],
      "links": [
          "app"
      ],
      "volumesFrom": [
          { "sourceContainer": "app" }
      ]
    },
    {
      "name": "app",
      "image": "IMAGE",
      "essential": true,
      "cpu": 10,
      "memoryReservation": 1000,
      "portMappings": [
        {
          "containerPort": 9000,
          "hostPort": 9000
        }
      ]
    }
    ]'
    

    And then I added VOLUME ["/var/www/html"] to my app Dockerfile. Now nginx can access the data with the volumes_from argument in the task definition.