Learning center

ECS container won't start: CannotStartContainerError or exit code 127

Read the stop code and startedAt first: a container that never started has no start time, while a process that exited at once has one and an exit code. Then compare the revision's entryPoint and command with the image's own entry point and command, since both failures usually come from a program that is not where the revision says.

Two records for one mistake#

A release changes the image or the task definition, and every new task stops within seconds. The same root cause, a program that is missing from the image or not on its PATH, leaves one of two records depending on how the revision starts it:

  • The runtime cannot start the container. With exec form, such as "entryPoint": ["/app/server"], the runtime itself executes /app/server. If the file is not there, no process starts. The stopped reason begins with CannotStartContainerError, and the task has no startedAt. AWS lists this category among the stopped-task errors for tasks that failed to start. CannotStartContainerError · Stopped task error codes
  • The process starts and exits at once. With a shell, such as "entryPoint": ["sh", "-c"] and "command": ["server --port 8080"], the shell is the process that starts, and it is present. It then fails to find server and exits with status 127, the status shells return for a command they cannot find. The task has a startedAt, stops with EssentialContainerExited, and the container reports exitCode 127. Bash command search · Task fields

A nearby exit code reads differently. Status 126 means the file was found but could not be run. Docker uses the same 126 and 127 meanings for a contained command that cannot be invoked or cannot be found. POSIX command exit status · Docker exit status

Two neighbors leave records that look like these. An image built for another CPU architecture also exits at once, usually with an exec format error; see platform mismatch. A container held back by a dependsOn condition never starts either, with a different reason; see container dependencies.

Read the stop code and the start time#

Use a read-only profile and replace the uppercase placeholders.

aws ecs describe-tasks \
  --profile PROFILE --region REGION --cluster CLUSTER --tasks TASK_ARN \
  --query 'tasks[].{definition:taskDefinitionArn,code:stopCode,reason:stoppedReason,started:startedAt,stopped:stoppedAt,overrides:overrides.containerOverrides[].{name:name,command:command},containers:containers[].{name:name,image:image,digest:imageDigest,exit:exitCode,reason:reason}}' \
  --output json --no-cli-pager
stopCodestartedAtContainerReadingNext read
TaskFailedToStartEmptyreason begins CannotStartContainerError and says the executable was not foundThe runtime could not execute the configured programThe exec-form path against the image
EssentialContainerExitedSetexitCode 127A shell or wrapper started and could not find the programThe log stream, then the shell command against the image
EssentialContainerExitedSetexitCode 126The program exists and could not be runFile permissions or the image platform
EssentialContainerExitedSetAnother nonzero codeThe program ran and exited on its ownThe application's logs

overrides matters for a task started by run-task or a scheduled rule, because a container override replaces the command in the task definition. ContainerOverride

For the exit-code case, the log stream usually holds the shell's own message, because the shell ran long enough to print it. For the CannotStartContainerError case there is no application output at all.

Compare the revision with the image#

ECS passes the revision's entryPoint as the container's entry point and command as its command, the same as docker run --entrypoint and the trailing command. A field the revision leaves empty keeps the image's value. ContainerDefinition

aws ecs describe-task-definition \
  --profile PROFILE --region REGION --task-definition TASK_DEFINITION_ARN \
  --query 'taskDefinition.containerDefinitions[].{name:name,image:image,entryPoint:entryPoint,command:command,workingDirectory:workingDirectory,user:user}' \
  --output json --no-cli-pager

Then read the image's own settings. These commands sign Docker in to ECR and download the image to your machine; pull by the digest the task reported so you inspect the image that failed. Registry authentication · docker image inspect

docker pull IMAGE_ACCOUNT.dkr.ecr.IMAGE_REGION.amazonaws.com/REPOSITORY@sha256:DIGEST
docker image inspect \
  --format 'entrypoint={{json .Config.Entrypoint}} cmd={{json .Config.Cmd}} workdir={{json .Config.WorkingDir}} path={{json .Config.Env}}' \
  IMAGE_ACCOUNT.dkr.ecr.IMAGE_REGION.amazonaws.com/REPOSITORY@sha256:DIGEST

.Config.Env includes the image's PATH. With an exec-form entry point, the program the container runs is the entry point followed by the command as its arguments. To check that the file exists in the image, list it without running the image's own entry point. Choosing between CMD and ENTRYPOINT

docker run --rm --entrypoint ls \
  IMAGE_ACCOUNT.dkr.ecr.IMAGE_REGION.amazonaws.com/REPOSITORY@sha256:DIGEST -l /app/server

This needs ls in the image. A minimal image without a shell or ls, such as one built from scratch, cannot be listed this way; read the build's final stage instead.

What each comparison means#

FindingReadingChange
The revision's path does not exist in the imageThe image moved or renamed the program, often in a multi-stage build that no longer copies itCorrect the path in the revision, or copy the program into the final image
The program exists but is not on the image's PATH, and the revision names it without a directoryA bare name needs PATH to find itUse an absolute path
The revision sets entryPoint or command and the image changed its ownThe revision overrides a value the new image no longer supportsRemove the override and let the image decide, or update it
A relative path, and workingDirectory differs from the image'sThe path resolves from another directorySet workingDirectory or use an absolute path
The file exists and runs locallyNot a missing commandCheck the exit code against the application's logs and the image platform

An illustrative release, two outcomes#

These records are fictional. orders:58 changed the base image of orders to a slimmer one. The new image installs the server at /usr/local/bin/orders-server instead of /app/server.

  • Outcome A. Revision 58 still sets "entryPoint": ["/app/server"]. Tasks stop with TaskFailedToStart, no startedAt, and a CannotStartContainerError that says the executable was not found. docker image inspect shows the image's entry point is ["/usr/local/bin/orders-server"]. Remove entryPoint from the revision.
  • Outcome B. Revision 58 sets "entryPoint": ["sh", "-c"] and "command": ["server --port 8080"]. Tasks start, stop with EssentialContainerExited, and report exit code 127; the log stream shows the shell saying server was not found. The new image has no program named server anywhere on its PATH. Change the command to /usr/local/bin/orders-server --port 8080.

Both outcomes come from the same image change. Outcome B is easy to mistake for a crashing application because the task did start; the exit code is what separates them.

Confirm on the replacement#

A replacement task should reach RUNNING with a startedAt and stay up past the seconds in which the old ones stopped. If the container has a restart policy, ECS can restart an exiting container without replacing the task, so also check that the container is not restarting in place. Container restart policy

Where this check misleads#

Exit code 127 is a convention: a wrapper script can exit with 127 for its own reasons, and the POSIX command and nohup utilities also use it for their own errors. Read the log line before you act on the number. The CannotStartContainerError category also covers other start failures, such as the container exceeding its memory limit at start, so read the rest of the reason. The stopped reason is free text that AWS rewords; the signature table sorts on stop codes for that reason.

Questions#

What does exit code 127 mean for an ECS container?

The container's process started and reported that it could not find a command, which is how shells report a missing program. Compare the command the revision runs with the files and PATH in the image.

Why does a task with CannotStartContainerError have no logs?

The runtime could not start the container's process, so nothing ran to write output. The stopped reason and the image's configuration are the evidence.

Should I set entryPoint in the task definition?

Only when you mean to override the image. A revision that sets entryPoint keeps it when the image changes, so an image that moves its program breaks the revision without any task definition change.

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