Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Edit: My first question was "how to link containers inside task definition on AWS ECS using Fargate?" But, may be I'm wrong from the start, so I changed my question and keep the content below:

I'm trying to deploy a simple Laravel based app on AWS via ECS. My service works as expected in local using docker-compose-yml file.

But on AWS I get: "nginx: [emerg] host not found in upstream "app" in /etc/nginx/conf.d/default.conf:12" from my web container log.

Here the containers of my service: web (nginx), app (Laravel), database (MySQL) and cache (redis).

I understand that all containers of the task description share the same namespace, so there no need for linking container (we can't anyway use links attribute with Fargate).

May you help me finding the problem here? I'm blind.

Here my working local docker-compose.yml file:

version: '2'
services:

  #  The Application
  app:
    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"

  # The Web Server
  web:
    image: 696759765437.dkr.ecr.us-east-1.amazonaws.com/ali-maison/laravel-web
    ports:
      - 80:80

  # The Database
  database:
    image: mysql:5.6
    volumes:
      - dbdata:/var/lib/mysql
    environment:
      - "MYSQL_DATABASE=homestead"
      - "MYSQL_USER=homestead"
      - "MYSQL_PASSWORD=secret"
      - "MYSQL_ROOT_PASSWORD=secret"

  # redis
  cache:
    image: redis:3.0-alpine

volumes:
  dbdata:

Here my web container Dockerfile:

FROM nginx:1.10

ADD vhost.conf /etc/nginx/conf.d/default.conf
WORKDIR /var/www

And my vhost.conf:

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 app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}
like image 506
FredRoger Avatar asked Jan 18 '18 18:01

FredRoger


People also ask

What is the difference between ECS and fargate?

If you need auto-scaling or run containers in a serverless environment, then Fargate is the right choice. But, ECS is better if you need more flexibility or are on a budget. Overall, both services are excellent choices for running containers in AWS. It just comes down to your specific needs and preferences.


1 Answers

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

like image 154
poldek Avatar answered Oct 02 '22 17:10

poldek