CryptoNetHub

Why composer install in Your Dockerfile Defeats Layer Caching

Member-only storyDockerfilesDockerComposerDocker ComposeContainerization18 min readJust now--Why yourcomposer installruns from scratch every build. The layer cache anti-pattern + the 40-line production. . .

By Ann R. · · 2 min read

Why composer install in Your Dockerfile Defeats Layer Caching

Member-only storyDockerfilesDockerComposerDocker ComposeContainerization18 min readJust now--Why yourcomposer installruns from scratch every build. The layer cache anti-pattern + the 40-line production Dockerfile. Press enter or click to view image in full sizePhoto byIan TayloronUnsplashA team’s Docker build takes 3 minutes for every code push. The developers complain — they’re pushing dozens of times per day, and 3 minutes each adds up to hours of waiting. The CI pipeline consumes a lot of compute budget. Someone suggests “maybe it’s a network issue” or “maybe the base image needs updating. ” The actual cause is one line in the Dockerfile:COPY . . beforeRUN composer install. Every code change invalidates the COPY layer, which invalidates the composer install layer, which re-downloads and re-installs every dependency. The 3-minute build is 5 seconds of actual work and 2 minutes 55 seconds of doing work that could have been cached. This is one of the most common Dockerfile anti-patterns in PHP projects. The pattern looks reasonable — copy the code, then install dependencies — and works correctly. It just misses out on Docker’s most valuable optimization: layer caching. Docker builds are much faster when layers can be reused between builds; ordering the Dockerfile to maximize reuse cuts build times dramatically. The fix is a specific two-step pattern (copycomposer. jsonandcomposer. lockfirst, then install, then copy the rest) that most teams don't discover until they…

Source: blog.devops.dev

More Layer 2 news