Search code examples
amazon-web-servicesdockeramazon-ecsaws-fargate

Why does number of layers in Dockerfile affects the launching time of AWS ECS Task container?


This is a general question regarding some basic concepts so please excuse me that there is no specific code to show.

I am using AWS ECS Fargate to scale up a service. I have my Dockerfile which builds my image that process certain job.

I then upload the image to Amazon Elastic Container Registry;

When I start a new ECS Task container using Fargate launch, the image is pulled from ECR, and a new Task container is created from the image.

If I want to speed up the launching time, it is advised to reduce the number of layers in the Dockerfile.

But why? I have already made it an image, and Fargate just pulls the image and runs a container from it. Fargate is not re-building the image layer by layer, right?

Why would it matter if the image was made with 5 layers versus 15 layers, if both are similar size?

In my understanding, since the image is already made, then it is regarded as one entity.


Solution

  • The layers are fetched one by one. In fact, as the documentation states:

    When you use docker pull to pull down an image from a repository, or when you create a container from an image that does not yet exist locally, each layer is pulled down separately, and stored in Docker’s local storage area

    So since more layers mean more disk space, then it is logical that to speed up the starting of a Fargate task, one should reduce the number of layers so that the image pulling is faster.