Home > Blockchain >  How to address another container in the same task definition in AWS ECS on Fargate?
How to address another container in the same task definition in AWS ECS on Fargate?

Time:10-28

I have an MQTT application which consists of a broker and multiple clients. The broker and each client run in their own container. Locally I am using Docker compose to set up my application:

services:
  broker:
    image: mqtt-broker:latest
    container_name: broker
    ports:
      - "1883:1883"
    networks:
      - engine-net
  db:
    image: database-client:latest
    container_name: vehicle-engine-db
    networks:
      - engine-net
    restart: on-failure
networks:
  engine-net:
    external: false
    name: engine-net

The application inside my clients is written in C and uses the Paho library. I use the async_client to connect to the broker. It takes two arguments, namely:

mqtt::async_client cli(server_address, client_id);

Hereby, server_address is the IP of the broker port, and the client_id is the "name" of the client that is connecting. While using the compose file, I can simply use the service name given in the file to address the other containers in the network (here "broker:1883" does the trick). My containers work, and now I want to deploy to AWS Fargate.

In the task definition, I add my containers and give a name to them (the same names like the services in the Docker compose file. However, the client does not seem to be able to connect to the broker, as the deployment fails. I am quite sure that it cannot connect because it cannot resolve the broker IP.

AWS Fargate uses network mode awsvpc which - to my understanding - puts all containers of a task into the same VPC subnet. Therefore, automatic name resolution like in Docker compose would make sense to me.

Has anybody encountered the same problem? How can I resolve it?

CodePudding user response:

Per the documentation, containers in the same Fargate task can address each other on 127.0.0.1 at the container's respective ports.

  • Related