I have deployed a PHP application on AWS ECS using bridge mode. The container is listening on port 8000, and the host port is set to 0, allowing ECS to assign a random host port. I am using an Application Load Balancer (ALB) to forward traffic to my ECS service.
However, the target group in the load balancer is showing as unhealthy, and I am getting a 502 Bad Gateway error. The health check is failing with the message: “Health checks failed with these codes: [502]”.
Here’s my current setup:
ECS Task Definition is in bridge mode with hostPort: 0.
The load balancer is set to forward traffic on port 80 (HTTP) to the ECS service.
I have allowed inbound traffic on 0-65535 in the EC2 instance security group for the load balancer’s security group.
The health check path is set to /health.
I’ve confirmed the application is listening on port 8000 inside the container.
Things I’ve checked:
The security group for the EC2 instances allows traffic on all ports from the load balancer’s security group.
I’ve increased the timeout and start period for the health check.
The application responds correctly when tested locally inside the ECS container.
Logs from the ECS container do not show any significant errors.
What might be causing the target group to stay unhealthy with 502 errors? How can I ensure that the load balancer forwards traffic to the correct host port in bridge mode?
Any insights would be appreciated!