Overview
Under the hood, we use buildkit (or docker buildx) to build docker images. This allows us to take advantage of advanced caching mechanisms to improve build times and reduce resource consumption. In this guide, we’ll provide some guidelines for creating cache-efficient Dockerfiles.NoteEnsure you have Docker Buildx and BuildKit enabled in your Docker environment
if you want to test your containers locally. Otherwise, you don’t need to
worry about it. fal platform takes care of it for you when you deploy your
application using container support.
General Guidelines
NotePlease also refer to the Dockerfile best
practices for detailed
information on Dockerfile best practices.
1. Minimize Layers
EachRUN
, COPY
, or ADD
instruction creates a new layer. Minimize the number of layers by combining commands.
Bad Example:
2. Leverage Layer Caching
Order instructions from least to most frequently changing to maximize layer caching. Example:3. Use --mount=type=cache
Utilize BuildKit’s --mount=type=cache
to cache directories across builds.
Example:
4. Multi-Stage Builds
Use multi-stage builds to reduce the final image size by copying only the necessary artifacts from intermediate stages. Example:5. Clean Up After Installations
Remove unnecessary files and caches after installing packages to keep the image size small. Example:6. Use .dockerignore
Specify files and directories to ignore during the build process to avoid unnecessary files in the build context.
CautionAs of now, fal does not support
.dockerignore
files. Since we don’t allow using COPY
and ADD
from the host filesystem, you can ignore this step. However, we plan to add support for this in the near future. Stay tuned!See below for more information.