John Davidson

php - Optimizing Laravel docker image


I updated the dockerfile for anyone who wants a good dockerfile for their laravel application.

I'm trying to build a Docker image from my laravel application. My application plus all the dependencies are about 380 MB but the image turns to be 840 MB. I used multistage build as Ivan suggested (Which halved the size of the image, it was 1.2 GB at first). But I still wondering why is my Docker image this big? And how can I reduce the size of the image?

Here is my Dockerfile:

# Instruction adapted from

# PHP Dependencies
FROM composer:latest as vendor

COPY database/ database/

COPY composer.json composer.json
COPY composer.lock composer.lock

RUN composer install \
--no-dev \
--ignore-platform-reqs \
--no-interaction \
--no-plugins \
--no-scripts \

# Frontend
FROM node:16.13.1 as frontend

RUN mkdir -p /app/public

COPY package.json webpack.mix.js tailwind.config.js /app/
COPY resources/ /app/resources/
COPY public/ /app/public/
COPY package-lock.json /app/package-lock.json


RUN npm ci && npm run production

# Application
FROM php:7.4-apache

COPY . /var/www/html
COPY --from=vendor /app/vendor/ /var/www/html/vendor/
COPY --from=frontend /app/public/ /var/www/html/public/

My Answer:
# Use the official PHP image as base image
FROM php:7.4-fpm AS base

# Set working directory
WORKDIR /var/www/html

# Install dependencies
RUN apt-get update && apt-get install -y \
git \
unzip \
libzip-dev \
&& docker-php-ext-install zip pdo_mysql

# Copy composer.lock and composer.json
COPY composer.lock composer.json ./

# Install composer dependencies
RUN curl -sS | php -- --install-dir=/usr/local/bin --filename=composer
RUN composer install --no-scripts --no-autoloader

# Copy the rest of the application
COPY . .

# Build the application
RUN composer dump-autoload --optimize

# Use a smaller image for production
FROM php:7.4-fpm-alpine

# Set working directory
WORKDIR /var/www/html

# Copy files from the base image
COPY --from=base /var/www/html .

# Expose port 9000 and start php-fpm server
CMD ["php-fpm"]

This Dockerfile is optimized for a Laravel application. Here are some key points to reduce the size of the image:

1. Use a smaller base image: In the final stage, use a smaller base image like `php:7.4-fpm-alpine` instead of `php:7.4-fpm`. Alpine images are smaller in size compared to Debian-based images.

2. Use multistage builds: Use multistage builds to reduce the size of the final image. In the first stage, install dependencies and build the application. In the final stage, copy only the necessary files from the first stage.

3. Remove unnecessary dependencies: Only install necessary dependencies in the Dockerfile. Avoid installing packages that are not required for the application to run.

4. Optimize composer: Use `--no-scripts` and `--no-autoloader` flags while running `composer install` to skip unnecessary scripts and autoloading.

By following these steps, you should be able to reduce the size of your Laravel Docker image significantly.

Rate this post

3 of 5 based on 4881 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us