I want to reduce latency when I pull images with Amazon Elastic Container Service (Amazon ECS).
Resolution
Reduce the size of images
To reduce your image pull time, reduce your image size. For information about how to reduce image size, see Building best practices on the Docker Docs website.
If you use an Amazon Linux 2023 container image, then it's a best practice to use an AL2023 minimal container image to reduce image size.
Use the nearest AWS Region endpoint
To reduce latency, use the Region endpoint nearest to where your application is running.
If you use a public registry such as Amazon Elastic Container Registry (Amazon ECR) Public or Docker Hub, then use a pull through cache. With a pull through cache, you can deploy the image to a private registry with the same endpoint as the application. For more information, see Sync an upstream registry with an Amazon ECR private registry.
If the image already exists in an Amazon ECR private registry in a different Region, then use private image replication. With private image replication, you can deploy the same image in the application Region.
Set ECS_IMAGE_PULL_BEHAVIOR to once
If you use the Amazon Elastic Compute Cloud (Amazon EC2) launch type, then modify the ECS_IMAGE_PULL_BEHAVIOR parameter to configure Amazon ECS pull behavior. To reduce your container instance storage, set ECS_IMAGE_PULL_BEHAVIOR to once. This configuration uses cached images and pulls images remotely only if the image was never pulled before or was removed by automatic cleanup. For more information about image cleanup, see Automatic Amazon ECS task and image cleanup.
In the following example, ECS_IMAGE_PULL_BEHAVIOUR is set to once. When the task with the amazonlinux:2023-minimal container is started for the first time, the Amazon ECS agent in the EC2 instance pulls the image. To check when the image pull started and ended, check the msg value, and to check the image pull time, see elapsed and elapsedMs:
level=info time=2024-11-11T17:02:09Z msg="Pulling image for container concurrently" container="al2023" image="amazonlinux:2023-minimal" task="taskid"
level=info time=2024-11-11T17:02:14Z msg="Finished pulling image for container" task="taskid" container="al2023" image="amazonlinux:2023-minimal" elapsed="4.6245648s" elapsedMs=4624
When the task with the amazonlinux:2023-minimal container is started for the second time, the Amazon ECS agent uses the cache but doesn't pull the image. To check whether Amazon ECS found and then used the cached image, check the msg value:
level=info time=2024-11-11T17:07:06Z msg="Found cached image, use it directly for container" task="taskid" container="al2023" image="amazonlinux:2023-minimal"
For more information about Amazon ECS agent configuration parameters, see Amazon ECS container agent configuration.
Related information
Cross-Region replication in Amazon ECR has landed
Optimizing performance for Amazon ECR