Configure container startup and exit order
You can use environment variables to set startup and exit priorities for containers within an ECI instance. These priorities control the order in which containers start and exit.
Background
By default, all containers in an ECI instance start and exit concurrently, with no specific order. In some scenarios, containers within an instance may have dependencies on each other. For example, one container might need to start only after another is running, or it might need to exit only after another has exited.
In an Istio service mesh, the
istio-proxycontainer must be ready before the application container starts generating traffic, and it must exit only after the application container has exited.When collecting logs, the logging container must be ready before the application container starts writing logs, and it must exit only after the application container has exited.
To support these scenarios, Elastic Container Instance lets you set startup and exit priorities for containers using environment variables. Add the appropriate environment variables to your containers to ensure they start and exit in a specific order.
Configuration
You can control the container startup and exit order with the following environment variables:
Parameter | Environment variable | Description |
Container startup priority | ECI_CONTAINER_LAUNCH_PRIORITY |
|
Container exit priority | ECI_CONTAINER_EXIT_PRIORITY |
|
When you create an ECI instance using OpenAPI, you can set the TerminationGracePeriodSeconds parameter to ensure a graceful exit. If you also configure a container exit order, the instance's total termination time may exceed the specified TerminationGracePeriodSeconds.
Examples
Configure container startup order
API
-
Create an ECI instance with a defined container startup order.
The following example shows the request parameters used to call the CreateContainerGroup operation. The instance contains two containers, c1 and c2. Container c1 has a higher startup priority than c2, and a readiness probe is configured for c1. This ensures that c2 starts only after c1 is ready.
{ "RegionId": "cn-beijing", "SecurityGroupId": "sg-2ze63v3jtm8e6syi****", "VSwitchId": "vsw-2ze94pjtfuj9vaymf****", "ContainerGroupName": "test-launch", "Container": [ { "Name": "c1", "Image": "registry.cn-shanghai.aliyuncs.com/eci_open/nginx:alpine", "EnvironmentVar": [ { "Key": "ECI_CONTAINER_LAUNCH_PRIORITY", "Value": "1000" } ], "ReadinessProbe.PeriodSeconds": 3, "ReadinessProbe.HttpGet.Path": "/", "ReadinessProbe.HttpGet.Port": 80 }, { "Name": "c2", "Image": "registry.cn-shanghai.aliyuncs.com/eci_open/nginx:alpine", "EnvironmentVar": [ { "Key": "ECI_CONTAINER_LAUNCH_PRIORITY", "Value": "0" } ], "Arg": [ "/bin/sh", "-c", "sleep 3600s" ] } ] } -
Check the instance details to confirm the startup time of each container.
Call the DescribeContainerGroups operation to query the instance details. In the response, check the
CurrentState.StartTimefield for each container in theContainersobject. The start time of c2 is later than that of c1.
Console
When you create an ECI instance in the Elastic Container Instance console, you can set environment variables in the Advanced Settings of each container, set a startup priority for the container, and set a Readiness Probe (i.e., an Readiness Probe) in the Health Check section.
Set the environment variable name to ECI_CONTAINER_LAUNCH_PRIORITY and the value to 1000. In the Health Check section, configure a readiness probe. Select HTTP Request as the check method. Set the path to /, the port to 80, and the protocol to HTTP.
After the instance is created, you can view the instance events to confirm the startup order of the containers.
Configure container exit order
API
-
Create an ECI instance with a defined container exit order.
The following example shows the request parameters used to call the CreateContainerGroup operation.
TerminationGracePeriodSecondsis set to 30 seconds. The instance contains three containers, and the exit priority is c1 > c2 > c3.{ "RegionId": "cn-beijing", "ContainerGroupName": "test-exit", "SecurityGroupId": "sg-2ze63v3jtm8e6syi****", "VSwitchId": "vsw-2ze94pjtfuj9vaymf****", "TerminationGracePeriodSeconds": 30, "Container": [ { "Name": "c1", "Image": "registry.cn-shanghai.aliyuncs.com/eci_open/nginx:alpine", "EnvironmentVar": [ { "Key": "ECI_CONTAINER_EXIT_PRIORITY", "Value": "1000" } ] }, { "Name": "c2", "Image": "registry.cn-shanghai.aliyuncs.com/eci_open/nginx:alpine", "EnvironmentVar": [ { "Key": "ECI_CONTAINER_EXIT_PRIORITY", "Value": "0" } ], "Arg": [ "/bin/sh", "-c", "sleep 3600s" ] }, { "Name": "c3", "Image": "registry.cn-shanghai.aliyuncs.com/eci_open/nginx:alpine", "EnvironmentVar": [ { "Key": "ECI_CONTAINER_EXIT_PRIORITY", "Value": "-1000" } ], "Arg": [ "/bin/sh", "-c", "sleep 3600s" ] } ] } -
Delete the instance and then observe the order of the Killing events for the containers.
NoteTo confirm the container exit order, you must query the events promptly. Events might not be available after the ECI instance is deleted.
Call the DescribeContainerGroupEvents operation to query instance events. In the
Eventsobject of the response, check the timestamp of the Killing event for each container. The events confirm that the containers exit in the order of c1, c2, and then c3.
Console
When you create an ECI instance in the Elastic Container Instance console, you can set an environment variable in the Advanced Settings section of each container to configure its exit priority.
Set the environment variable name to ECI_CONTAINER_EXIT_PRIORITY and the value to 1000.