Learning center

ECS deploy stuck: the new task needs a host port the old task holds

Check the revision's hostPort, then each instance's reserved PORTS: a static port that old tasks hold on every instance blocks the new task. The placement event names a port rather than CPU or memory, and the fix is structural: dynamic host ports in bridge mode, or awsvpc.

The signature#

A rolling deployment on the EC2 launch type stops making progress. The service events say service (service-name) was unable to place a task because no container instance met all of its requirements. and the detail names a port, not a resource:

The closest matching container-instance is already using a port required by your task.

The same sentence about requirements can end with insufficient CPU or memory instead. Read the detail before you add instances, because the two failures have different fixes. A port conflict is not a shortage of capacity: the instance may have plenty of free CPU and memory, but the one host port the task needs is taken. Service event messages · CPU and memory shortage

Why a rolling deployment collides with itself#

With a fixed host port, such as port 80 on the host for a web server, only one container can use that port on an instance at a time, so the service needs at least one container instance per task. A host port specified by a running task stays reserved until that task stops. Service event messages · PortMapping hostPort

A rolling deployment with maximumPercent above 100 tries to start new tasks before stopping old ones, provided the cluster resources are available. If every instance already runs one old task holding the port, no instance can take a new one. With minimumHealthyPercent at its default of 100, ECS also cannot stop an old task first to free a port, so the deployment waits. Deployment configuration

The host network mode behaves the same way: with port mappings, you cannot run more than one copy of the same task on a container instance. Network mode

Read the port mapping and the reserved ports#

Use a read-only profile and replace the uppercase placeholders. First read the new revision's network mode and port mappings. A hostPort of 0 or no hostPort in bridge mode is a dynamic mapping; any other value is static.

aws ecs describe-task-definition \
  --profile PROFILE --region REGION --task-definition TASK_DEFINITION_ARN \
  --query 'taskDefinition.{network:networkMode,ports:containerDefinitions[].{container:name,mappings:portMappings[].{containerPort:containerPort,hostPort:hostPort,protocol:protocol}}}' \
  --output json --no-cli-pager

Then read each instance's reserved ports. For port resources, remainingResources lists the ports the agent reserved at registration and the ports that running task containers have reserved with host or bridge mode. Any port not listed is free. ContainerInstance fields

aws ecs describe-container-instances \
  --profile PROFILE --region REGION --cluster CLUSTER \
  --container-instances CONTAINER_INSTANCE_ARN_1 CONTAINER_INSTANCE_ARN_2 \
  --query 'containerInstances[].{instance:ec2InstanceId,status:status,running:runningTasksCount,tcp:remainingResources[?name==`PORTS`].stringSetValue|[0],udp:remainingResources[?name==`PORTS_UDP`].stringSetValue|[0]}' \
  --output json --no-cli-pager

The defaults always appear: 22 for SSH, 2375 and 2376 for Docker, and the agent's 51678 to 51680. Look for the revision's static host port in each instance's list. Finally count instances against the deployment's peak: floor(desiredCount × maximumPercent / 100) tasks may run at once. PortMapping hostPort

What the reading means#

RevisionReserved ports on instancesCauseChange
Static hostPort in bridge or host modeThat port on every ACTIVE instanceOld tasks hold the port everywhereFree capacity per task, or move off static ports
Static hostPortPort free on some instancesPlacement should succeed on those; another requirement failedRead the full event for CPU, memory or attributes
Dynamic mapping or awsvpcAnyNot a host port conflictRead the event again; with awsvpc, check ENI attachment points
Many static ports per taskClose to 100 entriesThe instance's 100 reserved host port limitUse dynamic mapping

An instance can hold up to 100 reserved ports at a time, including the defaults. Dynamically assigned ports do not count toward that limit. When the closest instance would exceed it, the event cause is too many ports registered, a separate message from a port already in use. Service event messages

An illustrative fixture#

This fixture is fictional. The service checkout uses bridge mode with containerPort 8080 and hostPort 8080. It runs three tasks of checkout:41, with maximumPercent 200 and minimumHealthyPercent 100. The deployment of checkout:42 starts at 14:00.

With three container instances, each runs one old task and lists 8080 in its reserved ports. The new deployment's first task cannot go anywhere, the port event repeats, and nothing changes until someone acts, because ECS must keep three tasks running and cannot start a fourth.

With five container instances, two have port 8080 free. ECS places two checkout:42 tasks there. Once they count as healthy, five tasks are running, so ECS can stop two old tasks and keep three. Their instances release port 8080 and the last new task places. The deployment finishes, but it needed two idle instances to do it, which is the hidden cost of a static host port.

What to change#

To unstick the current deployment, lower minimumHealthyPercent so ECS can stop an old task before starting its replacement. With three tasks, 66 gives a floor of ceil(3 × 0.66) = 2, so one old task may stop at a time. The service runs one task short during the rollout. Alternatively, add instances, one per task the deployment wants to start. Deployment configuration bounds

The structural fix removes the shared port:

  • Dynamic host ports in bridge mode. Set hostPort to 0 or omit it. The container receives a host port from the instance's ephemeral range, so several copies of the task can share an instance. A load balancer target group registers each task as the instance ID and its assigned port. Any instance security group rule that admits only the old fixed port no longer matches the assigned ports. PortMapping hostPort · Load balancing with dynamic ports
  • awsvpc mode. Each task gets its own elastic network interface, so the container port is never shared with another task on the host. The target group must use the ip target type, so the service needs a new target group. Each instance can attach a limited number of interfaces, which becomes the new per-instance limit unless ENI trunking applies. awsvpc task networking · ENI attachment points

Changing the load balancer configuration of a rolling-update service starts a new deployment that registers and deregisters tasks. Plan the change as its own deployment. Load balancer considerations

Confirm on the replacement#

After the change, a new task's networkBindings show the host port it actually received. With dynamic mapping, two tasks on one instance should have different hostPort values for the same containerPort. Check that the target group shows each instance and port pair as healthy. NetworkBinding fields

aws ecs describe-tasks \
  --profile PROFILE --region REGION --cluster CLUSTER --tasks TASK_ARN_1 TASK_ARN_2 \
  --query 'tasks[].{task:taskArn,definition:taskDefinitionArn,host:containerInstanceArn,bindings:containers[].networkBindings[].{container:containerPort,host:hostPort}}' \
  --output json --no-cli-pager

Where the reading misleads#

Reserved ports are a snapshot. If the deployment rolled back or someone stopped tasks, the port may be free now although it was held at the failure; rely on the event time and the task history. A port listed on an instance can also belong to a different service whose tasks use the same static port, which a per-service view hides. Daemon tasks and sidecar containers reserve ports too, so read every container's mappings, not just the application's.

For other placement failures, see the signature table.

Questions#

Why does my ECS deployment work with one task per instance but stall on deploy?

A static host port allows one copy of the task per instance. A rolling deployment needs room for new tasks while the old ones still hold the port, so it needs spare instances or permission to stop old tasks first.

What does hostPort 0 do in an ECS task definition?

In bridge mode, a hostPort of 0 or no hostPort makes the agent assign a free port from the instance's ephemeral range. Several tasks can then share an instance, and these ports do not count toward the 100 reserved ports per instance.

Does awsvpc network mode avoid host port conflicts?

Yes, because each task gets its own network interface and the host port equals the container port on that interface. The target group must use ip targets, and the number of interfaces an instance can attach limits how many tasks it can run.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading