In Docker, each command in a Dockerfile creates a new layer in the image, and Docker caches these layers. When rebuilding an image, Docker reuses the cached layers if the commands and their contexts (files and other inputs) haven't changed. This caching mechanism speeds up the build process significantly.
To better leverage Docker's caching, especially focusing on package-lock.json:
Minimize the number of layers by combining commands where possible.
Install dependencies (npm ci) immediately after copying package.json and package-lock.json to ensure that changes to these files invalidate the cache as early as possible.
Avoid running apt-get update without an immediate apt-get install in the same RUN command to prevent using outdated package lists.