Restart individual containers in Amazon ECS tasks with container restart policies - Amazon Elastic Container Service
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Restart individual containers in Amazon ECS tasks with container restart policies

You can enable a restart policy for each essential and non-essential container defined in your task definition, to overcome transient failures faster and maintain task availability. When you enable a restart policy for a container, Amazon ECS can restart the container if it exits, without needing to replace the task.

Restart policies are not enabled for containers by default. When you enable a restart policy for a container, you can specify exit codes that the container will not be restarted on. These can be exit codes that indicate success, like exit code 0, that don't require a restart. You can also specify how long a container must run succesfully before a restart can be attempted. For more information about these parameters, see Restart policy. For an example task definition that specifies these values, see Specifying a container restart policy in an Amazon ECS task definition.

You can use the Amazon ECS task metadata endpoint or CloudWatch Container Insights to monitor the number of times a container has restarted. For more information about the task metadata endpoint, see Amazon ECS task metadata endpoint version 4 and Amazon ECS task metadata endpoint version 4 for tasks on Fargate. For more information about Container Insights metrics for Amazon ECS, see Amazon ECS Container Insights metrics in the Amazon CloudWatch User Guide.

Container restart policies are supported by tasks hosted on Fargate, Amazon EC2 instances, and external instances using Amazon ECS Anywhere.

Considerations

Consider the following before enabling a restart policy for your container:

  • For tasks hosted on Amazon EC2 instances, this feature requires version 1.86.0 or later of the container agent. However, we recommend using the latest container agent version. For information about how to check your agent version and update to the latest version, see Updating the Amazon ECS container agent.

  • For tasks hosted on Fargate, this feature requires platform version 1.4.0 or later. For information, see Fargate platform versions for Amazon ECS.

  • If you're using the EC2 launch type with the bridge network mode, you can't specify a container restart policy for the FireLens log router container that has the firelensConfiguration object in its container definition.

  • If a container with a restart policy exits when the Amazon ECS agent is disconnected on a container instance, the container won't be restarted.