7
votes

I am trying to run a php + mysql application on with the AWS ECS using docker. The application stack consists of a mysql, elk, php-fpm and nginx docker container. The containers all run on the same docker machine. When the Task starts, the nginx container exits with the following error: nginx: [emerg] host not found in upstream "php:9001" in /etc/nginx/conf.d/upstream.conf:1.

Nginx Dockerfile:

FROM alpine:3.4

RUN apk add --update nginx

#Clear Cache and Temp Data
RUN rm -rf /var/cache/apk/* && rm -rf /tmp/*

ADD nginx.conf /etc/nginx/
ADD symfony.conf /etc/nginx/conf.d/

RUN echo "upstream php-upstream { server php:9001; }" > /etc/nginx/conf.d/upstream.conf

RUN adduser -D -g '' -G www-data www-data

CMD ["nginx"]

EXPOSE 80
EXPOSE 443

RUN echo "upstream php-upstream { server php:9001; }" > /etc/nginx/conf.d/upstream.conf

The php:9001 part references to the php container, which is defined in the AWS ECS Task Definition.

nginx.conf:

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
  worker_connections  2048;
  multi_accept on;
  use epoll;
}

http {
  server_tokens off;
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 15;
  types_hash_max_size 2048;
  include /etc/nginx/mime.types;
  default_type application/octet-stream;
  access_log off;
  error_log off;
  gzip on;
  gzip_disable "msie6";
  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;
  open_file_cache max=100;
}

daemon off;

symfony.conf:

server {
    server_name _;
    root /var/www/symfony/web;

    location / {
        try_files $uri @rewriteapp;
    }

    location @rewriteapp {
        rewrite ^(.*)$ /app.php/$1 last;
    }

    location ~ ^/(app|app_dev|config)\.php(/|$) {
        fastcgi_pass php-upstream;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS off;
    }

    error_log /var/log/nginx/symfony_error.log;
    access_log /var/log/nginx/symfony_access.log;
}

/etc/php7/php-fpm.conf:

[symfony]
user = nobody
group = nobody
listen = 0.0.0.0:9001
pm = dynamic
pm.max_children = 1000
pm.start_servers = 1
pm.min_spare_servers = 1
pm.max_spare_servers = 3
env[DB_1_ENV_MYSQL_DATABASE] = $DB_1_ENV_MYSQL_DATABASE
env[DB_1_ENV_MYSQL_USER] = $DB_1_ENV_MYSQL_USER
env[DB_1_ENV_MYSQL_PASSWORD] = $DB_1_ENV_MYSQL_PASSWORD
catch_workers_output = yes

I also tried to change listen to listen = /var/run/php-fpm/php-fpm.sock or listen = 127.0.0.1:9001 with the according changes to the nginx config, but this resulted in various other error messages.

2

2 Answers

7
votes

I didn´t set the links inside the AWS ECS Task Definition between the php and nginx containers.

To set the links for a container go to NETWORK SETTINGS and then enter the link inside the Links field.

1
votes

As correctly said by @OnurDemir you cannt use links when your network mode is awsvpc - "networkMode": "awsvpc",.

With this network mode you can call the containers just with their names.

task-definition.json

{
    "family": "app-backend",
    "containerDefinitions": [
        {
            "name": "nginx",
            "image": "my-url-to-the-docker-image",
            "portMappings": [
                {
                    "containerPort": 80,
                    "hostPort": 80,
                    "protocol": "tcp"
                }
            ],
            "dependsOn": [
                {
                    "containerName": "php-fpm",
                    "condition": "START"
                }
            ],
            "essential": true
        },
        {
            "name": "php-fpm",
            "image": "my-url-to-the-docker-image",
            "portMappings": [
                {
                    "containerPort": 9000
                }
            ],
            "essential": true
        }
    ],
    "executionRoleArn": "ecsTaskExecutionRole",
    "cpu": "1024",
    "memory": "2048",
    "networkMode": "awsvpc"
}

Here you can see that:

  1. The nginx container dependsOn the php-fpm container.
  2. When you want to reference the php-fpm container in, f. ex. a vhost file in nginx, you can just call it by its name php-fpm.

Example 1: fastcgi_pass php-fpm:9000;

Another approach, when using AWS Fargate is, that you can just call the loopback 127.0.0.1 or localhost. Described in some AWS blog when using awsvpc the inter-container connection is made as it if all would be on the same machine, even if they are separate containers.

Example 2: fastcgi_pass 127.0.0.1:9000;