Muhwezi Jerald Basasa
Muhwezi Jerald Basasa

Reputation: 616

connect UWSGI container with NGINX container [Docker] after deploying containers to AWS repository

Am deploying two containers uwsgi and nginx to AWS ECS repository. am using fargate to deploy and setup the containers but am getting an error in connections and communication between the containers.

error:No host not found in upstream "flask_app" in /etc/nginx/conf.d/nginx.conf.

Docker compose yml file.

version: "3"

services:
  db:
    container_name: db
    image: postgres
    restart: always
    environment:
      POSTGRES_DB: XXXXX
      POSTGRES_USER: XXXX
      POSTGRES_PASSWORD: XXXXX
    ports:
      - "54321:5432"

  flask_app:
    container_name: flask_app
    image: XXXXXXXXXXXXXXX.dkr.ecr.us-east-2.amazonaws.com/YYYYY:flask
    build:
      context: ./
      dockerfile: ./docker/Dockerfile-flask
    volumes:
      - .:/app
    depends_on:
      - db
    ports:
      - 5000:5000
    links:
      - db


  nginx:
    container_name: nginx
    image: XXXXXXXXXXXXXXX.dkr.ecr.us-east-2.amazonaws.com/YYYYY:backend
    env_file:
      - ./docker/users.variables.env
    build:
      context: .
      dockerfile: ./docker/Dockerfile-nginx
    ports:
      - 8080:80
    depends_on:
      - flask_app
    links:
      - flask_app

Nginx (nginx.conf):
server {
    listen 80;
    server_name localhost;    
    root /usr/share/nginx/html;
    location / {
        resolver 169.254.169.253;
        include uwsgi_params;
        proxy_pass http://flask_app:5000/;
        proxy_set_header Host "localhost";
    }
}


UWSGI.ini:
[uwsgi]
protocol = http
; This is the name of our Python file
; minus the file extension
module = start
; This is the name of the variable
; in our script that will be called
callable = app
master = true
; Set uWSGI to start up 5 workers
processes = 5
; We use the port 5000 which we will
; then expose on our Dockerfile
socket = 0.0.0.0:5000
vacuum = true
die-on-term = trueS

Error 2019/08/23 12:27:13 [emerg] 1#1: host not found in upstream "flask_app" in /etc/nginx/conf.d/nginx.conf:8

Upvotes: 1

Views: 1883

Answers (1)

gustible
gustible

Reputation: 11

In your example you are relying on the container name (flask_app) that is set in Docker compose for communicating with the other container. This works fine for Docker compose in a local environment, but not in ECS using Fargate.

Deployments using Fargate use the awsvpc network mode. This allows these containers to communicate with each other over localhost provided that they belong to the same task - see the AWS documentation: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-networking.html

If you are trying to configure an nginx reverse proxy sidecar then these containers will belong to the same task. I realise the example below uses the uwsgi protocl rather than http, but it does demonstrate communication over localhost (unless you have to use http, you can easily modify your config to use uwsgi). Here is a working configuration for nginx and uwsgi as a Fargate deployment using the uwsgi protocol:

nginx.conf:

server {
    listen 80;
    server_name localhost 127.0.0.1;
    root /usr/share/nginx/html;
    location / {
        include uwsgi_params;
        uwsgi_pass localhost:5000;
    }

and uwsgi.ini like this:

[uwsgi]
module = wsgi:app
uid = www-data
gid = www-data
master = true

processes = 3

socket=localhost:5000
vacuum = true

die-on-term = true

(assuming you use port 5000 as in your example) This configuration will not work in you local environment with Docker compose and a bridge network - but if you are on Linux you can simulate it using a host network - instructions on setting up the local test environments can be found here: https://aws.amazon.com/blogs/compute/a-guide-to-locally-testing-containers-with-amazon-ecs-local-endpoints-and-docker-compose/

Hope this goes some way to resolving the issue.

Upvotes: 1

Related Questions