-
Notifications
You must be signed in to change notification settings - Fork 0
Container Cache
- Once a layer changes, then all downstream layers need to be rebuilt as well. Even if they wouldn’t build anything differently, they still need to re-run. Ex,
FROM openjdk:latest
COPY . java_context/
WORKDIR java_context
RUN ./gradlew build
EXPOSE 8080
EXPOSE 9090
CMD ["java", "-jar", "/java_context/build/libs/retailstore.jar"]-
Because a change causes a rebuild for steps that follow, try to make expensive steps appear near the beginning of the
Dockerfile. -
Keep layers small, Don’t include unnecessary files: Running a command like
COPY . /src_contextwill COPY your entire build context into the image. If you’ve gotlogs, package manager artifacts, or even previous build results in your current directory, those will also be copied over. -
create a .dockerignore file, and use that to specify which files and directories to exclude from the build context.
-
If you’re sick of re-downloading all external dependencies every time there’s a change to one of them, the cache mount can help you save time in the future.
Inside of your Dockerfile, add a mount flag, specifying which directories should be cached during the step.
https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/reference.md#mount-types
Example:
FROM gradle:7.3-jdk11 as builder
RUN --mount=type=cache,target=/var/cache/apt \
--mount=type=cache,target=/var/cache/apt \
--mount=type=secret,id=aws,target=/root/.aws/credentialsUsing the explicit cache with the --mount=? flag keeps the contents of the target=? directory preserved between builds.
When this layer needs to be rebuilt, then it’ll use the apt cache in /var/cache/apt.
- Minimize the number of layers, Use an appropriate base image
- Use multi-stage builds
- Combine commands together wherever possible.
See: