From 0a2d40c57c93c19a8bcd37d312d37292a5381d10 Mon Sep 17 00:00:00 2001 From: Kevyn Bruyere Date: Wed, 29 Jul 2020 09:19:21 +0200 Subject: [PATCH] Refining after PR comments --- sections/docker/Caching-for-shorter-build-time.md | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/sections/docker/Caching-for-shorter-build-time.md b/sections/docker/Caching-for-shorter-build-time.md index c2f5dd40..8376966f 100644 --- a/sections/docker/Caching-for-shorter-build-time.md +++ b/sections/docker/Caching-for-shorter-build-time.md @@ -2,11 +2,7 @@ ## One paragraph explainer -Docker images are a combination of layers, each instruction in your Dockerfile creates a layer. The docker daemon can reuse those layers between builds if the instructions are identicals or in the case of a `COPY` or `ADD` files used are identical. -It is important to layout your Dockerfile correctly to reduce the number of moving parts in your build, the less updated instructions should be at the top and the ones constantly changing (like app code) should be at the bottom. -Rebuilding a whole docker image from cache can be nearly instantaneous if done correctly. - -⚠️ If the cache can't be used for a particular layer, all the subsquent layers will be invalidated too. That's why order is important. +Docker images are a combination of layers, each instruction in your Dockerfile creates a layer. The docker daemon can reuse those layers between builds if the instructions are identicals or in the case of a `COPY` or `ADD` files used are identical. ⚠️ If the cache can't be used for a particular layer, all the subsquent layers will be invalidated too. That's why order is important. It is crucial to layout your Dockerfile correctly to reduce the number of moving parts in your build, the less updated instructions should be at the top and the ones constantly changing (like app code) should be at the bottom. It's also important to think that instructions that trigger long operation should be close to the top to ensure they happen only when really necessary (unless it changes everytime you build your docker image). Rebuilding a whole docker image from cache can be nearly instantaneous if done correctly. ![Docker layers](/assests/images/docker_layers_schema.png) @@ -36,8 +32,8 @@ The docker ignore avoids copying files that could bust our cache logic, like tes #### Install "system" packages first -If you need to install packages using `apt`,`yum`,`apk` or the likes, this should be one of the first instructions. You don't want to reinstall make,gcc or g++ every time you build your node app. -Do not install package only for convenience. This is a production app, no need to get +It is recommended to create a base docker image that has all the system packages you use. If you **really** need to install packages using `apt`,`yum`,`apk` or the likes, this should be one of the first instructions. You don't want to reinstall make,gcc or g++ every time you build your node app. +**Do not install package only for convenience, this is a production app.** #### First, only ADD your package.json and your lockfile @@ -46,7 +42,6 @@ COPY "package.json" "package-lock.json" "./" RUN npm ci ``` - The lockfile and the package.json change less often. Copying them first will keep the `npm install` step in the cache, this saves precious time. ### Then copy your files and run build step (if needed)