Optimize Docker with Multi-Stage Builds and Specific Base Images

Are you still using the same Dockerfile for development and production? Early in my career, I made this mistake constantly. One giant Dockerfile that installed dev dependencies, debugging tools, and everything else — then shipped it all to production. The image was bloated, slow to build, and a security risk. The fix? Multi-stage builds. They changed how I think about Docker entirely. Here's the pattern I use on almost every project now: # Stage 1: Build FROM composer:2 AS builder WORKDIR /app COPY composer.json composer.lock ./ RUN composer install --no-dev --optimize-autoloader COPY . . # Stage 2: Production FROM php:8.3-fpm-alpine COPY --from=builder /app /var/www/html RUN chown -R www-data:www-data /var/www/html/storage The key benefits I've seen after adopting this approach: - Final image dropped from ~800MB to ~120MB - Build cache works better because dependency install is separated from code copy - No dev tools or composer in the production image (smaller attack surface) - CI/CD pipelines got noticeably faster One extra tip: always use specific base image tags (php:8.3-fpm-alpine instead of php:latest). I learned this the hard way when a "latest" tag update broke a production deployment on a Friday evening. After 9+ years building and deploying applications, I can say that investing time in your Docker setup pays off every single day — faster builds, safer deployments, and fewer surprises at 2 AM. What's your go-to Docker optimization trick? I'd love to hear what's worked for your team. #Docker #DevOps #PHP #Laravel #Backend #WebDevelopment #CICD

To view or add a comment, sign in

Explore content categories