Amazon AWS ECS Docker Port not binding correctly

I ran into this exact problem when using the bridge network mode. I haven't found a solution yet. However, I have used two workarounds.

Workarounds

The easiest for me was to change NetworkMode to host in my ECS Task Definition.

Alternately, you can remove the need to know or care how ports are mapped by using an Application Load Balancer.

Network Modes

bridge maps the container port to another port (which may be different) on the host via the docker-proxy. This is the mode I have had trouble with in ECS.

host allows the container to open a port directly on the host, without requiring a proxy. The downside is that instances of the same container can't run on the same host without causing port conflicts.

awsvpc is like host, except it maps to an ENI in your VPC instead of a port on the host IP.

none is what it sounds like.

Application Load Balancer

Since posting this answer, the requirements of my project have changed. I haven't had a chance to go back and test port mappings in bridge mode directly. However, I am now using an Application Load Balancer to provide access to my containers.

When using an ALB, you don't have to worry about the host port at all. Instead, your ECS Service automatically adds your container as a target to a given ALB Target Group. This document goes over details on how to do that:

https://docs.aws.amazon.com/AmazonECS/latest/developerguide/service-load-balancing.html

Not elaborating more here because it's not a direct answer to the question about port binding issues.


Interestingly, network modes for ECS was announced just 5 days after you asked your question:

Announcement: https://aws.amazon.com/about-aws/whats-new/2016/08/amazon-ec2-container-service-now-supports-networking-modes-and-memory-reservation/

Network Mode Documentation: https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_RegisterTaskDefinition.html#ECS-RegisterTaskDefinition-request-networkMode

Hopefully this answer helps a few other Googlers. Note: I'll update this answer if I figure out how to get bridge mode working right in ECS.