Super Slow Docker Build
When docker-compose build suddenly stretches from a two-minute task to a fifteen-minute ordeal, it rarely stems from network latency or hardware degradation. In one specific case involving a 2018 MacBook Pro with 16GB of RAM and Docker Desktop 2.1.0.5, the build process hung indefinitely on the "Building php" stage before eventually completing once initiated. The machine's CPU usage spiked to 270% under com.docker.hyperkit, yet no progress occurred. This behavior was isolated to the local environment; colleagues cloning the identical repository from scratch on different OSs and hardware reported instant builds.
The culprit often lies not in the Dockerfile itself, but in how the build context interacts with the host filesystem. The user's docker-compose.yaml mounted several large directories: ../web, ../moodle, and ../moodledata. These paths point to project content that likely contained untracked files, binary assets, or complex directory structures present on the developer's machine but absent on a fresh clone. When Docker processes a build context containing massive amounts of data, it must copy this data into the container image layer-by-layer before executing the RUN instructions.
If the host directories are populated with large files or deep nesting, Docker attempts to traverse and copy them during the build stage, even if they are later intended for mounting as volumes. The "hanging" behavior describedâwhere the process appears frozen on a status messageâis frequently a symptom of the daemon struggling to read or compress a huge context before the actual instructions begin executing.
The resolution involved decoupling the build environment from the project content during the initial image creation. By moving the entire /docker directory to a test folder and creating empty directories for ../web, ../moodle, and ../moodledata before invoking the build command, the process resumed immediately. This confirms that the presence of substantial data in these mounted paths was bloating the build context size or causing I/O bottlenecks specific to the host machine's filesystem (likely APFS on macOS).
To prevent this in future workflows, consider separating your source code from your Docker build artifacts. One effective strategy is to use a dedicated Dockerfile that builds dependencies and installs software without assuming any files exist in the mounted volume paths. Alternatively, you can utilize .dockerignore files to explicitly exclude unnecessary directories like node_modules, local logs, or large asset folders that do not need to be baked into the image layers.
For projects requiring frequent rebuilds, a multi-stage build approach combined with a clean context is often superior. Ensure that your docker-compose.yaml mounts only the specific data volumes required at runtime, rather than trying to include development artifacts within the build step itself. This distinction ensures that Docker treats the mounted paths as external resources rather than part of the image construction process, significantly reducing build times and avoiding filesystem-related hangs.
If you are managing a team where build performance varies wildly between machines, standardizing on a .dockerignore file is a low-effort, high-impact fix. It guarantees that every developer, regardless of their local disk clutter or clone history, sees the same lightweight context when running docker-compose build.
