AWS: Communication between multiple containers on single EC2 instance


Typically your Service is just not a single Software, that’s straightforward to dockerize and deploy. However what in case your Service has a number of docker containers and one in every of them consumes information from one other? To realize this it’s essential to create some kind of communication channel between the containers. It will possibly simply be achieved by technique, once you create an EC2 occasion per Software, however this can improve your prices and clearly such a method is just not cost-effective. You then wish to get probably the most out of the EC2 occasion and run each of the dockerized Functions on a single EC2 occasion.

This can be a step-by-step information on how one can create AWS ECS job definition, consisting of two docker containers with Functions and with the flexibility to speak between them on a single EC2 occasion.

We’re going to use ECS service with a purpose to handle docker containers on EC2 occasion.

Create ECS Cluster & ECS service

Let’s begin from primary stack from official AWS-ECS example with little modifications.

declare const vpc: ec2.Vpc;

// Create an ECS cluster
const cluster = new ecs.Cluster(this, 'Cluster', {

// Add capability to it
cluster.addCapacity('DefaultAutoScalingGroupCapacity', {
  instanceType: new ec2.InstanceType("t2.micro"),
  desiredCapacity: 1,

// Place for including job definition,
// docker container choices,
// and all different choices from this text beneath

// Instantiate an Amazon ECS Service
const ecsService = new ecs.Ec2Service(this, 'Service', {
Enter fullscreen mode

Exit fullscreen mode

Add Process definition

The principle factor right here is to create job definition with Bridge Community Mode. This mode makes doable the communication between docker containers linked to the identical Docker’s inside community, or Bridge.

const taskDefinition = new ecs.Ec2TaskDefinition(this, 'TaskDef', {
  networkMode: Community.BRIDGE,
Enter fullscreen mode

Exit fullscreen mode

Add ProducerApp container

Now let’s add producer container. Producer Software might be a easy HTTP server on port 8080 with /information endpoint the place shoppers can get information.

const producerContainer = taskDefinition.addContainer('ProducerContainer', {
  picture: ecs.ContainerImage.fromRegistry('producer-image'),
  memoryLimitMiB: 256,
   containerPort: 80,
   hostPort: 8080,
   protocol: ecs.Protocol.TCP,
Enter fullscreen mode

Exit fullscreen mode

Add Client container

Additionally let’s add client container. Client Software might be an HTTP Server on port 8081 with the flexibility to behave like a HTTP shopper, which is able to retrieve information from Producer Software each minute.

However how the Client Software will know how one can attain out the Producer Software? What endpoint ought to Client App ping? We’ll make clear this in a second.

const consumerContainer = taskDefinition.addContainer('ConsumerContainer', {
  picture: ecs.ContainerImage.fromRegistry('consumer-image'),
  memoryLimitMiB: 256,
   containerPort: 81,
   hostPort: 8081,
   protocol: ecs.Protocol.TCP,
Enter fullscreen mode

Exit fullscreen mode

Add communication between containers

And now the ultimate trick: we have to create a manner of communication between ProducerContainer and ConsumerContainer. We’ll use addLink methodology for it. Benefit of that methodology, that we don’t want to fret about mapping ports or no matter. Internally this methodology provides an alias to /and so forth/hosts which permits containers to speak with one another simply utilizing an alias and as you bear in mind each of the containers are in the identical Bridge Community, so it makes containers reachable.

Enter fullscreen mode

Exit fullscreen mode

That is it from CDK viewpoint. However we nonetheless lacking Producer’s endpoint, from the place Client Software ought to retrieve information.

Diving deeper to search out Producer endpoint

After profitable deployment of CDK, let’s learn the way Producer endpoint appears to be like like.
Hook up with EC2 occasion through SSH or session supervisor.

~$ sudo docker ps -a
CONTAINER ID   IMAGE                  COMMAND                  CREATED       STATUS     PORTS     NAMES
d6a008b35bb2   producerContainer      "/usr/src/app/produc…"   10 min in the past   Up 10 min             producerContainer
a85bbfa3fae3   consumerContainer      "/usr/src/app/consum…"   10 min in the past   Up 10 min             consumerContainer
Enter fullscreen mode

Exit fullscreen mode

To begin with we are able to clearly see that each docker containers are up and working. Let’s dive deep into the consumerContainer.

~$ sudo docker exec -it a85bbfa3fae3 /bin/sh
~$ cat /and so forth/hosts   localhost
::1         localhost ip6-localhost ip6-loopback  producerContainer d6a008b35bb2  a85bbfa3fae3
Enter fullscreen mode

Exit fullscreen mode

As you may even see, now we all know an alias for the producerContainer: producerContainer d6a008b35bb2.

Let’s get some information from it!

~$ curl producerContainer:8080/information
~$ {information: "Essential information"}
Enter fullscreen mode

Exit fullscreen mode

It labored like a attraction! Now you simply want so as to add producerContainer:8080/information as goal endpoint for HTTP shopper of Client Software and retrieve the information from Producer Software!

As an extra thought, when you retrieve information extra typically otherwise you want 2-way communication between containers and will think about to ascertain WebSocket connection between containers and produce/eat information in additional handy and sooner manner!


This trick will provide help to not simply to know extra about docker & AWS but in addition utilise extra EC2 occasion and prevent some cash! Hope you loved it!

Add a Comment

Your email address will not be published. Required fields are marked *