-1

Updated 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 https://laravel-news.com/multi-stage-docker-builds-for-laravel

# 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 \
    --prefer-dist

# 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

WORKDIR /app

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/
Fariman Kashani
  • 856
  • 1
  • 16
  • 29
  • 2
    If you do `docker history `, you can see how much space each layer adds. – Hans Kilian Dec 17 '21 at 13:10
  • Does this answer your question? [Reducing Docker container size](https://stackoverflow.com/questions/65072331/reducing-docker-container-size) – Nico Haase Dec 17 '21 at 13:15
  • 1
    How does your _application_ use `git`, or `curl`, or `xvfb`, while it's running? Can you trim down that `apt-get install` dependency list to only the things your application actually needs to run? The `RUN chown` and `RUN chmod` commands also take up more space than they obviously need and probably aren't necessary. – David Maze Dec 17 '21 at 13:58
  • @DavidMaze I modified my dockerfile to use the multistage build. My image is still big. I tried using better base images and no more garbage dependencies. Do you have any other suggestions in mind? – Fariman Kashani Dec 17 '21 at 17:06

1 Answers1

2

Your image is big because it contains all application which you was install via apt-get and their dependencies.

There are multiple ways to solve problem:

  • use multistage build
  • use suitable base image
  • use Alpine linux

Multistage build

Use one base image for get/build/test your app and copy needed result to next stage.

FROM ubuntu:18.04 AS build
*do smth*
FROM php:7.4.27-fpm-alpine AS final
COPY from build...

Suitable base image

Use image that already contains environment which you need to run application. Where no need to install all these garbage.

Use Alpine linux

Use the images which based on Alpine or similar distro, who optimized for docker/clouds, and build your app based on them.

Ivan
  • 76
  • 5
  • 1
    (If you're installing 1,000 MB of OS packages, the difference between a 5 MB Alpine base and a 50 MB Ubuntu base isn't going to be noticeable, and it comes with some potential compatibility issues.) – David Maze Dec 17 '21 at 13:57
  • @Ivan I used multistage build and it halved my image size and updated my dockerfile above. But it is still very big. Do you have other suggestions in mind? – Fariman Kashani Dec 17 '21 at 17:08