How to run multiple docker images on ecs ec2 based

0

I have a docker image with app listening on port 12345. I want to run it on ecs (ec2 based) with better scallability.

I`ve done:

  • prepare an architecture with: 1 vpc, 2 subnets
  • autoscaling group with launch template for ec2 machines for ecs
  • load balancer listening on one external port exposed (e.g. 54321) with target group
  • ecs cluster (with autoscaling group mentioned above), capacity provider
  • ecs task and ecs service (using ecs task, ecs cluster capacity provider and all other services mentioned above)

Now ecs can spawn multiple ec2 instances with ecs tasks on them.

But I think it cannot run same docker images on single ec2 machine (since each docker image exposes the same port in task definition).

How to achieve ability to spawn multiple same tasks on single ecs ec2 machine? I`ve followed this tutorial: https://spacelift.io/blog/terraform-ecs (with minor changes), but it seems that it is prepared to be able to spawn only one instance of the task on each ec2 machine (port 80 exposed by each image)

1 Answer
0

To run multiple instances of the same Docker image on a single EC2 instance within an ECS cluster, you need to make a few adjustments to your current setup:

  1. Dynamic Port Mapping: Instead of specifying a fixed host port in your task definition, use dynamic port mapping. This allows ECS to assign a random available port on the host for each container instance.

  2. Task Definition: In your task definition, specify only the container port (12345 in your case) without defining a host port. This tells ECS to use dynamic port mapping.

  3. Load Balancer Configuration: Configure your Application Load Balancer to use dynamic ports. The target group should be set up to use the instance ID and port combinations provided by ECS.

  4. Security Group: Ensure that the security group associated with your EC2 instances allows inbound traffic on the dynamic port range used by ECS (typically 32768-65535).

  5. Service Definition: When creating your ECS service, make sure to enable the "Enable service discovery integration" option if you need containers to communicate with each other.

By implementing these changes, ECS will be able to run multiple instances of your task on a single EC2 instance, each with its own dynamically assigned port. The load balancer will then route traffic to the correct port on each instance.

Remember to adjust your application to bind to 0.0.0.0 instead of localhost to ensure it's accessible via the dynamically assigned port.

This approach allows for better resource utilization and scalability, as you can run multiple containers on a single EC2 instance based on available resources, rather than being limited by port conflicts.
Sources
Does ECS have a limitation where only one task can run on the same EC2 instance? | AWS re:Post
Best practices for Amazon ECS container images - Amazon Elastic Container Service

profile picture
answered a month ago
profile picture
EXPERT
reviewed a month ago
profile pictureAWS
EXPERT
reviewed a month ago
  • Agree with the AI agent: Dynamic Port Mapping is the way to go in your config.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions