Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I link 2 containers running in a AWS ECS task

I am new to ECS and I am trying to deploy a couple of containers in a ECS task using Fargate.

I have 1 container running that uses Angular2 and is running on nginx, the other container is the backend and is running on Springboot and uses the port 42048.

I am using the awsvpc network with Fargate and I have to do it that way.

The Angular app communicates with the backend using localhost:42048/some_url and it works fine in my local docker but in AWS the front-end doesn't find the backend. Currently I have my ports mapped with 80 for the front end and 42048 for the backend and the front-end when deployed locally was able to find the backend as localhost:42048

Any help would be appreciated. Thank you

like image 970
Linkavich14 Avatar asked Jun 11 '19 02:06

Linkavich14


Video Answer


2 Answers

linking is not allowed in AWSVPC.

You can do linking only in network mode when its set to bridge.

links

Type: string array

Required: no

The link parameter allows containers to communicate with each other without the need for port mappings. Only supported if the network mode of a task definition is set to bridge. The name:internalName construct is analogous to name:alias in Docker links. Up to 255 letters (uppercase and lowercase), numbers, hyphens, and underscores are allowed. For more information about linking Docker containers, go to https://docs.docker.com/engine/userguide/networking/default_network/dockerlinks/. This parameter maps to Links in the Create a container section of the Docker Remote API and the --link option to docker run.

Note

This parameter is not supported for Windows containers or tasks using the awsvpc network mode.

Important

Containers that are collocated on a single container instance may be able to communicate with each other without requiring links or host port mappings. Network isolation is achieved on the container instance using security groups and VPC settings.

task_definition_parameters

In network mode, you have to define two containers in the same task definition and then mentioned the name of the container in the link.

enter image description here And then Mentioned the name of backend container in frontend container.

enter image description here

like image 57
Adiii Avatar answered Nov 16 '22 03:11

Adiii


With Fargate, If you want to access your backend using localhost:42048, then you can try configuring your Frontend and Backend in the same Task definition. While deploying the task, all the containers defined in the same task definition would run in the same underlying host and we can access it using localhost. Remember that Fargate storage is ephemeral and your backend shouldn't maintain application state in the container.

...
"containerDefinitions": [
    {
      "name": "frontend",
      "image": "my-repo/angularapp",
      "cpu": 256,
      "memory": 1024,
      "essential": true,
      "portMappings": [ {
          "containerPort": 8080,
          "hostPort": 8080
       }
     ]
    },
    {
      "name": "backend",
      "image": "my-repo/springboot",
      "cpu": 256,
      "memory": 1024,
      "essential": true,
      "portMappings": [ {
          "containerPort": 42048,
          "hostPort": 42048
       }
     ]
    }
  ]
...  

But I'm afraid this approach isn't suitable for production grade.

like image 22
Haran Avatar answered Nov 16 '22 01:11

Haran