How to configure a Laravel based app on AWS ECS using Fargate? How to configure a Laravel based app on AWS ECS using Fargate? docker docker

How to configure a Laravel based app on AWS ECS using Fargate?


The solution is:

  1. Make sure both containers are defined in the same task eg nginx-php
  2. use localhost:port eg localhost:9000 for php-fpm

The second part was mentioned above but it was not clearly said that both containers have to be in the same task.

More information you can find here: https://github.com/aws-samples/amazon-ecs-fargate-aspnetcore/blob/master/README.md


For others that might run across this, I had the same issue connecting two container running in the same task on aws ecs fargate and found that I had to replace 'app' with 'localhost' in the vhost.conf, and it worked. (This was mentioned in some of the comments on another answer)So the vhost.conf would look like:

server {    listen 80;    index index.php index.html;    root /var/www/public;    location / {        try_files $uri /index.php?$args;    }    location ~ \.php$ {        fastcgi_split_path_info ^(.+\.php)(/.+)$;        fastcgi_pass localhost:9000;        fastcgi_index index.php;        include fastcgi_params;        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;        fastcgi_param PATH_INFO $fastcgi_path_info;    }}

I also made sure that port 9000 was mapped and exposed on the app container (might be unnecessary)

#  The Applicationapp:  image: 696759765437.dkr.ecr.us-east-1.amazonaws.com/ali-maison/tribe-migrate  volumes:    - /var/www/storage  env_file: '.env'  environment:    - "DB_HOST=database"    - "REDIS_HOST=cache"  ports:    - 9000:9000


From the vhost.conf:

fastcgi_pass app:9000;

The hostname "app" works for docker-compose because that's what you've named the service in the docker-compose.yml. docker-compose creates a new network for the containers it has to manage, and that network enables the containers to reference each other by hostname with no further configuration.

Fargate doesn't have this feature, so "app" won't resolve.

The only networking mode Fargate currently allows you to use is a special AWS mode called awsvpc. Each task running in Fargate gets its own elastic network interface (ENI), which means each task gets it's own private IP address. You can additionally configure the ENI to have a public IP address as well, just like you can with an ENI on an EC2 instance.


How to resolve your app container from the nginx container

In Fargate, drill down into your running task definition for the app server (Cluster > Tasks tab > click the container ID in the Task column for your app server). The Network section will have the private IP.

Replace the host name "app" with this ip in your conf:

fastcgi_pass <private_ip>:9000;