Networking Containers for Security, Load Balancing and Discovery
This lesson explains how the way tasks connect to the network decides how they are secured, load balanced and discovered by other services.
Learning outcomes
After completing this lesson, you will be able to:
- Choose a task network mode.
- Describe service discovery and private access for tasks.
- Identify the VPC endpoints that private tasks need.
Network modes
An ECS task definition's network mode decides how its containers connect to the network. In awsvpc mode, which Fargate requires, each task receives its own elastic network interface, private IP address and security group, exactly like an instance. Security can therefore be set per task, and a load balancer registers tasks by IP address.
In bridge mode, available on EC2, containers share the host's network through Docker's virtual network, and dynamic port mapping assigns each container a different host port, so many copies of a task can run on one instance behind an ALB. In host mode, containers use the host's network directly, which is fast, but only one task can use a given port on each host. The following table summarises the modes.
| Mode | Behaviour |
|---|---|
| awsvpc (required for Fargate) | Each task gets its own ENI, private IP and security group; the ALB uses IP targets |
| bridge | Containers share the host's network through Docker; dynamic port mapping lets many tasks share an EC2 host behind an ALB |
| host | Containers use the host's network directly; one task per port per host |
With awsvpc, every task consumes an IP address, so subnets must be sized for the maximum task count.
Service discovery and private access
Containerised services must find one another as tasks start and stop with changing addresses. ECS Service Connect lets services call each other by name, and adds load balancing, retries and traffic metrics between services.
Tasks in private subnets need a path to pull images: through a NAT gateway, or more securely and cheaply through VPC endpoints for Amazon ECR and an S3 gateway endpoint, because image layers are stored in S3. In awsvpc mode, each task has its own security group, which allows tier-to-tier rules, such as permitting only the front-end service to reach the API service.
The following diagram shows the private path for image pulls.

Container networking
Private tasks that cannot pull images
ECS tasks on Fargate in private subnets with no NAT gateway fail to start because they cannot pull images from ECR.
| Endpoint | Why it is needed |
|---|---|
| ECR API interface endpoint | Authentication and image metadata |
| ECR Docker interface endpoint | Image manifest requests |
| S3 gateway endpoint | Image layers are stored in S3 |
| CloudWatch Logs interface endpoint | The awslogs driver |
With these endpoints, tasks run with no internet access at all, and each task's security group controls exactly what it may reach.
Which network mode gives each ECS task its own elastic network interface and security groups?
Show answer
The awsvpc network mode, which Fargate requires.
Exam Tips
Remember for the exam
- To give each task its own security group, use awsvpc networking.
- When private tasks cannot pull images, add ECR api and dkr interface endpoints and an S3 gateway endpoint.
- For service-to-service discovery by name, use ECS Service Connect.
- With awsvpc networking, size subnets for one IP address per task.