Another best practice for handling dependencies and updates in your Dockerfile is to use multi-stage builds. Multi-stage builds allow you to split your Dockerfile into multiple stages, each with its own base image and commands. This can help you reduce the size and complexity of your final image, as well as isolate the dependencies for different tasks. For example, use one stage to build your application code, another stage to run tests, and another stage to create the production-ready image. Copy only the artifacts you need from one stage to another, and discard the rest. For example,
creates a builder stage with Node.js installed,
copies your application code to the builder stage,
RUN npm install && npm run build
installs and builds your application in the builder stage,
FROM nginx:alpine AS final
creates a final stage with Nginx installed, and
COPY --from=builder /app/dist /usr/share/nginx/html
copies only the built files from the builder stage to the final stage.