Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

re-arranged for faster builds #411

Merged
merged 3 commits into from
Jul 7, 2023

Conversation

rlc4
Copy link
Contributor

@rlc4 rlc4 commented Feb 25, 2022

A few changes to the dockerfile to improve the cache hits on subsequent builds for most development changes.
If you are doing development without adding any new node modules or touching package.json, this will remove 2-3 minutes of waiting time for every time you rebuild the docker image.

Previous timing:

First build

❯ time docker build -f Dockerfile -t antest .
[+] Building 151.8s (13/13) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 359B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 34B 0.0s
=> [internal] load metadata for docker.io/library/node:16 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 16.27kB 0.0s
=> CACHED [1/8] FROM docker.io/library/node:16 0.0s
=> [2/8] WORKDIR /src 0.0s
=> [3/8] COPY nav-app/ /src/nav-app/ 0.1s
=> [4/8] COPY layers/*.md /src/layers/ 0.0s
=> [5/8] COPY .md /src/ 0.0s
=> [6/8] WORKDIR /src/nav-app 0.0s
=> [7/8] RUN chown -R node:node ./ 0.6s
=> [8/8] RUN npm install --unsafe-perm 138.6s
=> exporting to image 12.3s
=> => exporting layers 12.3s
=> => writing image sha256:581813171f1b146d7116bad96e9f25459a4f63277e9acbec61e79e3034c069d4 0.0s
=> => naming to docker.io/library/antest 0.0s
docker build -f Dockerfile -t antest . 0.55s user 0.37s system 0% cpu 2:32.35 total

Subsequent builds:

❯ time docker build -f Dockerfile -t antest .
[+] Building 141.9s (13/13) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 37B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 34B 0.0s
=> [internal] load metadata for docker.io/library/node:16 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 16.66kB 0.0s
=> [1/8] FROM docker.io/library/node:16 0.0s
=> CACHED [2/8] WORKDIR /src 0.0s
=> [3/8] COPY nav-app/ /src/nav-app/ 0.2s
=> [4/8] COPY layers/.md /src/layers/ 0.0s
=> [5/8] COPY *.md /src/ 0.0s
=> [6/8] WORKDIR /src/nav-app 0.0s
=> [7/8] RUN chown -R node:node ./ 0.6s
=> [8/8] RUN npm install --unsafe-perm 128.3s
=> exporting to image 12.6s
=> => exporting layers 12.5s
=> => writing image sha256:c8e704cfef0a3947107243b6fab894582c3d587df4e821299ce3500401282714 0.0s
=> => naming to docker.io/library/antest 0.0s
docker build -f Dockerfile -t antest . 0.51s user 0.37s system 0% cpu 2:22.40 total%

Timing after re-arranging the Dockerfile:

First build:

❯ time docker build -f Dockerfile.new -t antest .
[+] Building 189.4s (15/15) FINISHED
=> [internal] load build definition from Dockerfile.new 0.0s
=> => transferring dockerfile: 492B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 54B 0.0s
=> [internal] load metadata for docker.io/library/node:16 0.0s
=> [internal] load build context 0.3s
=> => transferring context: 5.90MB 0.2s
=> [ 1/10] FROM docker.io/library/node:16 0.3s
=> [ 2/10] WORKDIR /src/nav-app 0.0s
=> [ 3/10] COPY nav-app/package*.json nav-app/patch-webpack.js . 0.0s
=> [ 4/10] RUN npm install --unsafe-perm 64.5s
=> [ 5/10] RUN chown -R node:node ./ 107.9s
=> [ 6/10] COPY nav-app/ ./ 0.1s
=> [ 7/10] WORKDIR /src 0.0s
=> [ 8/10] COPY layers/.md ./layers/ 0.0s
=> [ 9/10] COPY .md ./ 0.1s
=> [10/10] WORKDIR /src/nav-app 0.0s
=> exporting to image 16.2s
=> => exporting layers 16.2s
=> => writing image sha256:57599cd9b25158cdfb27a8beefafb66324540932cc169fa1065753b89bebd558 0.0s
=> => naming to docker.io/library/antest 0.0s
docker build -f Dockerfile.new -t antest . 0.63s user 0.44s system 0% cpu 3:09.90 total

Subsequent builds:

❯ time docker build -f Dockerfile.new -t antest .
[+] Building 0.5s (15/15) FINISHED
=> [internal] load build definition from Dockerfile.new 0.0s
=> => transferring dockerfile: 41B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 34B 0.0s
=> [internal] load metadata for docker.io/library/node:16 0.0s
=> [ 1/10] FROM docker.io/library/node:16 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 16.66kB 0.0s
=> CACHED [ 2/10] WORKDIR /src/nav-app 0.0s
=> CACHED [ 3/10] COPY nav-app/package.json nav-app/patch-webpack.js . 0.0s
=> CACHED [ 4/10] RUN npm install --unsafe-perm 0.0s
=> CACHED [ 5/10] RUN chown -R node:node ./ 0.0s
=> [ 6/10] COPY nav-app/ ./ 0.1s
=> [ 7/10] WORKDIR /src 0.0s
=> [ 8/10] COPY layers/.md ./layers/ 0.0s
=> [ 9/10] COPY *.md ./ 0.0s
=> [10/10] WORKDIR /src/nav-app 0.0s
=> exporting to image 0.1s
=> => exporting layers 0.1s
=> => writing image sha256:b5bd0a4ad4c7d9bb168f98c69b0b299eb002a7089584d7b69fbe0452eea01075 0.0s
=> => naming to docker.io/library/antest 0.0s
docker build -f Dockerfile.new -t antest . 0.19s user 0.13s system 32% cpu 0.982 total%

@sergiuser1
Copy link
Contributor

@jondricek ping

@jondricek
Copy link
Contributor

@seansica would you mind looking into this? Seems possibly fine but I'm just not focusing on Navigator a lot at the moment

@seansica
Copy link
Contributor

seansica commented Mar 9, 2023

Ran some tests yesterday evening to validate your work, @sergiuser1.

Using the current Dockerfile:

  1. Using the current Dockerfile image and no cached layers (including the base node:16 image), it took 10m8s.
  2. When all layers are cached and the image is rebuilt, it took 14s.
  3. When I add a test.md file to the root directory and rebuild, layer 5 is a cache-miss, causing the npm install layer to cache-miss as well. The build took 2m42s.

Using your modified Dockerfile:

  1. With layer 1 (FROM node:16) cached and all other layers un-cached, the build took 4m7s.
  2. When I perform the same test previously mentioned (Step 3: add a test.md file to the root directory), the npm install layer did not cache-miss as it previously did. The build took 20s.

This change looks like a solid improvement and is reasonable to merge. The team will review your PR next week and, assuming no objections arise, should approve your pull request.

Dockerfile Outdated Show resolved Hide resolved
sergiuser1 added a commit to sergiuser1/attack-navigator that referenced this pull request Mar 19, 2023
The original PR is here:
mitre-attack#411
@clemiller clemiller merged commit 5f3b2cf into mitre-attack:develop Jul 7, 2023
@clemiller
Copy link
Contributor

Thank you for your contributions!

@rlc4
Copy link
Contributor Author

rlc4 commented Jul 24, 2023

Glad to have helped! Hopefully this makes development more enjoyable and efficient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants