My image builds are slow, how can I make them faster?

When building my images in Okteto, image builds are slower than in my laptop

Fast image builds are a crucial part of the Okteto developer experience. These builds are executed within the Okteto Build service, designed for high performance by minimizing redundant image builds and maximizing the utilization of image cache and layers.

To enhance the speed of your image builds, we recommend adhering to several best practices:

  • Follow Dockerfile best practices
  • Configure CPU/Memory resources for the Okteto Build service
  • Configure faster storage for the Okteto Build service

Follow Dockerfile best practices

Dockerfiles offer various optimizations to facilitate cache reuse during builds. From a performance perspective, consider the following recommendations:

  • Utilize a .dockerignore file to exclude files that are irrelevant to the image build process. This minimizes the time required to transfer your local files to the Okteto Build service. For detailed guidance, consult the official .dockerignore file documentation.
  • Arrange Dockerfile instructions from those that change infrequently to those that change more frequently. This sequence aids in leveraging the Dockerfile build cache. Below is an example of the recommended order of instructions:
    • Install tools you need to build your application
    • Install or update library dependencies
    • Generate your application
  • Use buildkit cache mounts to persist cache folders between image builds. Leverage buildkit cache mounts. We have a tutorial explaining how to use cache mounts for webpack/nodejs. Follow this link for a list of the cache folder used by the main programming language and frameworks.

Configure CPU/Memory resources for the Okteto Build service

Okteto Self-Hosted users can configure the CPU and memory resources allocated for the Okteto Build service by modifying the buildkit section in your Okteto helm values file. For instance, the following configuration reserves 1 CPU and 4 GB of memory for the Okteto Build service, with a limit of 2 CPUs and 8GB of memory:

buildkit:
  resources:
    requests:
      cpu: 1
      memory: 4GB
    limits:
      cpu: 2
      memory: 8GB          

Note that the performance of the Okteto Build service can be significantly influenced by the type of processor it runs on. Different virtual machines (VMs) may have processors with varying capabilities. You can enhance performance by configuring the Okteto Build service to run on a dedicated node pool with a high-speed processor using the Okteto helm values section tolerations.buildPool:

tolerations:
  buildPool: build

Moreover, to further optimize performance, you can scale the Okteto Build service instances based on CPU utilization by either configuring more instances with buildkit.replicaCount or utilizing buildkit.hpa to implement a Horizontal Pod Autoscaler based on CPU utilization:

buildkit:
  replicaCount: 2
  hpa:
    enabled: true
    min: 2
    max: 4
    CPU: 50

Configure faster storage for the Okteto Build service

Okteto Self-Hosted users can configure the storage class associated with the Okteto Build service. Image builds are inherently I/O intensive, and opting for SSDs over standard disks can substantially impact build performance. You can configure the storage class of Okteto Build service by setting the buildkit.persistence.storageClass field. The fields buildkit.persistence.size and buildkit.persistence.cache configure the size of the Okteto Build service storage and cache folders:

buildkit:
  persistence:
    enabled: true
    storageClass: ssd
    size: 800Gi
    cache: 600000