How to make AWS containers communicate

by CorayThan   Last Updated January 02, 2018 23:00 PM

I'm creating a single page application where the static frontend assets are served by an nginx docker container, and the java backend is also running in a docker container. I tied the two containers together with Docker Compose and got it working locally.

So I went to put it up in AWS using Elastic Beanstalk and found out EBS doesn't support Docker Compose. So instead I created a Dockerrun.aws.json file, which is apparently how you define a multi-container Docker setup for EBS. I got that to create my containers in EBS, and the nginx container does serve my static front end content.

Unfortunately, I can't figure out how to get the nginx container to forward requests to my backend java api container. I can't even seem to find any documentation on how to do this with EBS.

Here's my nginx configuration. Specifically I think proxy_pass http://api:5000 needs to be changed.

server {
    listen       4000;
    server_name  localhost;

    root   /usr/share/nginx/html;
    index  index.html index.htm;

    # Matches any request with a file extension. Adds cache headers.
    location ~ .*\..* {
        try_files $uri =404;
        expires 1y;
        access_log off;
        add_header Cache-Control "public";
    }

    # Matches api requests and redirects to the api server's port.
    location /api {
        proxy_pass http://api:5000;
    }

    # Any route that doesn't have a file extension. This helps react router route page urls using index.html.
    location / {
        try_files $uri $uri/ /index.html;
    }

}

This is the error nginx logs when it tries to forward the request:

2018/01/02 22:03:01 [error] 5#5: *22 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.1.70, server: localhost, request: "POST /api/auth/login HTTP/1.1", upstream: "http://172.17.0.2:5000/api/auth/login", host: "my-app-dev.us-west-2.elasticbeanstalk.com", referrer: "http://my-app-dev.us-west-2.elasticbeanstalk.com/login"

And my Dockerrun.aws.json file:

{
  "AWSEBDockerrunVersion": "2",
  "containerDefinitions": [
    {
      "name": "proxy",
      "image": "1234.dkr.ecr.us-west-2.amazonaws.com/my-app-proxy:1",
      "essential": true,
      "memory": 64,
      "portMappings": [
        {
          "hostPort": 80,
          "containerPort": 4000
        }
      ],
      "links": [
        "api"
      ]
    },
    {
      "name": "api",
      "image": "1234.dkr.ecr.us-west-2.amazonaws.com/my-app:1",
      "essential": true,
      "memory": 128
    }
  ]
}

And you probably don't need it, but here's the docker-compose.yml I'm translating into the Dockerrun.aws.json:

version: '3'

services:

  proxy:
    build:
      context: ./ui
      dockerfile: Dockerfile
    image: "1234.dkr.ecr.us-west-2.amazonaws.com/my-app-proxy:1"
    ports:
      - "80:4000"
    depends_on:
      - api
    restart: always

  api:
    image: "1234.dkr.ecr.us-west-2.amazonaws.com/my-app:1"
    restart: always


Related Questions


Multiple docker containers elastic beanstalk

Updated April 30, 2015 21:00 PM

Deploy a Docker environment on Elastic Beanstalk

Updated August 03, 2015 14:00 PM

AWS ElasticBeanstalk: container keeps restarting

Updated September 23, 2015 20:00 PM